blob: 71409f7ae46dcad25109e8f5781074d94b4a1179 [file] [log] [blame]
James Feist481c5d52019-08-13 14:40:40 -07001#include "Utils.hpp"
2
3#include <boost/container/flat_map.hpp>
4#include <nlohmann/json.hpp>
James Feist8c505da2020-05-28 10:06:33 -07005
James Feist481c5d52019-08-13 14:40:40 -07006#include <variant>
7
8#include "gtest/gtest.h"
9
10TEST(TemplateCharReplace, replaceOneInt)
11{
12 nlohmann::json j = {{"foo", "$bus"}};
13 auto it = j.begin();
14 boost::container::flat_map<std::string, BasicVariantType> data;
15 data["BUS"] = 23;
16
17 templateCharReplace(it, data, 0);
18
19 nlohmann::json expected = 23;
20 EXPECT_EQ(expected, j["foo"]);
21}
22
23TEST(TemplateCharReplace, replaceOneStr)
24{
25 nlohmann::json j = {{"foo", "$TEST"}};
26 auto it = j.begin();
27 boost::container::flat_map<std::string, BasicVariantType> data;
28 data["TEST"] = std::string("Test");
29
30 templateCharReplace(it, data, 0);
31
32 nlohmann::json expected = "Test";
33 EXPECT_EQ(expected, j["foo"]);
34}
35
36TEST(TemplateCharReplace, replaceSecondStr)
37{
38 nlohmann::json j = {{"foo", "the $TEST"}};
39 auto it = j.begin();
40 boost::container::flat_map<std::string, BasicVariantType> data;
41 data["TEST"] = std::string("Test");
42
43 templateCharReplace(it, data, 0);
44
45 nlohmann::json expected = "the Test";
46 EXPECT_EQ(expected, j["foo"]);
47}
48
James Feist481c5d52019-08-13 14:40:40 -070049TEST(TemplateCharReplace, replaceMiddleStr)
50{
51 nlohmann::json j = {{"foo", "the $TEST worked"}};
52 auto it = j.begin();
53 boost::container::flat_map<std::string, BasicVariantType> data;
54 data["TEST"] = std::string("Test");
55
56 templateCharReplace(it, data, 0);
57
58 nlohmann::json expected = "the Test worked";
59 EXPECT_EQ(expected, j["foo"]);
60}
James Feist481c5d52019-08-13 14:40:40 -070061
62TEST(TemplateCharReplace, replaceLastStr)
63{
64 nlohmann::json j = {{"foo", "the Test $TEST"}};
65 auto it = j.begin();
66 boost::container::flat_map<std::string, BasicVariantType> data;
67 data["TEST"] = 23;
68
69 templateCharReplace(it, data, 0);
70
71 nlohmann::json expected = "the Test 23";
72 EXPECT_EQ(expected, j["foo"]);
73}
74
75TEST(TemplateCharReplace, increment)
76{
77 nlohmann::json j = {{"foo", "3 plus 1 equals $TEST + 1"}};
78 auto it = j.begin();
79 boost::container::flat_map<std::string, BasicVariantType> data;
80 data["TEST"] = 3;
81
82 templateCharReplace(it, data, 0);
83
84 nlohmann::json expected = "3 plus 1 equals 4";
85 EXPECT_EQ(expected, j["foo"]);
86}
87
88TEST(TemplateCharReplace, decrement)
89{
90 nlohmann::json j = {{"foo", "3 minus 1 equals $TEST - 1 !"}};
91 auto it = j.begin();
92 boost::container::flat_map<std::string, BasicVariantType> data;
93 data["TEST"] = 3;
94
95 templateCharReplace(it, data, 0);
96
97 nlohmann::json expected = "3 minus 1 equals 2 !";
98 EXPECT_EQ(expected, j["foo"]);
99}
100
101TEST(TemplateCharReplace, modulus)
102{
103 nlohmann::json j = {{"foo", "3 mod 2 equals $TEST % 2"}};
104 auto it = j.begin();
105 boost::container::flat_map<std::string, BasicVariantType> data;
106 data["TEST"] = 3;
107
108 templateCharReplace(it, data, 0);
109
110 nlohmann::json expected = "3 mod 2 equals 1";
111 EXPECT_EQ(expected, j["foo"]);
112}
113
114TEST(TemplateCharReplace, multiply)
115{
116 nlohmann::json j = {{"foo", "3 * 2 equals $TEST * 2"}};
117 auto it = j.begin();
118 boost::container::flat_map<std::string, BasicVariantType> data;
119 data["TEST"] = 3;
120
121 templateCharReplace(it, data, 0);
122
123 nlohmann::json expected = "3 * 2 equals 6";
124 EXPECT_EQ(expected, j["foo"]);
125}
126
127TEST(TemplateCharReplace, divide)
128{
129 nlohmann::json j = {{"foo", "4 / 2 equals $TEST / 2"}};
130 auto it = j.begin();
131 boost::container::flat_map<std::string, BasicVariantType> data;
132 data["TEST"] = 4;
133
134 templateCharReplace(it, data, 0);
135
136 nlohmann::json expected = "4 / 2 equals 2";
137 EXPECT_EQ(expected, j["foo"]);
James Feist8c20feb2019-08-14 15:10:11 -0700138}
139
140TEST(TemplateCharReplace, multiMath)
141{
142 nlohmann::json j = {{"foo", "4 * 2 % 6 equals $TEST * 2 % 6"}};
143 auto it = j.begin();
144 boost::container::flat_map<std::string, BasicVariantType> data;
145 data["TEST"] = 4;
146
147 templateCharReplace(it, data, 0);
148
149 nlohmann::json expected = "4 * 2 % 6 equals 2";
150 EXPECT_EQ(expected, j["foo"]);
151}
152
153TEST(TemplateCharReplace, twoReplacements)
154{
155 nlohmann::json j = {{"foo", "$FOO $BAR"}};
156 auto it = j.begin();
157 boost::container::flat_map<std::string, BasicVariantType> data;
158 data["FOO"] = std::string("foo");
159 data["BAR"] = std::string("bar");
160
161 templateCharReplace(it, data, 0);
162
163 nlohmann::json expected = "foo bar";
164 EXPECT_EQ(expected, j["foo"]);
165}
166
167TEST(TemplateCharReplace, twoReplacementsWithMath)
168{
169 nlohmann::json j = {{"foo", "4 / 2 equals $TEST / 2 $BAR"}};
170 auto it = j.begin();
171 boost::container::flat_map<std::string, BasicVariantType> data;
172 data["TEST"] = 4;
173 data["BAR"] = std::string("bar");
174
175 templateCharReplace(it, data, 0);
176
177 nlohmann::json expected = "4 / 2 equals 2 bar";
178 EXPECT_EQ(expected, j["foo"]);
James Feistb0097d42019-08-15 09:24:13 -0700179}
180
181TEST(TemplateCharReplace, hexAndWrongCase)
182{
183 nlohmann::json j = {{"Address", "0x54"},
184 {"Bus", 15},
185 {"Name", "$bus sensor 0"},
186 {"Type", "SomeType"}};
187
188 boost::container::flat_map<std::string, BasicVariantType> data;
189 data["BUS"] = 15;
190
191 for (auto it = j.begin(); it != j.end(); it++)
192 {
193 templateCharReplace(it, data, 0);
194 }
195 nlohmann::json expected = {{"Address", 84},
196 {"Bus", 15},
197 {"Name", "15 sensor 0"},
198 {"Type", "SomeType"}};
199 EXPECT_EQ(expected, j);
200}
201
202TEST(TemplateCharReplace, replaceSecondAsInt)
203{
204 nlohmann::json j = {{"foo", "twelve is $TEST"}};
205 auto it = j.begin();
206 boost::container::flat_map<std::string, BasicVariantType> data;
207 data["test"] = 12;
208
209 templateCharReplace(it, data, 0);
210
211 nlohmann::json expected = "twelve is 12";
212 EXPECT_EQ(expected, j["foo"]);
213}
James Feistc296c802019-08-28 09:26:47 -0700214
215TEST(TemplateCharReplace, singleHex)
216{
217 nlohmann::json j = {{"foo", "0x54"}};
218 auto it = j.begin();
219 boost::container::flat_map<std::string, BasicVariantType> data;
220
221 templateCharReplace(it, data, 0);
222
223 nlohmann::json expected = 84;
224 EXPECT_EQ(expected, j["foo"]);
225}