| Leonetienne/Hazelnupp Simple, easy to use, command line parameter interface

Functions
TestHazelnupp Namespace Reference

## Functions
| TEST_CLASS (_Abbreviations)
| TEST_CLASS (_Basics)
| TEST_CLASS (_Constraints)
| TEST_CLASS (_Conversion)

## Function Documentation

## ◆ TEST_CLASS() [1/4]

TestHazelnupp::TEST_CLASS | ( | _Abbreviations |  | ) |

Definition at line 9 of file Abbreviations.cpp.

10 {
11 public:
12
13 // Tests keys exist after parsing
14 TEST_METHOD(KeysExist)
15 {
16 // Setup
17 ArgList args({
18 "/my/fake/path/wahoo.out",
19 "-ms",
20 "billybob",
21 "-mv",
22 "-mf",
23 "-23.199",
24 "-mi",
25 "199",
26 "-mnl",
27 "1",
28 "2",
29 "3",
30 "4",
31 "-msl",
32 "apple",
33 "banana",
34 "pumpkin",
35 });
36
37 // Exercise
38 Hazelnupp nupp;
39 nupp.SetCrashOnFail(false);
40
41 nupp.RegisterAbbreviation("-ms", "--my_string");
42 nupp.RegisterAbbreviation("-mv", "--my_void");
43 nupp.RegisterAbbreviation("-mi", "--my_int");
44 nupp.RegisterAbbreviation("-mf", "--my_float");
45 nupp.RegisterAbbreviation("-mnl", "--my_num_list");
46 nupp.RegisterAbbreviation("-msl", "--my_str_list");
47
48 nupp.Parse(C_Ify(args));
49
50 // Verify
51 Assert::IsTrue(nupp.HasParam("--my_string"));
52 Assert::IsTrue(nupp.HasParam("--my_void"));
53 Assert::IsTrue(nupp.HasParam("--my_float"));
54 Assert::IsTrue(nupp.HasParam("--my_int"));
55 Assert::IsTrue(nupp.HasParam("--my_num_list"));
56 Assert::IsTrue(nupp.HasParam("--my_str_list"));
57
58 return;
59 }
60
61 // Tests keys are of the correct type after parsing
62 TEST_METHOD(CorrectType)
63 {
64 // Setup
65 ArgList args({
66 "/my/fake/path/wahoo.out",
67 "-ms",
68 "billybob",
69 "-mv",
70 "-mf",
71 "-23.199",
72 "-mi",
73 "199",
74 "-mnl",
75 "1",
76 "2",
77 "3",
78 "4",
79 "-msl",
80 "apple",
81 "banana",
82 "pumpkin",
83 });
84
85 // Exercise
86 Hazelnupp nupp;
87 nupp.SetCrashOnFail(false);
88
89 nupp.RegisterAbbreviation("-ms", "--my_string");
90 nupp.RegisterAbbreviation("-mv", "--my_void");
91 nupp.RegisterAbbreviation("-mi", "--my_int");
92 nupp.RegisterAbbreviation("-mf", "--my_float");
93 nupp.RegisterAbbreviation("-mnl", "--my_num_list");
94 nupp.RegisterAbbreviation("-msl", "--my_str_list");
95
96 nupp.Parse(C_Ify(args));
97
98 // Verify
99 Assert::IsTrue(nupp["--my_string"].GetDataType() == DATA_TYPE::STRING);
100 Assert::IsTrue(nupp["--my_void"].GetDataType() == DATA_TYPE::VOID);
101 Assert::IsTrue(nupp["--my_float"].GetDataType() == DATA_TYPE::FLOAT);
102 Assert::IsTrue(nupp["--my_int"].GetDataType() == DATA_TYPE::INT);
103 Assert::IsTrue(nupp["--my_num_list"].GetDataType() == DATA_TYPE::LIST);
104 Assert::IsTrue(nupp["--my_str_list"].GetDataType() == DATA_TYPE::LIST);
105
106 return;
107 }
108
109 // Tests keys have the correct value after parsing
110 TEST_METHOD(CorrectValues)
111 {
112 // Setup
113 ArgList args({
114 "/my/fake/path/wahoo.out",
115 "-ms",
116 "billybob",
117 "-mv",
118 "-mf",
119 "-23.199",
120 "-mi",
121 "199",
122 "-mnl",
123 "1",
124 "2",
125 "3",
126 "4",
127 "-msl",
128 "apple",
129 "banana",
130 "pumpkin",
131 });
132
133 // Exercise
134 Hazelnupp nupp;
135 nupp.SetCrashOnFail(false);
136
137 nupp.RegisterAbbreviation("-ms", "--my_string");
138 nupp.RegisterAbbreviation("-mv", "--my_void");
139 nupp.RegisterAbbreviation("-mi", "--my_int");
140 nupp.RegisterAbbreviation("-mf", "--my_float");
141 nupp.RegisterAbbreviation("-mnl", "--my_num_list");
142 nupp.RegisterAbbreviation("-msl", "--my_str_list");
143
144 nupp.Parse(C_Ify(args));
145
146 // Verify
147 Assert::AreEqual(nupp["--my_string"].GetString(), std::string("billybob"));
148 Assert::AreEqual(nupp["--my_float"].GetFloat32(), -23.199);
149 Assert::AreEqual(nupp["--my_int"].GetInt32(), 199);
150 Assert::AreEqual(nupp["--my_num_list"].GetList()[0]->GetInt32(), 1);
151 Assert::AreEqual(nupp["--my_num_list"].GetList()[1]->GetInt32(), 2);
152 Assert::AreEqual(nupp["--my_num_list"].GetList()[2]->GetInt32(), 3);
153 Assert::AreEqual(nupp["--my_num_list"].GetList()[3]->GetInt32(), 4);
154 Assert::AreEqual(nupp["--my_str_list"].GetList()[0]->GetString(), std::string("apple"));
155 Assert::AreEqual(nupp["--my_str_list"].GetList()[1]->GetString(), std::string("banana"));
156 Assert::AreEqual(nupp["--my_str_list"].GetList()[2]->GetString(), std::string("pumpkin"));
157
158 return;
159 }
160 };

