btool
A parser/converter/transpiler for .bib files
TranslationTableFixtureTest.cpp
Go to the documentation of this file.
1 #include <TranslationTable.hpp>
2 #include <gtest/gtest.h>
3 #include <boost/filesystem/path.hpp>
4 #include <utility>
5 #include <sstream>
6 #include <vector>
7 
8 using namespace std::literals::string_literals;
9 
15 struct TranslationTableFixtureTest : public ::testing::Test {
17 
19  // language=json
20  std::stringstream{R"(
21  {
22  "sortableFields": [],
23  "styles": [
24  {
25  "name": "article",
26  "requiredFields": [
27  "author",
28  "title"
29  ],
30  "optionalFields": [
31  "some",
32  "other",
33  "field"
34  ]
35  },
36  {
37  "name": "book",
38  "requiredFields": [
39  "author",
40  "title",
41  "year",
42  "publisher"
43  ],
44  "optionalFields": [
45  "volume",
46  "series",
47  "address",
48  "edition",
49  "month",
50  "note",
51  "isbn"
52  ]
53  }
54  ]
55  })"}
56  } {};
57 };
58 
59 TEST_F(TranslationTableFixtureTest, getAllStylesTest) {
60  const auto expectedStyles = std::vector<StyleProperties>{
61  {
62  "article"s,
63  {
64  "author"s,
65  "title"s,
66  },
67  {
68  "some"s,
69  "other"s,
70  "field"s
71  }
72  },
73  {
74  "book"s,
75  {
76  "author"s,
77  "title"s,
78  "year"s,
79  "publisher"s
80  },
81  {
82  "volume"s,
83  "series"s,
84  "address"s,
85  "edition"s,
86  "month"s,
87  "note"s,
88  "isbn"s
89  }
90  }
91  };
92  ASSERT_EQ(sample.getStyleProperties(), expectedStyles);
93 }
94 
96  ASSERT_EQ(sample.stylePropertiesOf("book"s),
98  "book"s,
99  {
100  "author"s,
101  "title"s,
102  "year"s,
103  "publisher"s
104  },
105  {
106  "volume"s,
107  "series"s,
108  "address"s,
109  "edition"s,
110  "month"s,
111  "note"s,
112  "isbn"s
113  }
114  })
115  );
116 }
117 
118 TEST_F(TranslationTableFixtureTest, getInvalidStyleTest) {
119  ASSERT_EQ(sample.stylePropertiesOf("invalid style"s), std::nullopt);
120 }
121 
122 TEST(TranslationTableTest, constructorTestFromPath) {
123  ASSERT_NO_THROW(TranslationTable(boost::filesystem::path("../../src/translationTable.json")));
124 }
125 
126 TEST(TranslationTableTest, parsingInvalidStyleFieldsTest) {
127  auto invalidContent = std::stringstream{R"(
128  {
129  "styles": [
130  {
131  "name": "garbage",
132  "abc": "def"
133  }
134  ]
135  }
136  )"};
137  const auto table = TranslationTable(std::move(invalidContent));
138  const auto expected = std::vector<StyleProperties>{{"garbage", {}, {}}};
139  ASSERT_EQ(table.getStyleProperties(), expected);
140 }
141 
142 TEST(TranslationTableTest, parsingInvalidStyleNameTest) {
143  auto invalidContent = std::stringstream{R"(
144  {
145  "styles": [
146  {
147  "someKey": "garbage",
148  "requiredFields": "def",
149  "optionalFields": "def"
150  }
151  ]
152  }
153  )"};
154  const auto table = TranslationTable(std::move(invalidContent));
155  const auto expected = std::vector<StyleProperties>{{"", {}, {}}};
156  ASSERT_EQ(table.getStyleProperties(), expected);
157 }
158 
159 TEST(TranslationTableTest, parsingInvalidStylesTest) {
160  auto invalidContent = std::stringstream{R"(
161  {
162  "not styles": [
163  {
164  "name": "garbage",
165  "requiredFields": "def",
166  "optionalFields": "def"
167  }
168  ]
169  }
170  )"};
171  ASSERT_ANY_THROW(const auto table = TranslationTable(std::move(invalidContent)));
172 }
173 
174 TEST(TranslationTableTest, parsingInvalidFileContent) {
175  auto invalidContent = std::stringstream{R"(
176  {,
177  "some": [
178  {
179  "invalid": "json"
180  },
181  ],
182  }
183  )"};
184  ASSERT_ANY_THROW(const auto table = TranslationTable(std::move(invalidContent)));
185 }
186 
187 TEST(TranslationTableTest, parsingInvalidFile) {
188  auto invalidFile = boost::filesystem::path("./someUnExistingFile.json");
189  ASSERT_ANY_THROW(const auto table = TranslationTable(invalidFile););
190 }
191 
192 TEST(TranslationTableTest, parsingDirectory) {
193  auto invalidFile = boost::filesystem::path(".");
194  ASSERT_ANY_THROW(const auto table = TranslationTable(invalidFile););
195 }
StyleProperties
style-properties-container
Definition: StyleProperties.hpp:11
TranslationTableFixtureTest
Definition: TranslationTableFixtureTest.cpp:15
TranslationTableFixtureTest::TranslationTableFixtureTest
TranslationTableFixtureTest()
Definition: TranslationTableFixtureTest.cpp:18
TEST
TEST(TranslationTableTest, constructorTestFromPath)
Definition: TranslationTableFixtureTest.cpp:122
TEST_F
TEST_F(TranslationTableFixtureTest, getAllStylesTest)
Definition: TranslationTableFixtureTest.cpp:59
TranslationTableFixtureTest::sample
TranslationTable sample
Definition: TranslationTableFixtureTest.cpp:16
TranslationTable.hpp
TranslationTable
api to interact with translation-table resource
Definition: TranslationTable.hpp:16