ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
testKeywordEx.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_dpm_libmerge
4  * @copyright ESO - European Southern Observatory
5  */
6 #include <daq/dpm/keywordEx.hpp>
7 
8 #include <gtest/gtest.h>
9 
10 namespace daq::dpm {
11 
12 class TestKeywordMatchP : public ::testing::Test,
13  public ::testing::WithParamInterface<fits::KeywordVariant> {};
14 
15 class TestKeywordMatch : public ::testing::Test {};
16 class TestKeywordTransform : public ::testing::Test {};
17 
18 TEST(TestParseEx, FailsIfOperatorIsInvalid) {
19  using namespace detail;
20 
21  EXPECT_THROW(ParseEx(""), std::invalid_argument);
22  EXPECT_THROW(ParseEx("? *"), std::invalid_argument);
23  EXPECT_THROW(ParseEx("a *"), std::invalid_argument);
24 }
25 
26 TEST(TestParseEx, FailsIfScopeIsInValid) {
27  using namespace detail;
28 
29  EXPECT_THROW(ParseEx("+"), std::invalid_argument);
30  EXPECT_THROW(ParseEx("+a *"), std::invalid_argument);
31  EXPECT_THROW(ParseEx("+ve *"), std::invalid_argument);
32  EXPECT_THROW(ParseEx("+cc *"), std::invalid_argument);
33 }
34 
35 TEST(TestParseEx, FailsIfWhitespaceIsMssing) {
36  using namespace detail;
37 
38  EXPECT_THROW(ParseEx("+pattern"), std::invalid_argument);
39  EXPECT_THROW(ParseEx("+vpattern"), std::invalid_argument);
40 }
41 
42 TEST(TestParseEx, FailsIfPatternIsMissing) {
43  using namespace detail;
44 
45  EXPECT_THROW(ParseEx("+ "), std::invalid_argument);
46  EXPECT_THROW(ParseEx("+e"), std::invalid_argument);
47 }
48 
49 TEST(TestParseEx, IncludeEsoKw) {
50  using namespace detail;
51 
52  Rule r = ParseEx("+e *");
53 
54  EXPECT_EQ(r.op, Operator::Include);
55  EXPECT_EQ(r.scope, Scope::Eso);
56  EXPECT_EQ(r.pattern, "*");
57 }
58 
59 TEST(TestParseEx, ExcludeEsoKw) {
60  using namespace detail;
61 
62  Rule r = ParseEx("-e *");
63 
64  EXPECT_EQ(r.op, Operator::Exclude);
65  EXPECT_EQ(r.scope, Scope::Eso);
66  EXPECT_EQ(r.pattern, "*");
67 }
68 
69 TEST(TestParseEx, IncludeValueKw) {
70  using namespace detail;
71 
72  Rule r = ParseEx("+v *");
73 
74  EXPECT_EQ(r.op, Operator::Include);
75  EXPECT_EQ(r.scope, Scope::Value);
76  EXPECT_EQ(r.pattern, "*");
77 }
78 
79 TEST(TestParseEx, IncludeCommentaryKw) {
80  using namespace detail;
81 
82  Rule r = ParseEx("+c *");
83 
84  EXPECT_EQ(r.op, Operator::Include);
85  EXPECT_EQ(r.scope, Scope::Commentary);
86  EXPECT_EQ(r.pattern, "*");
87 }
88 
89 TEST(TestParseEx, IncludeAnyKw) {
90  using namespace detail;
91 
92  Rule r = ParseEx("+ pat");
93 
94  EXPECT_EQ(r.op, Operator::Include);
95  EXPECT_EQ(r.scope, Scope::Any);
96  EXPECT_EQ(r.pattern, "pat");
97 }
98 
99 TEST(TestKeywordEx, Constructors) {
100  EXPECT_NO_THROW(KeywordEx()) << "Default constructor";
101  EXPECT_NO_THROW(KeywordEx("+ *")) << "Single rule";
102  EXPECT_NO_THROW(KeywordEx({"+ *", "- *"})) << "From initilizer list";
103  std::vector<std::string> strings = {"+ *", "- *"};
104  EXPECT_NO_THROW(KeywordEx(std::begin(strings), std::end(strings))) << "From iterators";
105 }
106 
107 TEST_P(TestKeywordMatchP, EmptyPatternDoesntMatch) {
108  EXPECT_FALSE(KeywordMatch(GetParam(), KeywordEx()));
109 }
110 
111 TEST_P(TestKeywordMatchP, MatchAnyWithSingle) {
112  EXPECT_TRUE(KeywordMatch(GetParam(), KeywordEx("+ *")));
113 }
114 
115 TEST_P(TestKeywordMatchP, MatchAnyWithMultiple) {
116  EXPECT_TRUE(KeywordMatch(GetParam(), KeywordEx({"+e *", "+v *", "+c *"})));
117  EXPECT_TRUE(KeywordMatch(GetParam(), KeywordEx({"+e VALUE", "+v VALUE", "+c VALUE"})));
118 }
119 
122  testing::Values(fits::EsoKeyword("VALUE", "value"),
123  fits::ValueKeyword("VALUE", 0.0),
124  fits::LiteralKeyword("VALUE = 'tgt' / comment"),
125  fits::LiteralKeyword("VALUE Comment")));
126 
127 TEST_F(TestKeywordMatch, EsoSimpleInclusivePattern) {
128  auto kw = fits::EsoKeyword("CAT", "value");
129  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+ CAT")));
130  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+e CAT")));
131  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+e C?T")));
132  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+e *")));
133  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+ *")));
134 
135  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+e C")));
136  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+v CAT")));
137  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+c CAT")));
138 }
139 
140 TEST_F(TestKeywordMatch, ValueSimpleInclusivePattern) {
141  auto kw = fits::ValueKeyword("ORIGIN", "value");
142  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+ ORIGIN")));
143  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+v ORIGIN")));
144  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+v OR*IN")));
145  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+v *")));
146  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+ *")));
147 
148  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+v ORIGI")));
149  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+e ORIGIN")));
150  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+c ORIGIN")));
151 }
152 
153 TEST_F(TestKeywordMatch, LiteralSimpleInclusivePattern) {
154  auto kw = fits::LiteralKeyword("ORIGIN = 'value'");
155  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+ ORIGIN")));
156  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+v ORIGIN")));
157  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+v OR*IN")));
158  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+v *")));
159  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+ *")));
160 
161  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+v ORIGI")));
162  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+e ORIGIN")));
163  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+c ORIGIN")));
164 }
165 
166 TEST_F(TestKeywordMatch, CommentarySimpleInclusivePattern) {
167  auto kw = fits::LiteralKeyword("COMMENT value");
168  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+ COMMENT")));
169  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+c COMMENT")));
170  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+ COMME*")));
171  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+c *")));
172  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+ *")));
173 
174  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+c COMME")));
175  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+e COMMENT")));
176  EXPECT_FALSE(KeywordMatch(kw, KeywordEx("+v COMMENT")));
177 }
178 
179 TEST_F(TestKeywordMatch, SimpleExclusivePattern) {
180  EXPECT_FALSE(KeywordMatch(fits::EsoKeyword("CAT", "value"), KeywordEx("- CAT")));
181  EXPECT_FALSE(KeywordMatch(fits::EsoKeyword("CAT", "value"), KeywordEx("-e CAT")));
182  EXPECT_FALSE(KeywordMatch(fits::EsoKeyword("CAT", "value"), KeywordEx("-e C?T")));
183  EXPECT_FALSE(KeywordMatch(fits::EsoKeyword("CAT", "value"), KeywordEx("-e *")));
184  EXPECT_FALSE(KeywordMatch(fits::EsoKeyword("CAT", "value"), KeywordEx("- *")));
185 
186  EXPECT_FALSE(KeywordMatch(fits::EsoKeyword("CAT", "value"), KeywordEx("-e C")));
187  EXPECT_FALSE(KeywordMatch(fits::EsoKeyword("CAT", "value"), KeywordEx("-v CAT")));
188  EXPECT_FALSE(KeywordMatch(fits::EsoKeyword("CAT", "value"), KeywordEx("-c CAT")));
189 }
190 
191 TEST_F(TestKeywordMatch, EsoInclusiveExclusivePattern) {
192  auto kw = fits::EsoKeyword("CAT TEST", "value");
193  EXPECT_TRUE(KeywordMatch(kw, KeywordEx("+e CAT*")));
194 
195  EXPECT_FALSE(KeywordMatch(kw, KeywordEx({"+e CAT", "-e CAT TEST"})));
196  EXPECT_FALSE(KeywordMatch(kw, KeywordEx({"+e *", "-e *"})));
197  EXPECT_FALSE(KeywordMatch(kw, KeywordEx({"+ *", "-e *"})));
198 }
199 
201  auto kw = fits::EsoKeyword("CAT FOO", "value");
202  EXPECT_EQ(KeywordTransform(kw, std::regex("^CAT"), "CAT1"),
203  fits::EsoKeyword("CAT1 FOO", "value"));
204  EXPECT_EQ(KeywordTransform(fits::KeywordVariant(kw), std::regex("^CAT"), "CAT1"),
205  fits::KeywordVariant(fits::EsoKeyword("CAT1 FOO", "value")));
206 }
207 
208 TEST_F(TestKeywordTransform, InvalidEsoKeyword) {
209  std::string long_name(70, 'A');
210  auto kw = fits::EsoKeyword("CAT FOO", "value");
211  EXPECT_THROW(KeywordTransform(kw, std::regex("^CAT"), long_name.c_str()), std::runtime_error);
212 }
213 
215  auto kw = fits::ValueKeyword("FOOBAR", "value");
216  EXPECT_EQ(KeywordTransform(kw, std::regex("^FOO"), "F00"),
217  fits::ValueKeyword("F00BAR", "value"));
218  EXPECT_EQ(KeywordTransform(fits::KeywordVariant(kw), std::regex("^FOO"), "F00"),
219  fits::KeywordVariant(fits::ValueKeyword("F00BAR", "value")));
220 }
221 
222 TEST_F(TestKeywordTransform, InvalidValueKeyword) {
223  std::string long_name(70, 'A');
224  auto kw = fits::ValueKeyword("CAT FOO", "value");
225  EXPECT_THROW(KeywordTransform(kw, std::regex("^CAT"), long_name.c_str()), std::runtime_error);
226  EXPECT_THROW(KeywordTransform(kw, std::regex("^CAT"), "%!@"), std::runtime_error);
227 }
228 
229 TEST_F(TestKeywordTransform, LiteralKeyword) {
230  using fits::KeywordVariant;
231  using fits::LiteralKeyword;
232  LiteralKeyword kw1("HIERARCH ESO A1 = B");
233  EXPECT_EQ(KeywordTransform(KeywordVariant(kw1), std::regex("^A1"), "$& B1"),
234  fits::KeywordVariant(LiteralKeyword("HIERARCH ESO A1 B1 = B /")));
235 }
236 
237 } // namespace daq::dpm
daq::dpm::TEST
TEST(TestParseEx, FailsIfOperatorIsInvalid)
Definition: testKeywordEx.cpp:18
keywordEx.hpp
daq::dpm::INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(Test, TestKeywordMatchP, testing::Values(fits::EsoKeyword("VALUE", "value"), fits::ValueKeyword("VALUE", 0.0), fits::LiteralKeyword("VALUE = 'tgt' / comment"), fits::LiteralKeyword("VALUE Comment")))
daq::fits::KeywordVariant
std::variant< ValueKeyword, EsoKeyword, LiteralKeyword > KeywordVariant
The different variants of keywords that are supported.
Definition: keyword.hpp:400
daq::dpm::KeywordTransform
fits::KeywordVariant KeywordTransform(fits::KeywordVariant const &keyword, std::regex const &re, char const *fmt)
Transforms keyword name using regex.
Definition: keywordEx.cpp:164
daq::fits::Eso
@ Eso
An ESO hiearchical keyword.
Definition: keyword.hpp:72
daq::dpm::TestKeywordMatchP
Definition: testKeywordEx.cpp:13
daq::fits::EsoKeyword
BasicKeyword< EsoKeywordTraits > EsoKeyword
ESO hiearchical keyword.
Definition: keyword.hpp:337
daq::fits::BasicKeyword
A type safe version of LiteralKeyword that consist of the three basic components of a FITS keyword ke...
Definition: keyword.hpp:266
daq::dpm::TEST_P
TEST_P(TestKeywordMatchP, EmptyPatternDoesntMatch)
Definition: testKeywordEx.cpp:107
daq::dpm::detail::Scope::Any
@ Any
daq::dpm::KeywordEx
Create keyword expression that memoize the provided string pattern.
Definition: keywordEx.hpp:59
daq::dpm
Definition: testDpSpec.cpp:16
daq::dpm::TestKeywordTransform
Definition: testKeywordEx.cpp:16
daq::fits::ValueKeyword
BasicKeyword< ValueKeywordTraits > ValueKeyword
Standard FITS value keyword.
Definition: keyword.hpp:330
daq::dpm::TestKeywordMatch
Definition: testKeywordEx.cpp:15
daq::dpm::TEST_F
TEST_F(TestParseOrigin, ParseSucceeds)
Definition: testDpSpec.cpp:20
daq::dpm::detail::Operator::Include
@ Include
daq::fits::Value
@ Value
A value keyword.
Definition: keyword.hpp:68
daq::dpm::detail::ParseEx
Rule ParseEx(std::string_view ex)
Parse expression of the form documented in KeywordEx.
Definition: keywordEx.cpp:17
daq::dpm::KeywordMatch
bool KeywordMatch(fits::KeywordVariant const &keyword, KeywordEx const &ex)
Definition: keywordEx.cpp:150
daq::fits::LiteralKeyword
Represents the literal 80-character FITS keyword record.
Definition: keyword.hpp:125