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

Abbreviations.cpp
Go to the documentation of this file.
1 #include "CppUnitTest.h"
2 #include "helper.h"
3 #include "../Hazelnupp/Hazelnupp.h"
4
5 using namespace Microsoft::VisualStudio::CppUnitTestFramework;
6
7 namespace TestHazelnupp
8 {
9 TEST_CLASS(_Abbreviations)
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 };
161 }
DATA_TYPE::VOID
@ VOID
DATA_TYPE::LIST
@ LIST
DATA_TYPE::FLOAT
@ FLOAT
TestHazelnupp
Definition: Abbreviations.cpp:7
TestHazelnupp::TEST_CLASS
TEST_CLASS(_Abbreviations)
Definition: Abbreviations.cpp:9
DATA_TYPE::INT
@ INT
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