ifw-daq 3.1.0
IFW Data Acquisition modules
Loading...
Searching...
No Matches
testDictKeywordFormatter.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @copyright
4 * (c) Copyright ESO 2024
5 * All Rights Reserved
6 * ESO (eso.org) is an Intergovernmental Organisation, and therefore special legal conditions apply.
7 */
9
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13using namespace ::testing;
14using core::dit::did::DataType;
15using core::dit::did::Record;
16
17#define EXPECT_THROW_THAT(stmt, etype, matcher) \
18 EXPECT_THROW( \
19 try { stmt; } catch (const etype& ex) { \
20 EXPECT_THAT(std::string(ex.what()), matcher); \
21 throw; \
22 }, \
23 etype)
24
25namespace daq {
26using namespace daq::fits;
27
28class TestFormatKeyword : public Test {
29public:
30 void SetUp() {
31 m_str_record = Record("NAME",
32 core::dit::did::DataTypeToStr(DataType::STRING),
33 "COMMENT",
34 /*desccription*/ "",
35 /*format*/ "",
36 /*unit*/ "");
37 m_float_record = Record("NAME",
38 core::dit::did::DataTypeToStr(DataType::DOUBLE),
39 "COMMENT",
40 /*desccription*/ "",
41 /*format*/ ".3f",
42 /*unit*/ "");
43 m_float_no_format_record = Record("NAME",
44 core::dit::did::DataTypeToStr(DataType::DOUBLE),
45 "COMMENT",
46 /*desccription*/ "",
47 /*format*/ "",
48 /*unit*/ "");
49 m_bool_record = Record("NAME",
50 core::dit::did::DataTypeToStr(DataType::BOOL),
51 "COMMENT",
52 /*desccription*/ "",
53 /*format*/ "",
54 /*unit*/ "");
55 m_int_record = Record("NAME",
56 core::dit::did::DataTypeToStr(DataType::INTEGER),
57 "COMMENT",
58 /*desccription*/ "",
59 /*format*/ "",
60 /*unit*/ "");
61 }
62
68};
69
73
74TEST_F(TestFormatLiteralKeyword, StringIsWrongType) {
75 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = 'string' / comment"), m_float_record),
77 HasSubstr("parsing floating point value"));
78}
79
81 // Note: LiteralKeyword will make minimal validation of the string token so malformed strings
82 // cannot be parsed at all.
83 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = '\x1f' / comment"), m_str_record),
85 HasSubstr("invalid character"));
86 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = '\x7f' / comment"), m_str_record),
88 HasSubstr("invalid character"));
89}
90
92 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = /comment"), m_float_record),
94 HasSubstr("parsing floating point value"));
95 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = 123.456f / comment"), m_float_record),
97 HasSubstr("parsing floating point value"));
98 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = a1234 / comment"), m_float_record),
100 HasSubstr("parsing floating point value"));
101 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = . / comment"), m_float_record),
103 HasSubstr("parsing floating point value"));
104 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = 1..2 / comment"), m_float_record),
106 HasSubstr("parsing floating point value"));
107 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = 1,34 / comment"), m_float_record),
109 HasSubstr("parsing floating point value"));
110}
111
113 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = 123.456 / comment"), m_int_record),
115 HasSubstr("parsing base-10 integer value"))
116 << "floats should not be demoted to integers";
117 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = /comment"), m_int_record),
119 HasSubstr("parsing base-10 integer value"));
120 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = -+0 /comment"), m_int_record),
122 HasSubstr("parsing base-10 integer value"));
123 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = 123.456f / comment"), m_int_record),
125 HasSubstr("parsing base-10 integer value"));
126 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = a1234 / comment"), m_int_record),
128 HasSubstr("parsing base-10 integer value"));
129 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = / comment"), m_int_record),
131 HasSubstr("parsing base-10 integer value"));
132 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = 1..2 / comment"), m_int_record),
134 HasSubstr("parsing base-10 integer value"));
135 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = 0x0a / comment"), m_int_record),
137 HasSubstr("parsing base-10 integer value"));
138}
139
141 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = /comment"), m_bool_record),
143 HasSubstr("is not `T` or `F`"));
144 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = TRUE /comment"), m_bool_record),
146 HasSubstr("is not `T` or `F`"));
147 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = 'T' /comment"), m_bool_record),
149 HasSubstr("is not `T` or `F`"));
150 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = TT /comment"), m_bool_record),
152 HasSubstr("is not `T` or `F`"));
153 EXPECT_THROW_THAT(Format(LiteralKeyword("NAME = A /comment"), m_bool_record),
155 HasSubstr("is not `T` or `F`"));
156}
157
159 {
160 auto befor = LiteralKeyword("NAME = 'string' /comment");
161 auto after = LiteralKeyword("NAME = 'string' / comment");
162 ASSERT_EQ(befor.GetType(), KeywordType::Value);
163 EXPECT_EQ(Format(befor, m_str_record), after);
164 }
165 {
166 auto befor = LiteralKeyword("NAME = 'str''ng' /comment");
167 auto after = LiteralKeyword("NAME = 'str''ng' / comment");
168 ASSERT_EQ(befor.GetType(), KeywordType::Value);
169 EXPECT_EQ(Format(befor, m_str_record), after);
170 }
171 {
172 auto befor = LiteralKeyword("NAME = 'str''ng' /");
173 auto after = LiteralKeyword("NAME = 'str''ng' / COMMENT");
174 ASSERT_EQ(befor.GetType(), KeywordType::Value);
175 EXPECT_EQ(Format(befor, m_str_record), after);
176 }
177 {
178 auto befor = LiteralKeyword("HIERARCH ESO NAME = 'string ' /comment");
179 auto after = LiteralKeyword("HIERARCH ESO NAME = 'string ' / comment");
180 ASSERT_EQ(befor.GetType(), KeywordType::Eso);
181 EXPECT_EQ(Format(befor, m_str_record), after);
182 }
183 {
184 auto befor = LiteralKeyword("HIERARCH ESO NAME = 'str''ng' /comment");
185 auto after = LiteralKeyword("HIERARCH ESO NAME = 'str''ng' / comment");
186 ASSERT_EQ(befor.GetType(), KeywordType::Eso);
187 EXPECT_EQ(Format(befor, m_str_record), after);
188 }
189 {
190 auto befor = LiteralKeyword("HIERARCH ESO NAME = 'str''ng' /");
191 auto after = LiteralKeyword("HIERARCH ESO NAME = 'str''ng' / COMMENT");
192 ASSERT_EQ(befor.GetType(), KeywordType::Eso);
193 EXPECT_EQ(Format(befor, m_str_record), after) << "Expected comment from record to be added";
194 }
195}
196
197TEST_F(TestFormatLiteralKeyword, ValidFloatsAreReformatted) {
198 {
199 auto befor = LiteralKeyword("NAME = 12.3456 /comment");
200 auto after = LiteralKeyword("NAME = 12.346 / comment");
201 ASSERT_EQ(befor.GetType(), KeywordType::Value);
202 EXPECT_EQ(Format(befor, m_float_record), after);
203 }
204 {
205 auto befor = LiteralKeyword("NAME = 12.1 /");
206 auto after = LiteralKeyword("NAME = 12.100 / COMMENT");
207 ASSERT_EQ(befor.GetType(), KeywordType::Value);
208 EXPECT_EQ(Format(befor, m_float_record), after);
209 }
210 {
211 auto befor = LiteralKeyword("NAME = 12 /");
212 auto after = LiteralKeyword("NAME = 12.000 / COMMENT");
213 ASSERT_EQ(befor.GetType(), KeywordType::Value);
214 EXPECT_EQ(Format(befor, m_float_record), after)
215 << "Integers are allowed to be promoted to float";
216 }
217 {
218 auto befor = LiteralKeyword("HIERARCH ESO NAME = 12. /comment");
219 auto after = LiteralKeyword("HIERARCH ESO NAME = 12.000 / comment");
220 ASSERT_EQ(befor.GetType(), KeywordType::Eso);
221 EXPECT_EQ(Format(befor, m_float_record), after);
222 }
223 {
224 auto befor = LiteralKeyword("HIERARCH ESO NAME = 12.1 /");
225 auto after = LiteralKeyword("HIERARCH ESO NAME = 12.100 / COMMENT");
226 ASSERT_EQ(befor.GetType(), KeywordType::Eso);
227 EXPECT_EQ(Format(befor, m_float_record), after);
228 }
229 {
230 auto befor = LiteralKeyword("HIERARCH ESO NAME = 12 /");
231 auto after = LiteralKeyword("HIERARCH ESO NAME = 12.000 / COMMENT");
232 ASSERT_EQ(befor.GetType(), KeywordType::Eso);
233 EXPECT_EQ(Format(befor, m_float_record), after)
234 << "Integers are allowed to be promoted to float";
235 }
236}
237
238TEST_F(TestFormatLiteralKeyword, ValidFloatsWithoutFormatKeepsFormatting) {
239 {
240 auto befor = LiteralKeyword("NAME = 12.3456 /comment");
241 auto after = LiteralKeyword("NAME = 12.3456 / comment");
242 ASSERT_EQ(befor.GetType(), KeywordType::Value);
243 EXPECT_EQ(Format(befor, m_float_no_format_record), after);
244 }
245 {
246 auto befor = LiteralKeyword("NAME = 12.1 /");
247 auto after = LiteralKeyword("NAME = 12.1 / COMMENT");
248 ASSERT_EQ(befor.GetType(), KeywordType::Value);
249 EXPECT_EQ(Format(befor, m_float_no_format_record), after);
250 }
251 {
252 auto befor = LiteralKeyword("NAME = 12 /");
253 auto after = LiteralKeyword("NAME = 12 / COMMENT");
254 ASSERT_EQ(befor.GetType(), KeywordType::Value);
255 EXPECT_EQ(Format(befor, m_float_no_format_record), after)
256 << "Integers are allowed to be promoted to float";
257 }
258 {
259 auto befor = LiteralKeyword("HIERARCH ESO NAME = 12.3456 /comment");
260 auto after = LiteralKeyword("HIERARCH ESO NAME = 12.3456 / comment");
261 ASSERT_EQ(befor.GetType(), KeywordType::Eso);
262 EXPECT_EQ(Format(befor, m_float_no_format_record), after);
263 }
264 {
265 auto befor = LiteralKeyword("HIERARCH ESO NAME = 12.1 /");
266 auto after = LiteralKeyword("HIERARCH ESO NAME = 12.1 / COMMENT");
267 ASSERT_EQ(befor.GetType(), KeywordType::Eso);
268 EXPECT_EQ(Format(befor, m_float_no_format_record), after);
269 }
270 {
271 auto befor = LiteralKeyword("HIERARCH ESO NAME = 12 /");
272 auto after = LiteralKeyword("HIERARCH ESO NAME = 12 / COMMENT");
273 ASSERT_EQ(befor.GetType(), KeywordType::Eso);
274 EXPECT_EQ(Format(befor, m_float_no_format_record), after)
275 << "Integers are allowed to be promoted to float";
276 }
277}
278
280 {
281 auto befor = LiteralKeyword("NAME = T /");
282 auto after = LiteralKeyword("NAME = T / COMMENT");
283 ASSERT_EQ(befor.GetType(), KeywordType::Value);
284 EXPECT_EQ(Format(befor, m_bool_record), after);
285 }
286 {
287 auto befor = LiteralKeyword("NAME = F / comment");
288 auto after = LiteralKeyword("NAME = F / comment");
289 ASSERT_EQ(befor.GetType(), KeywordType::Value);
290 EXPECT_EQ(Format(befor, m_bool_record), after);
291 }
292 {
293 auto befor = LiteralKeyword("NAME = T /");
294 auto after = LiteralKeyword("NAME = T / COMMENT");
295 ASSERT_EQ(befor.GetType(), KeywordType::Value);
296 EXPECT_EQ(Format(befor, m_bool_record), after);
297 }
298 {
299 auto befor = LiteralKeyword("HIERARCH ESO NAME = T /");
300 auto after = LiteralKeyword("HIERARCH ESO NAME = T / COMMENT");
301 ASSERT_EQ(befor.GetType(), KeywordType::Eso);
302 EXPECT_EQ(Format(befor, m_bool_record), after);
303 }
304}
305
307 {
308 auto befor = ValueKeyword("NAME", "str'in'g", "comment");
309 auto after = LiteralKeyword("NAME = 'str''in''g' / comment");
310 EXPECT_EQ(Format(befor, m_str_record), after);
311 }
312}
313
315 {
316 auto befor = ValueKeyword("NAME", true, "comment");
317 auto after = LiteralKeyword("NAME = T / comment");
318 EXPECT_EQ(Format(befor, m_bool_record), after);
319 }
320}
321
323 {
324 auto befor = ValueKeyword("NAME", 12.3456);
325 auto after = LiteralKeyword("NAME = 12.346 / COMMENT");
326 EXPECT_EQ(Format(befor, m_float_record), after);
327 }
328 {
329 auto befor = ValueKeyword("NAME", 12.34124, "comment");
330 auto after = LiteralKeyword("NAME = 12.3 / comment");
331 EXPECT_EQ(Format(befor, m_float_no_format_record), after);
332 }
333}
334
335TEST_F(TestFormatValueKeyword, ValidFloatNoDecimalDigits) {
336 {
337 auto record = Record("NAME",
338 core::dit::did::DataTypeToStr(DataType::DOUBLE),
339 "COMMENT",
340 /*desccription*/ "",
341 /*format*/ "#.0f",
342 /*unit*/ "");
343 auto befor = ValueKeyword("NAME", -12.3456);
344 auto after = LiteralKeyword("NAME = -12. / COMMENT");
345 EXPECT_EQ(Format(befor, record), after);
346 }
347}
348
350 {
351 auto befor = ValueKeyword("NAME", static_cast<std::uint64_t>(12u), "");
352 auto after = LiteralKeyword("NAME = 12 / ");
353 EXPECT_EQ(Format(befor, m_int_record), after);
354 }
355 {
356 auto befor = ValueKeyword("NAME", -12);
357 auto after = LiteralKeyword("NAME = -12 / COMMENT");
358 EXPECT_EQ(Format(befor, m_int_record), after);
359 }
360}
361
362TEST_F(TestFormatValueKeyword, NoRecordCommentCoverage) {
363 {
364 auto record = Record("NAME", core::dit::did::DataTypeToStr(DataType::INTEGER), "", "", "");
365 auto befor = ValueKeyword("NAME", -12);
366 auto after = LiteralKeyword("NAME = -12 /");
367 EXPECT_EQ(Format(befor, record), after);
368 }
369}
370
371TEST_F(TestFormatValueKeyword, StringIsWrongType) {
372 EXPECT_THROW_THAT(Format(ValueKeyword("NAME", "str'in'g", "comment"), m_bool_record),
374 HasSubstr("dictionary specifies value to be DidBoolKey"));
375}
376
377TEST_F(TestFormatValueKeyword, DoubleIsWrongType) {
378 EXPECT_THROW_THAT(Format(ValueKeyword("NAME", 0.1, "comment"), m_bool_record),
380 HasSubstr("dictionary specifies value to be DidBoolKey"));
381}
382
383TEST_F(TestFormatEsoKeyword, DoubleIsWrongType) {
384 EXPECT_THROW_THAT(Format(EsoKeyword("NAME", 0.1, "comment"), m_bool_record),
386 HasSubstr("dictionary specifies value to be DidBoolKey"));
387}
388
389TEST_F(TestFormatEsoKeyword, StringIsWrongType) {
390 EXPECT_THROW_THAT(Format(EsoKeyword("NAME", "str'in'g", "comment"), m_int_record),
392 HasSubstr("dictionary specifies value to be DidIntKey"));
393}
394
395TEST_F(TestFormatEsoKeyword, IntegersAreTheWrongType) {
397 Format(ValueKeyword("NAME", static_cast<std::uint64_t>(1), "comment"), m_bool_record),
399 HasSubstr("dictionary specifies value to be DidBoolKey but value is uint64"));
401 Format(ValueKeyword("NAME", static_cast<std::int64_t>(1), "comment"), m_bool_record),
403 HasSubstr("dictionary specifies value to be DidBoolKey but value is int64"));
404}
405
406TEST_F(TestFormatEsoKeyword, BoolIsTheWrongType) {
407 EXPECT_THROW_THAT(Format(ValueKeyword("NAME", false, "comment"), m_int_record),
409 HasSubstr("dictionary specifies value to be DidIntKey but value is bool"));
410}
411
412} // namespace daq
Indicates keyword is invalid for some reason.
Definition: keyword.hpp:534
Represents the literal 80-character FITS keyword record.
Definition: keyword.hpp:129
LiteralKeyword Format(KeywordVariant const &keyword)
Definition: keyword.cpp:782
BasicKeyword< ValueKeywordTraits > ValueKeyword
Standard FITS value keyword.
Definition: keyword.hpp:339
BasicKeyword< EsoKeywordTraits > EsoKeyword
ESO hiearchical keyword.
Definition: keyword.hpp:346
TEST_F(TestDpmClient, StartMonitoringSendsRequestAndReceivesReply)
#define EXPECT_THROW_THAT(stmt, etype, matcher)
EXPECT_EQ(meta.rr_uri, "zpb.rr://meta")
ASSERT_EQ(meta.keyword_rules.size(), 1u)