1 #include "Utils.hpp" 2 3 #include <boost/container/flat_map.hpp> 4 #include <nlohmann/json.hpp> 5 6 #include <variant> 7 8 #include "gtest/gtest.h" 9 10 TEST(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 23 TEST(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 36 TEST(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 49 TEST(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 } 61 62 TEST(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 75 TEST(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 88 TEST(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 101 TEST(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 114 TEST(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 127 TEST(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"]); 138 } 139 140 TEST(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 153 TEST(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 167 TEST(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"]); 179 } 180 181 TEST(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 202 TEST(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 } 214 215 TEST(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 } 226