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