xref: /openbmc/entity-manager/test/test_entity-manager.cpp (revision 481c5d5cc8a1d598c00111f65b5c4bb7fe6cf434)
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 /*
49 TODO This should work
50 
51 TEST(TemplateCharReplace, replaceMiddleStr)
52 {
53     nlohmann::json j = {{"foo", "the $TEST worked"}};
54     auto it = j.begin();
55     boost::container::flat_map<std::string, BasicVariantType> data;
56     data["TEST"] = std::string("Test");
57 
58     templateCharReplace(it, data, 0);
59 
60     nlohmann::json expected = "the Test worked";
61     EXPECT_EQ(expected, j["foo"]);
62 }
63 */
64 
65 TEST(TemplateCharReplace, replaceLastStr)
66 {
67     nlohmann::json j = {{"foo", "the Test $TEST"}};
68     auto it = j.begin();
69     boost::container::flat_map<std::string, BasicVariantType> data;
70     data["TEST"] = 23;
71 
72     templateCharReplace(it, data, 0);
73 
74     nlohmann::json expected = "the Test 23";
75     EXPECT_EQ(expected, j["foo"]);
76 }
77 
78 TEST(TemplateCharReplace, increment)
79 {
80     nlohmann::json j = {{"foo", "3 plus 1 equals $TEST + 1"}};
81     auto it = j.begin();
82     boost::container::flat_map<std::string, BasicVariantType> data;
83     data["TEST"] = 3;
84 
85     templateCharReplace(it, data, 0);
86 
87     nlohmann::json expected = "3 plus 1 equals 4";
88     EXPECT_EQ(expected, j["foo"]);
89 }
90 
91 TEST(TemplateCharReplace, decrement)
92 {
93     nlohmann::json j = {{"foo", "3 minus 1 equals $TEST - 1 !"}};
94     auto it = j.begin();
95     boost::container::flat_map<std::string, BasicVariantType> data;
96     data["TEST"] = 3;
97 
98     templateCharReplace(it, data, 0);
99 
100     nlohmann::json expected = "3 minus 1 equals 2 !";
101     EXPECT_EQ(expected, j["foo"]);
102 }
103 
104 TEST(TemplateCharReplace, modulus)
105 {
106     nlohmann::json j = {{"foo", "3 mod 2 equals $TEST % 2"}};
107     auto it = j.begin();
108     boost::container::flat_map<std::string, BasicVariantType> data;
109     data["TEST"] = 3;
110 
111     templateCharReplace(it, data, 0);
112 
113     nlohmann::json expected = "3 mod 2 equals 1";
114     EXPECT_EQ(expected, j["foo"]);
115 }
116 
117 TEST(TemplateCharReplace, multiply)
118 {
119     nlohmann::json j = {{"foo", "3 * 2 equals $TEST * 2"}};
120     auto it = j.begin();
121     boost::container::flat_map<std::string, BasicVariantType> data;
122     data["TEST"] = 3;
123 
124     templateCharReplace(it, data, 0);
125 
126     nlohmann::json expected = "3 * 2 equals 6";
127     EXPECT_EQ(expected, j["foo"]);
128 }
129 
130 TEST(TemplateCharReplace, divide)
131 {
132     nlohmann::json j = {{"foo", "4 / 2 equals $TEST / 2"}};
133     auto it = j.begin();
134     boost::container::flat_map<std::string, BasicVariantType> data;
135     data["TEST"] = 4;
136 
137     templateCharReplace(it, data, 0);
138 
139     nlohmann::json expected = "4 / 2 equals 2";
140     EXPECT_EQ(expected, j["foo"]);
141 }