## ◆ TEST_CLASS() [2/4]

TestHazelnupp::TEST_CLASS | ( | _Basics |  | ) |

Definition at line 10 of file Basics.cpp.

11 {
12 public:
13
14 // Tests the application path gets exported correctly
15 TEST_METHOD(ApplicationPathWorks)
16 {
17 // Setup
18 ArgList args({
19 "/my/fake/path/wahoo.out"
20 });
21
22 // Exercise
23 Hazelnupp nupp(C_Ify(args));
24 nupp.SetCrashOnFail(false);
25
26 // Verify
27 Assert::AreEqual(std::string("/my/fake/path/wahoo.out"), nupp.GetExecutableName());
28
29 return;
30 }
31
32 // Edgecase test: We only have one param.
33 TEST_METHOD(Only_One_Param)
34 {
35 // Setup
36 ArgList args({
37 "/my/fake/path/wahoo.out",
38 "--dummy"
39 });
40
41 // Exercise
42 Hazelnupp nupp(C_Ify(args));
43 nupp.SetCrashOnFail(false);
44
45 // Verify
46 Assert::IsTrue(nupp.HasParam("--dummy"));
47
48 return;
49 }
50
51 // Edgecase test: We begin with an actual value, instead of an argument.
52 TEST_METHOD(Weird_Case_1)
53 {
54 // Setup
55 ArgList args({
56 "/my/fake/path/wahoo.out",
57 "dummy"
58 });
59
60 // Exercise
61 Hazelnupp nupp(C_Ify(args));
62 nupp.SetCrashOnFail(false);
63
64 // Verify (no exception)
65
66 return;
67 }
68
69 // Edgecase test: We begin with first an actual value, and then an argument.
70 TEST_METHOD(Weird_Case_2)
71 {
72 // Setup
73 ArgList args({
74 "/my/fake/path/wahoo.out",
75 "dummy",
76 "--dummy"
77 });
78
79 // Exercise
80 Hazelnupp nupp(C_Ify(args));
81 nupp.SetCrashOnFail(false);
82
83 // Verify
84 Assert::IsTrue(nupp.HasParam("--dummy"), L"Failed has-param");
85 Assert::IsTrue(nupp["--dummy"].GetDataType() == DATA_TYPE::VOID, L"Failed type");
86
87 return;
88 }
89
90 // Tests keys exist after parsing
91 TEST_METHOD(KeysExist)
92 {
93 // Setup
94 ArgList args({
95 "/my/fake/path/wahoo.out",
96 "--my_string",
97 "billybob",
98 "--my_void",
99 "--my_float",
100 "-23.199",
101 "--my_int",
102 "199",
103 "--my_num_list",
104 "1",
105 "2",
106 "3",
107 "4",
108 "--my_str_list",
109 "apple",
110 "banana",
111 "pumpkin",
112 });
113
114 // Exercise
115 Hazelnupp nupp(C_Ify(args));
116 nupp.SetCrashOnFail(false);
117
118 // Verify
119 Assert::IsTrue(nupp.HasParam("--my_string"));
120 Assert::IsTrue(nupp.HasParam("--my_void"));
121 Assert::IsTrue(nupp.HasParam("--my_float"));
122 Assert::IsTrue(nupp.HasParam("--my_int"));
123 Assert::IsTrue(nupp.HasParam("--my_num_list"));
124 Assert::IsTrue(nupp.HasParam("--my_str_list"));
125
126 return;
127 }
128
129 // Tests keys are of the correct type after parsing
130 TEST_METHOD(CorrectType)
131 {
132 // Setup
133 ArgList args({
134 "/my/fake/path/wahoo.out",
135 "--my_string",
136 "billybob",
137 "--my_void",
138 "--my_float",
139 "-23.199",
140 "--my_int",
141 "199",
142 "--my_num_list",
143 "1",
144 "2",
145 "3",
146 "4",
147 "--my_str_list",
148 "apple",
149 "banana",
150 "pumpkin",
151 });
152
153 // Exercise
154 Hazelnupp nupp(C_Ify(args));
155 nupp.SetCrashOnFail(false);
156
157 // Verify
158 Assert::IsTrue(nupp["--my_string"].GetDataType() == DATA_TYPE::STRING);
159 Assert::IsTrue(nupp["--my_void"].GetDataType() == DATA_TYPE::VOID);
160 Assert::IsTrue(nupp["--my_float"].GetDataType() == DATA_TYPE::FLOAT);
161 Assert::IsTrue(nupp["--my_int"].GetDataType() == DATA_TYPE::INT);
162 Assert::IsTrue(nupp["--my_num_list"].GetDataType() == DATA_TYPE::LIST);
163 Assert::IsTrue(nupp["--my_str_list"].GetDataType() == DATA_TYPE::LIST);
164
165 return;
166 }
167
168 // Tests keys have the correct value after parsing
169 TEST_METHOD(CorrectValues)
170 {
171 // Setup
172 ArgList args({
173 "/my/fake/path/wahoo.out",
174 "--my_string",
175 "billybob",
176 "--my_void",
177 "--my_float",
178 "-23.199",
179 "--my_int",
180 "199",
181 "--my_num_list",
182 "1",
183 "2",
184 "3",
185 "4",
186 "--my_str_list",
187 "apple",
188 "banana",
189 "pumpkin",
190 });
191
192 // Exercise
193 Hazelnupp nupp(C_Ify(args));
194 nupp.SetCrashOnFail(false);
195
196 // Verify
197 Assert::AreEqual(nupp["--my_string"].GetString(), std::string("billybob"));
198 Assert::AreEqual(nupp["--my_float"].GetFloat32(), -23.199);
199 Assert::AreEqual(nupp["--my_int"].GetInt32(), 199);
200 Assert::AreEqual(nupp["--my_num_list"].GetList()[0]->GetInt32(), 1);
201 Assert::AreEqual(nupp["--my_num_list"].GetList()[1]->GetInt32(), 2);
202 Assert::AreEqual(nupp["--my_num_list"].GetList()[2]->GetInt32(), 3);
203 Assert::AreEqual(nupp["--my_num_list"].GetList()[3]->GetInt32(), 4);
204 Assert::AreEqual(nupp["--my_str_list"].GetList()[0]->GetString(), std::string("apple"));
205 Assert::AreEqual(nupp["--my_str_list"].GetList()[1]->GetString(), std::string("banana"));
206 Assert::AreEqual(nupp["--my_str_list"].GetList()[2]->GetString(), std::string("pumpkin"));
207
208 return;
209 }
210
211 // Tests that an HazelnuppInvalidKeyException gets raised, if an invalid gey is tried to access
212 TEST_METHOD(Exception_On_Invalid_Key)
213 {
214 // Setup
215 ArgList args({
216 "/my/fake/path/wahoo.out",
217 "--my_string",
218 "billybob",
219 "--my_void",
220 "--my_float",
221 "-23.199",
222 "--my_int",
223 "199",
224 "--my_num_list",
225 "1",
226 "2",
227 "3",
228 "4",
229 "--my_str_list",
230 "apple",
231 "banana",
232 "pumpkin",
233 });
234
235 Hazelnupp nupp(C_Ify(args));
236 nupp.SetCrashOnFail(false);
237
238 // Exercise, Verify
239 Assert::ExpectException<HazelnuppInvalidKeyException>(
240 [args]
241 {
242 Hazelnupp nupp(C_Ify(args));
243 nupp["--borrnana"];
244 }
245 );
246
247 return;
248 }
249 };

