Leonetienne/Hazelnupp
Simple, easy to use, command line parameter interface
Conversion.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(_Conversion)
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  };
258 }
TestHazelnupp
Definition: Abbreviations.cpp:7
TestHazelnupp::TEST_CLASS
TEST_CLASS(_Conversion)
Definition: Conversion.cpp:10
C_Ify
#define C_Ify(vector)
Definition: helper.h:4
ArgList
std::vector< const char * > ArgList
Definition: helper.h:6
Hazelnupp
The main class to interface with.
Definition: Hazelnupp.h:9
helper.h