Leonetienne/Hazelnupp
Simple, easy to use, command line parameter interface
Constraints.cpp
Go to the documentation of this file.
1 #include "CppUnitTest.h"
2 #include "helper.h"
3 #include "../Hazelnupp/Hazelnupp.h"
4 #include "../Hazelnupp/HazelnuppException.h"
5 
6 using namespace Microsoft::VisualStudio::CppUnitTestFramework;
7 
8 namespace TestHazelnupp
9 {
10  TEST_CLASS(_Constraints)
11  {
12  public:
13 
14  // Tests that default values get added
15  TEST_METHOD(DefaultValues_GetAdded)
16  {
17  // Setup
18  ArgList args({
19  "/my/fake/path/wahoo.out",
20  "--dummy",
21  "123"
22  });
23 
24  // Exercise
25  Hazelnupp nupp;
26  nupp.SetCrashOnFail(false);
27 
28  nupp.RegisterConstraints({
29  ParamConstraint::Require("--elenor-int", {"5994"}),
30  ParamConstraint::Require("--federich-float", {"420.69"}),
31  ParamConstraint::Require("--siegbert-string", {"banana"}),
32  ParamConstraint::Require("--lieber-liste", {"banana", "apple", "59"})
33  });
34 
35  nupp.Parse(C_Ify(args));
36 
37  // Verify
38  Assert::IsTrue(nupp.HasParam("--elenor-int"));
39  Assert::IsTrue(nupp["--elenor-int"].GetDataType() == DATA_TYPE::INT);
40  Assert::AreEqual(nupp["--elenor-int"].GetInt32(), 5994);
41 
42  Assert::IsTrue(nupp.HasParam("--federich-float"));
43  Assert::IsTrue(nupp["--federich-float"].GetDataType() == DATA_TYPE::FLOAT);
44  Assert::AreEqual(nupp["--federich-float"].GetFloat32(), 420.69);
45 
46  Assert::IsTrue(nupp.HasParam("--siegbert-string"));
47  Assert::IsTrue(nupp["--siegbert-string"].GetDataType() == DATA_TYPE::STRING);
48  Assert::AreEqual(nupp["--siegbert-string"].GetString(), std::string("banana"));
49 
50  Assert::IsTrue(nupp.HasParam("--lieber-liste"));
51  Assert::IsTrue(nupp["--lieber-liste"].GetDataType() == DATA_TYPE::LIST);
52  Assert::AreEqual(nupp["--lieber-liste"].GetList()[0]->GetString(), std::string("banana"));
53  Assert::AreEqual(nupp["--lieber-liste"].GetList()[1]->GetString(), std::string("apple"));
54  Assert::AreEqual(nupp["--lieber-liste"].GetList()[2]->GetInt32(), 59);
55 
56  return;
57  }
58 
59  // Tests that the default values do not override actually set values
60  TEST_METHOD(DefaultValues_DefaultDoesntOverride)
61  {
62  // Setup
63  ArgList args({
64  "/my/fake/path/wahoo.out",
65  "--dummy",
66  "--elenor-int",
67  "5994",
68  "--federich-float",
69  "420.69",
70  "--siegbert-string",
71  "banana",
72  "--lieber-liste",
73  "banana",
74  "apple",
75  "59"
76  });
77 
78  // Exercise
79  Hazelnupp nupp;
80  nupp.SetCrashOnFail(false);
81 
82  nupp.RegisterConstraints({
83  ParamConstraint::Require("--elenor-int", {"6871"}),
84  ParamConstraint::Require("--federich-float", {"-199.44"}),
85  ParamConstraint::Require("--siegbert-string", {"bornana"}),
86  ParamConstraint::Require("--lieber-liste", {"bornana", "ollpe", "5"})
87  });
88 
89  nupp.Parse(C_Ify(args));
90 
91  // Verify
92  Assert::IsTrue(nupp.HasParam("--elenor-int"));
93  Assert::IsTrue(nupp["--elenor-int"].GetDataType() == DATA_TYPE::INT);
94  Assert::AreEqual(nupp["--elenor-int"].GetInt32(), 5994);
95 
96  Assert::IsTrue(nupp.HasParam("--federich-float"));
97  Assert::IsTrue(nupp["--federich-float"].GetDataType() == DATA_TYPE::FLOAT);
98  Assert::AreEqual(nupp["--federich-float"].GetFloat32(), 420.69);
99 
100  Assert::IsTrue(nupp.HasParam("--siegbert-string"));
101  Assert::IsTrue(nupp["--siegbert-string"].GetDataType() == DATA_TYPE::STRING);
102  Assert::AreEqual(nupp["--siegbert-string"].GetString(), std::string("banana"));
103 
104  Assert::IsTrue(nupp.HasParam("--lieber-liste"));
105  Assert::IsTrue(nupp["--lieber-liste"].GetDataType() == DATA_TYPE::LIST);
106  Assert::AreEqual(nupp["--lieber-liste"].GetList()[0]->GetString(), std::string("banana"));
107  Assert::AreEqual(nupp["--lieber-liste"].GetList()[1]->GetString(), std::string("apple"));
108  Assert::AreEqual(nupp["--lieber-liste"].GetList()[2]->GetInt32(), 59);
109 
110  return;
111  }
112 
113  // Tests that data types get forced according to the constraints
114  TEST_METHOD(ForceTypes)
115  {
116  // Setup
117  ArgList args({
118  "/my/fake/path/wahoo.out",
119  "--dummy",
120  "--num-apples",
121  "39.75",
122  "--table-height",
123  "400",
124  "--license-plate",
125  "193273",
126  "--fav-fruits",
127  "apple",
128  "--indices",
129  "9",
130  "--force",
131  "plsdontuseme"
132  });
133 
134  // Exercise
135  Hazelnupp nupp;
136  nupp.SetCrashOnFail(false);
137 
138  nupp.RegisterConstraints({
145  });
146 
147  nupp.Parse(C_Ify(args));
148 
149  // Verify
150  Assert::IsTrue(nupp.HasParam("--num-apples"));
151  Assert::IsTrue(nupp["--num-apples"].GetDataType() == DATA_TYPE::INT);
152  Assert::AreEqual(nupp["--num-apples"].GetInt32(), 39);
153 
154  Assert::IsTrue(nupp.HasParam("--table-height"));
155  Assert::IsTrue(nupp["--table-height"].GetDataType() == DATA_TYPE::FLOAT);
156  Assert::AreEqual(nupp["--table-height"].GetFloat32(), 400.0);
157 
158  Assert::IsTrue(nupp.HasParam("--license-plate"));
159  Assert::IsTrue(nupp["--license-plate"].GetDataType() == DATA_TYPE::STRING);
160  Assert::AreEqual(nupp["--license-plate"].GetString(), std::string("193273"));
161 
162  Assert::IsTrue(nupp.HasParam("--fav-fruits"));
163  Assert::IsTrue(nupp["--fav-fruits"].GetDataType() == DATA_TYPE::LIST);
164  Assert::AreEqual(nupp["--fav-fruits"].GetList()[0]->GetString(), std::string("apple"));
165 
166  Assert::IsTrue(nupp.HasParam("--indices"));
167  Assert::IsTrue(nupp["--indices"].GetDataType() == DATA_TYPE::LIST);
168  Assert::AreEqual(nupp["--indices"].GetList()[0]->GetInt32(), 9);
169 
170  Assert::IsTrue(nupp.HasParam("--force"));
171  Assert::IsTrue(nupp["--force"].GetDataType() == DATA_TYPE::VOID);
172 
173  return;
174  }
175 
176  // Tests that an HazelnuppConstraintMissingValue gets raised if a required parameter
177  // is missing and does not have a default parameter
178  TEST_METHOD(Exception_MissingImportant_Parameter_WithoutDefault)
179  {
180  // Setup
181  ArgList args({
182  "/my/fake/path/wahoo.out",
183  "--dummy",
184  "--federich-float",
185  "420.69",
186  "--siegbert-string",
187  "banana",
188  "--lieber-liste",
189  "banana",
190  "apple",
191  "59"
192  });
193 
194  Assert::ExpectException<HazelnuppConstraintMissingValue>(
195  [args]
196  {
197  Hazelnupp nupp;
198  nupp.SetCrashOnFail(false);
199 
200  nupp.RegisterConstraints({
201  ParamConstraint::Require("--elenor-int"),
202  });
203 
204  nupp.Parse(C_Ify(args));
205  }
206  );
207 
208  return;
209  }
210 
211  // Tests that an HazelnuppConstraintTypeMissmatch gets raised if a required parameter
212  // is missing of the wrong type and cannot be converted
213  TEST_METHOD(Exception_TypeMismatch_Parameter_NotConvertable)
214  {
215  // Setup
216  ArgList args({
217  "/my/fake/path/wahoo.out",
218  "--dummy",
219  "--elenor-int",
220  "hello"
221  "--federich-float",
222  "420.69",
223  "--siegbert-string",
224  "banana",
225  "--lieber-liste",
226  "banana",
227  "apple",
228  "59"
229  });
230 
231  Assert::ExpectException<HazelnuppConstraintTypeMissmatch>(
232  [args]
233  {
234  Hazelnupp nupp;
235  nupp.SetCrashOnFail(false);
236 
237  nupp.RegisterConstraints({
239  });
240 
241  nupp.Parse(C_Ify(args));
242  }
243  );
244 
245  return;
246  }
247 
248  // Tests that everything can be converted to void
249  TEST_METHOD(Weird_Load_Conversions_ToVoid)
250  {
251  // Setup
252  ArgList args({
253  "/my/fake/path/wahoo.out",
254  "--dummy",
255  "--void1",
256  "--void2",
257  "12",
258  "--void3",
259  "9.5",
260  "--void4",
261  "hello",
262  "--void5",
263  "foo",
264  "baz"
265  });
266 
267  Hazelnupp nupp;
268  nupp.SetCrashOnFail(false);
269 
270  nupp.RegisterConstraints({
276  });
277 
278 
279  // Exercise
280  nupp.Parse(C_Ify(args));
281 
282  // Verify
283  Assert::IsTrue(nupp["--void1"].GetDataType() == DATA_TYPE::VOID);
284  Assert::IsTrue(nupp["--void2"].GetDataType() == DATA_TYPE::VOID);
285  Assert::IsTrue(nupp["--void3"].GetDataType() == DATA_TYPE::VOID);
286  Assert::IsTrue(nupp["--void4"].GetDataType() == DATA_TYPE::VOID);
287  Assert::IsTrue(nupp["--void5"].GetDataType() == DATA_TYPE::VOID);
288 
289  return;
290  }
291 
292  // Tests that everything a void can be converted to an empty list
293  TEST_METHOD(Weird_Load_Conversions_VoidToEmptyList)
294  {
295  // Setup
296  ArgList args({
297  "/my/fake/path/wahoo.out",
298  "--dummy",
299  "--empty-list",
300  });
301 
302  Hazelnupp nupp;
303  nupp.SetCrashOnFail(false);
304 
305  nupp.RegisterConstraints({
307  });
308 
309 
310  // Exercise
311  nupp.Parse(C_Ify(args));
312 
313  // Verify
314  Assert::IsTrue(nupp["--empty-list"].GetDataType() == DATA_TYPE::LIST);
315  Assert::AreEqual(std::size_t(0), nupp["--empty-list"].GetList().size());
316 
317  return;
318  }
319  };
320 }
DATA_TYPE::VOID
@ VOID
Hazelnupp::RegisterConstraints
void RegisterConstraints(const std::vector< ParamConstraint > &constraints)
Will register parameter constraints.
Definition: Hazelnupp.cpp:352
DATA_TYPE::LIST
@ LIST
DATA_TYPE::FLOAT
@ FLOAT
TestHazelnupp
Definition: Abbreviations.cpp:7
ParamConstraint::TypeSafety
static ParamConstraint TypeSafety(const std::string &key, DATA_TYPE wantedType, bool constrainType=true)
Constructs a type-safety constraint.
Definition: ParamConstraint.h:25
TestHazelnupp::TEST_CLASS
TEST_CLASS(_Constraints)
Definition: Constraints.cpp:10
DATA_TYPE::INT
@ INT
ParamConstraint::Require
static ParamConstraint Require(const std::string &key, const std::vector< std::string > &defaultValue={}, bool required=true)
Constructs a require constraint.
Definition: ParamConstraint.h:14
C_Ify
#define C_Ify(vector)
Definition: helper.h:4
Hazelnupp::SetCrashOnFail
void SetCrashOnFail(bool crashOnFail)
Sets whether to crash the application, and print to stderr, when an exception is raised whilst parsin...
Definition: Hazelnupp.cpp:379
ArgList
std::vector< const char * > ArgList
Definition: helper.h:6
DATA_TYPE::STRING
@ STRING
Hazelnupp
The main class to interface with.
Definition: Hazelnupp.h:9
helper.h
Hazelnupp::Parse
void Parse(const int argc, const char *const *argv)
Will parse command line arguments.
Definition: Hazelnupp.cpp:33