## ◆ TEST_CLASS() [3/4]

TestHazelnupp::TEST_CLASS | ( | _Constraints |  | ) |

Definition at line 10 of file Constraints.cpp.

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({
139 ParamConstraint::TypeSafety("--num-apples", DATA_TYPE::INT),
140 ParamConstraint::TypeSafety("--table-height", DATA_TYPE::FLOAT),
141 ParamConstraint::TypeSafety("--license-plate", DATA_TYPE::STRING),
142 ParamConstraint::TypeSafety("--fav-fruits", DATA_TYPE::LIST),
143 ParamConstraint::TypeSafety("--indices", DATA_TYPE::LIST),
144 ParamConstraint::TypeSafety("--force", DATA_TYPE::VOID),
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({
238 ParamConstraint::TypeSafety("--elenor-int", DATA_TYPE::INT),
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({
271 ParamConstraint::TypeSafety("--void1", DATA_TYPE::VOID),
272 ParamConstraint::TypeSafety("--void2", DATA_TYPE::VOID),
273 ParamConstraint::TypeSafety("--void3", DATA_TYPE::VOID),
274 ParamConstraint::TypeSafety("--void4", DATA_TYPE::VOID),
275 ParamConstraint::TypeSafety("--void5", DATA_TYPE::VOID)
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({
306 ParamConstraint::TypeSafety("--empty-list", DATA_TYPE::LIST),
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 };

## ◆ TEST_CLASS() [4/4]

TestHazelnupp::TEST_CLASS | ( | _Conversion |  | ) |

Definition at line 10 of file Conversion.cpp.

11 {
12 public:
13
14 // Tests that string conversion methods work
15 TEST_METHOD(Convert_String)
16 {
17 // Setup
18 ArgList args({
19 "/my/fake/path/wahoo.out",
20 "--pud",
21 "hello"
22 });
23
24 // Exercise
25 Hazelnupp nupp(C_Ify(args));
26 nupp.SetCrashOnFail(false);
27
28 // Verify
29 const Hazelnupp* ptnupp = &nupp;
30
31 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
32 [ptnupp]
33 {
34 (*ptnupp)["--pud"].GetInt64();
35 }
36 , L"Int64");
37
38 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
39 [ptnupp]
40 {
41 (*ptnupp)["--pud"].GetInt32();
42 }
43 , L"Int32");
44
45 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
46 [ptnupp]
47 {
48 (*ptnupp)["--pud"].GetFloat64();
49 }
50 , L"Float64");
51
52 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
53 [ptnupp]
54 {
55 (*ptnupp)["--pud"].GetFloat32();
56 }
57 , L"Float32");
58
59 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
60 [ptnupp]
61 {
62 (*ptnupp)["--pud"].GetList();
63 }
64 , L"List");
65
66
67 return;
68 }
69
70 // Tests that void conversion methods work
71 TEST_METHOD(Convert_Void)
72 {
73 // Setup
74 ArgList args({
75 "/my/fake/path/wahoo.out",
76 "--pud"
77 });
78
79 // Exercise
80 Hazelnupp nupp(C_Ify(args));
81 nupp.SetCrashOnFail(false);
82
83 // Verify
84 const Hazelnupp* ptnupp = &nupp;
85
86 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
87 [ptnupp]
88 {
89 (*ptnupp)["--pud"].GetInt64();
90 }
91 , L"Int64");
92
93 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
94 [ptnupp]
95 {
96 (*ptnupp)["--pud"].GetInt32();
97 }
98 , L"Int32");
99
100 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
101 [ptnupp]
102 {
103 (*ptnupp)["--pud"].GetFloat64();
104 }
105 , L"Float64");
106
107 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
108 [ptnupp]
109 {
110 (*ptnupp)["--pud"].GetFloat32();
111 }
112 , L"Float32");
113
114 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
115 [ptnupp]
116 {
117 (*ptnupp)["--pud"].GetString();
118 }
119 , L"String");
120
121 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
122 [ptnupp]
123 {
124 (*ptnupp)["--pud"].GetList();
125 }
126 , L"List");
127
128
129 return;
130 }
131
132 // Tests that int conversion methods work
133 TEST_METHOD(Convert_Int)
134 {
135 // Setup
136 ArgList args({
137 "/my/fake/path/wahoo.out",
138 "--pud",
139 "39"
140 });
141
142 // Exercise
143 Hazelnupp nupp(C_Ify(args));
144 nupp.SetCrashOnFail(false);
145
146 // Verify
147 const Hazelnupp* ptnupp = &nupp;
148
149 Assert::AreEqual(39ll, nupp["--pud"].GetInt64(), L"Int64");
150 Assert::AreEqual(39, nupp["--pud"].GetInt32(), L"Int32");
151 Assert::IsTrue(39.0l == nupp["--pud"].GetFloat64(), L"Float64");
152 Assert::AreEqual(39.0, nupp["--pud"].GetFloat32(), L"Float32");
153 Assert::AreEqual(std::string("39"), nupp["--pud"].GetString(), L"String");
154
155 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
156 [ptnupp]
157 {
158 (*ptnupp)["--pud"].GetList();
159 }
160 , L"List");
161
162
163 return;
164 }
165
166 // Tests that float conversion methods work
167 TEST_METHOD(Convert_Float)
168 {
169 // Setup
170 ArgList args({
171 "/my/fake/path/wahoo.out",
172 "--pud",
173 "39.5"
174 });
175
176 // Exercise
177 Hazelnupp nupp(C_Ify(args));
178 nupp.SetCrashOnFail(false);
179
180 // Verify
181 const Hazelnupp* ptnupp = &nupp;
182
183 Assert::AreEqual(39ll, nupp["--pud"].GetInt64(), L"Int64");
184 Assert::AreEqual(39, nupp["--pud"].GetInt32(), L"Int32");
185 Assert::IsTrue(39.5l == nupp["--pud"].GetFloat64(), L"Float64");
186 Assert::AreEqual(39.5, nupp["--pud"].GetFloat32(), L"Float32");
187 Assert::AreEqual(std::string("39.5"), nupp["--pud"].GetString(), L"String");
188
189 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
190 [ptnupp]
191 {
192 (*ptnupp)["--pud"].GetList();
193 }
194 , L"List");
195
196
197 return;
198 }
199
200 // Tests that list conversion methods work
201 TEST_METHOD(Convert_List)
202 {
203 // Setup
204 ArgList args({
205 "/my/fake/path/wahoo.out",
206 "--pud",
207 "apple",
208 "1",
209 "2",
210 "3"
211 });
212
213 // Exercise
214 Hazelnupp nupp(C_Ify(args));
215 nupp.SetCrashOnFail(false);
216
217 // Verify
218 const Hazelnupp* ptnupp = &nupp;
219
220 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
221 [ptnupp]
222 {
223 (*ptnupp)["--pud"].GetInt64();
224 }
225 , L"Int64");
226
227 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
228 [ptnupp]
229 {
230 (*ptnupp)["--pud"].GetInt32();
231 }
232 , L"Int32");
233
234 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
235 [ptnupp]
236 {
237 (*ptnupp)["--pud"].GetFloat64();
238 }
239 , L"Float64");
240
241 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
242 [ptnupp]
243 {
244 (*ptnupp)["--pud"].GetFloat32();
245 }
246 , L"Float32");
247
248 Assert::ExpectException<HazelnuppValueNotConvertibleException>(
249 [ptnupp]
250 {
251 (*ptnupp)["--pud"].GetString();
252 }
253 , L"String");
254
255 return;
256 }
257 };
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
ParamConstraint::TypeSafety
static ParamConstraint TypeSafety(const std::string &key, DATA_TYPE wantedType, bool constrainType=true)
Constructs a type-safety constraint.
Definition: ParamConstraint.h:25
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
Hazelnupp::Parse
void Parse(const int argc, const char *const *argv)
Will parse command line arguments.
Definition: Hazelnupp.cpp:33