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

Basics.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(_Basics)
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 };
250 }
DATA_TYPE::VOID
@ VOID
DATA_TYPE::LIST
@ LIST
DATA_TYPE::FLOAT
@ FLOAT
TestHazelnupp
Definition: Abbreviations.cpp:7
TestHazelnupp::TEST_CLASS
TEST_CLASS(_Basics)
Definition: Basics.cpp:10
DATA_TYPE::INT
@ INT
C_Ify
#define C_Ify(vector)
Definition: helper.h:4
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