UFO
Parameters.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2019 Met Office UK
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #ifndef TEST_UFO_PARAMETERS_H_
9 #define TEST_UFO_PARAMETERS_H_
10 
11 #include <string>
12 #include <vector>
13 
14 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
15 
16 #include "eckit/config/LocalConfiguration.h"
17 #include "eckit/testing/Test.h"
18 #include "oops/runs/Test.h"
19 #include "oops/util/Expect.h"
20 #include "oops/util/parameters/OptionalParameter.h"
21 #include "oops/util/parameters/Parameters.h"
22 #include "test/TestEnvironment.h"
24 
25 namespace ufo {
26 namespace test {
27 
28 const bool validationSupported = oops::Parameters::isValidationSupported();
29 
30 class MyParameters : public oops::Parameters {
31  OOPS_CONCRETE_PARAMETERS(MyParameters, Parameters)
32  public:
33  oops::OptionalParameter<ufo::Variable> optVariableParameter{"opt_variable_parameter", this};
34 };
35 
36 template <typename ParametersType>
37 void doTestSerialization(const eckit::Configuration &config) {
38  // We deserialize a configuration loaded from a YAML file into parameters and then serialize them
39  // back into a configuration. The test verifies that the configuration objects produce the same
40  // output when printed.
41  //
42  // For this to work, parameter names in the YAML file must be ordered alphabetically; that's
43  // because the YAML parser creates configurations storing keys and values OrderedMapContent
44  // objects (preserving the order in which individual options were specified in the YAML file),
45  // but the LocalConfiguration::set() method stores keys and values in MapContent objects (with
46  // keys ordered alphabetically).
47 
48  ParametersType params;
49  params.deserialize(config);
50 
51  eckit::LocalConfiguration outputConfig;
52  params.serialize(outputConfig);
53 
54  std::stringstream expectedStream;
55  expectedStream << config;
56  const std::string expected = expectedStream.str();
57 
58  std::stringstream receivedStream;
59  receivedStream << outputConfig;
60  std::string received = receivedStream.str();
61 
62  EXPECT_EQUAL(received, expected);
63 }
64 
66  const eckit::LocalConfiguration conf(::test::TestEnvironment::config());
67 
68  MyParameters params;
69  EXPECT(params.optVariableParameter.value() == boost::none);
70 
71  const eckit::LocalConfiguration emptyConf(conf, "empty");
72  EXPECT_NO_THROW(params.validate(emptyConf));
73  params.deserialize(emptyConf);
74 
75  EXPECT(params.optVariableParameter.value() == boost::none);
76 }
77 
79  MyParameters params;
80  const eckit::LocalConfiguration fullConf(::test::TestEnvironment::config(), "full");
81  EXPECT_NO_THROW(params.validate(fullConf));
82  params.deserialize(fullConf);
83 
84  EXPECT(params.optVariableParameter.value() != boost::none);
85  EXPECT_EQUAL(params.optVariableParameter.value().get().group(), "MetaData");
86  EXPECT_EQUAL(params.optVariableParameter.value().get().variable(), "latitude");
87  EXPECT_EQUAL(params.optVariableParameter.value().get().channels(), (std::vector<int>{1, 5}));
88 }
89 
91  MyParameters params;
92  const eckit::LocalConfiguration fullConf(::test::TestEnvironment::config(), "simple_string");
93  EXPECT_NO_THROW(params.validate(fullConf));
94  params.deserialize(fullConf);
95 
96  EXPECT(params.optVariableParameter.value() != boost::none);
97  EXPECT_EQUAL(params.optVariableParameter.value().get().group(), "MetaData");
98  EXPECT_EQUAL(params.optVariableParameter.value().get().variable(), "latitude");
99 }
100 
102  MyParameters params;
103  const eckit::LocalConfiguration fullConf(::test::TestEnvironment::config(), "no_channels");
104  EXPECT_NO_THROW(params.validate(fullConf));
105  params.deserialize(fullConf);
106 
107  EXPECT(params.optVariableParameter.value() != boost::none);
108  EXPECT_EQUAL(params.optVariableParameter.value().get().group(), "MetaData");
109  EXPECT_EQUAL(params.optVariableParameter.value().get().variable(), "latitude");
110  EXPECT_EQUAL(params.optVariableParameter.value().get().channels(), (std::vector<int>{}));
111 }
112 
114  MyParameters params;
115  const eckit::LocalConfiguration fullConf(::test::TestEnvironment::config(), "complex_channels");
116  EXPECT_NO_THROW(params.validate(fullConf));
117  params.deserialize(fullConf);
118 
119  EXPECT(params.optVariableParameter.value() != boost::none);
120  EXPECT_EQUAL(params.optVariableParameter.value().get().group(), "MetaData");
121  EXPECT_EQUAL(params.optVariableParameter.value().get().variable(), "latitude");
122  EXPECT_EQUAL(params.optVariableParameter.value().get().channels(),
123  (std::vector<int>{1, 5, 10, 11, 12, 13, 14, 15}));
124 }
125 
127  MyParameters params;
128  const eckit::LocalConfiguration conf(::test::TestEnvironment::config(), "missing_name");
130  EXPECT_THROWS(params.validate(conf));
131  EXPECT_THROWS_MSG(params.deserialize(conf), "ConfigurationNotFound: [name]");
132 }
133 
135  MyParameters params;
136  const eckit::LocalConfiguration conf(::test::TestEnvironment::config(), "misspelled_property");
138  EXPECT_THROWS(params.validate(conf));
139 }
140 
142  MyParameters params;
143  const eckit::LocalConfiguration conf(::test::TestEnvironment::config(), "full");
144  doTestSerialization<MyParameters>(conf);
145 }
146 
148  MyParameters params;
149  const eckit::LocalConfiguration conf(::test::TestEnvironment::config(), "no_channels");
150  doTestSerialization<MyParameters>(conf);
151 }
152 
153 class Parameters : public oops::Test {
154  private:
155  std::string testid() const override {return "ufo::test::Parameters";}
156 
157  void register_tests() const override {
158  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
159 
160  ts.emplace_back(CASE("ufo/Parameters/defaultValue") {
162  });
163  ts.emplace_back(CASE("ufo/Parameters/correctValue") {
165  });
166  ts.emplace_back(CASE("ufo/Parameters/testSimpleString") {
168  });
169  ts.emplace_back(CASE("ufo/Parameters/noChannels") {
170  testNoChannels();
171  });
172  ts.emplace_back(CASE("ufo/Parameters/complexChannels") {
174  });
175  ts.emplace_back(CASE("ufo/Parameters/missingName") {
176  testMissingName();
177  });
178  ts.emplace_back(CASE("ufo/Parameters/misspelledProperty") {
180  });
181  ts.emplace_back(CASE("ufo/Parameters/serializationWithChannels") {
183  });
184  ts.emplace_back(CASE("ufo/Parameters/serializationWithoutChannels") {
186  });
187  }
188 
189  void clear() const override {}
190 };
191 
192 } // namespace test
193 } // namespace ufo
194 
195 #endif // TEST_UFO_PARAMETERS_H_
oops::OptionalParameter< ufo::Variable > optVariableParameter
Definition: Parameters.h:33
std::string testid() const override
Definition: Parameters.h:155
void clear() const override
Definition: Parameters.h:189
void register_tests() const override
Definition: Parameters.h:157
void testMisspelledProperty()
Definition: Parameters.h:134
void testMissingName()
Definition: Parameters.h:126
void testDefaultValue()
Definition: Parameters.h:65
void testNoChannels()
Definition: Parameters.h:101
void testCorrectValue()
Definition: Parameters.h:78
const bool validationSupported
Definition: Parameters.h:28
void testSimpleString()
Definition: Parameters.h:90
void testSerializationWithoutChannels()
Definition: Parameters.h:147
void testSerializationWithChannels()
Definition: Parameters.h:141
void testComplexChannels()
Definition: Parameters.h:113
CASE("ufo/DataExtractor/bilinearinterp/float_linear")
void doTestSerialization(const eckit::Configuration &config)
Definition: Parameters.h:37
Definition: RunCRTM.h:27