|
4 | 4 | #include "gtest/gtest.h" |
5 | 5 |
|
6 | 6 | #include "jinja2cpp/template.h" |
| 7 | +#include "test_tools.h" |
7 | 8 |
|
8 | 9 | using namespace jinja2; |
9 | 10 |
|
10 | | -TEST(ExpressionsTest, BasicValueSubstitution) |
11 | | -{ |
12 | | - std::string source = R"( |
13 | | -IntValue: {{intValue}} |
14 | | -StringValue: {{stringValue}} |
15 | | -DoubleValue: {{doubleValue}} |
16 | | -BoolFalceValue: {{boolFalseValue}} |
17 | | -BoolTrueValue: {{boolTrueValue}} |
18 | | -)"; |
19 | | - |
20 | | - Template tpl; |
21 | | - ASSERT_TRUE(tpl.Load(source)); |
22 | | - |
23 | | - ValuesMap params = { |
24 | | - {"intValue", 3}, |
25 | | - {"doubleValue", 12.123f}, |
26 | | - {"stringValue", "rain"}, |
27 | | - {"boolFalseValue", false}, |
28 | | - {"boolTrueValue", true}, |
29 | | - }; |
30 | | - |
31 | | - std::string result = tpl.RenderAsString(params); |
32 | | - std::cout << result << std::endl; |
33 | | - std::string expectedResult = R"( |
34 | | -IntValue: 3 |
35 | | -StringValue: rain |
36 | | -DoubleValue: 12.123 |
37 | | -BoolFalceValue: false |
38 | | -BoolTrueValue: true |
39 | | -)"; |
40 | | - |
41 | | - EXPECT_STREQ(expectedResult.c_str(), result.c_str()); |
42 | | -} |
43 | | - |
44 | | -TEST(ExpressionsTest, BasicConstantSubstitution) |
45 | | -{ |
46 | | - std::string source = R"( |
47 | | -{{ "Hello" }} {{ 'world' }}!!! |
48 | | -)"; |
49 | | - |
50 | | - Template tpl; |
51 | | - ASSERT_TRUE(tpl.Load(source)); |
52 | | - |
53 | | - ValuesMap params = { |
54 | | - }; |
55 | | - |
56 | | - std::string result = tpl.RenderAsString(params); |
57 | | - std::cout << result << std::endl; |
58 | | - std::string expectedResult = R"( |
59 | | -Hello world!!! |
60 | | -)"; |
61 | | - |
62 | | - EXPECT_STREQ(expectedResult.c_str(), result.c_str()); |
63 | | -} |
64 | | - |
65 | | -TEST(ExpressionsTest, ConstantSubstitution) |
66 | | -{ |
67 | | - std::string source = R"( |
68 | | -SingleQuotedString={{ 'SingleQuotedString' }} |
69 | | -DoubleQuotedString={{ "DoubleQuotedString" }} |
70 | | -IntValue={{ 100500 }} |
71 | | -DoubleValue={{ 42.42 }} |
72 | | -BoolTrueValue={{ True }} |
73 | | -BoolFalseValue={{ False }} |
74 | | -)"; |
75 | | - |
76 | | - Template tpl; |
77 | | - ASSERT_TRUE(tpl.Load(source)); |
78 | | - |
79 | | - ValuesMap params = { |
80 | | - }; |
81 | | - |
82 | | - std::string result = tpl.RenderAsString(params); |
83 | | - std::cout << result << std::endl; |
84 | | - std::string expectedResult = R"( |
85 | | -SingleQuotedString=SingleQuotedString |
86 | | -DoubleQuotedString=DoubleQuotedString |
87 | | -IntValue=100500 |
88 | | -DoubleValue=42.42 |
89 | | -BoolTrueValue=true |
90 | | -BoolFalseValue=false |
91 | | -)"; |
92 | | - |
93 | | - EXPECT_STREQ(expectedResult.c_str(), result.c_str()); |
94 | | -} |
95 | | - |
96 | 11 | TEST(ExpressionsTest, BinaryMathOperations) |
97 | 12 | { |
98 | 13 | std::string source = R"( |
@@ -141,31 +56,6 @@ Hello World rain |
141 | 56 | EXPECT_STREQ(expectedResult.c_str(), result.c_str()); |
142 | 57 | } |
143 | 58 |
|
144 | | -TEST(ExpressionFiltersTest, Join) |
145 | | -{ |
146 | | - std::string source = R"( |
147 | | -{{ ("Hello", 'world') | join }}!!! |
148 | | -{{ ("Hello", 'world') | join(', ') }}!!! |
149 | | -{{ ("Hello", 'world') | join(d = '; ') }}!!! |
150 | | -)"; |
151 | | - |
152 | | - Template tpl; |
153 | | - ASSERT_TRUE(tpl.Load(source)); |
154 | | - |
155 | | - ValuesMap params = { |
156 | | - }; |
157 | | - |
158 | | - std::string result = tpl.RenderAsString(params); |
159 | | - std::cout << result << std::endl; |
160 | | - std::string expectedResult = R"( |
161 | | -Helloworld!!! |
162 | | -Hello, world!!! |
163 | | -Hello; world!!! |
164 | | -)"; |
165 | | - |
166 | | - EXPECT_STREQ(expectedResult.c_str(), result.c_str()); |
167 | | -} |
168 | | - |
169 | 59 | TEST(ExpressionsTest, IfExpression) |
170 | 60 | { |
171 | 61 | std::string source = R"( |
@@ -194,34 +84,64 @@ TEST(ExpressionsTest, IfExpression) |
194 | 84 | EXPECT_STREQ(expectedResult.c_str(), result.c_str()); |
195 | 85 | } |
196 | 86 |
|
197 | | -TEST(ExpressionsTest, IndexExpression) |
198 | | -{ |
199 | | - std::string source = R"( |
200 | | -{{listValue[0]}} |
201 | | -{{dictValue.item1}} |
202 | | -{{dictValue["item1"]}} |
203 | | -{{listValue[dictValue.item2]}} |
204 | | -{{dictValue.item4[2]}} |
205 | | -)"; |
206 | | - |
207 | | - Template tpl; |
208 | | - ASSERT_TRUE(tpl.Load(source)); |
209 | | - |
210 | | - ValuesMap params = { |
211 | | - {"listValue", ValuesList{10, 20, 30, 40}}, |
212 | | - {"dictValue", ValuesMap{{"item1", 1}, {"item2", 2}, {"item3", 3}, {"item4", ValuesList{60, 70, 80}}}}, |
213 | | - }; |
214 | | - |
215 | | - std::string result = tpl.RenderAsString(params); |
216 | | - std::cout << result << std::endl; |
217 | | - std::string expectedResult = R"( |
218 | | -10 |
219 | | -1 |
220 | | -1 |
221 | | -30 |
222 | | -80 |
223 | | -)"; |
224 | | - |
225 | | - EXPECT_STREQ(expectedResult.c_str(), result.c_str()); |
226 | | -} |
227 | | - |
| 87 | +SUBSTITUION_TEST_P(ExpressionSubstitutionTest) |
| 88 | + |
| 89 | +INSTANTIATE_TEST_CASE_P(ConstantSubstitutionTest, ExpressionSubstitutionTest, ::testing::Values( |
| 90 | + InputOutputPair{"", ""}, |
| 91 | + InputOutputPair{"'str1'", "str1"}, |
| 92 | + InputOutputPair{"\"str1\"", "str1"}, |
| 93 | + InputOutputPair{"100500", "100500"}, |
| 94 | + InputOutputPair{"'100.555'", "100.555"}, |
| 95 | + InputOutputPair{"true", "true"}, |
| 96 | + InputOutputPair{"false", "false"} |
| 97 | + )); |
| 98 | + |
| 99 | +INSTANTIATE_TEST_CASE_P(BasicValueSubstitutionTest, ExpressionSubstitutionTest, ::testing::Values( |
| 100 | + InputOutputPair{"intValue", "3"}, |
| 101 | + InputOutputPair{"doubleValue", "12.123"}, |
| 102 | + InputOutputPair{"stringValue", "rain"}, |
| 103 | + InputOutputPair{"boolTrueValue", "true"}, |
| 104 | + InputOutputPair{"boolFalseValue", "false"} |
| 105 | + )); |
| 106 | + |
| 107 | +INSTANTIATE_TEST_CASE_P(IndexSubscriptionTest, ExpressionSubstitutionTest, ::testing::Values( |
| 108 | + InputOutputPair{"intValue[0]", ""}, |
| 109 | + InputOutputPair{"doubleValue[0]", ""}, |
| 110 | + InputOutputPair{"stringValue[0]", "r"}, |
| 111 | + InputOutputPair{"boolTrueValue[0]", ""}, |
| 112 | + InputOutputPair{"boolFalseValue[0]", ""}, |
| 113 | + InputOutputPair{"intList[-1]", ""}, |
| 114 | + InputOutputPair{"intList[10]", ""}, |
| 115 | + InputOutputPair{"intList[0]", "9"}, |
| 116 | + InputOutputPair{"intList[9]", "4"}, |
| 117 | + InputOutputPair{"intList[5]", "2"}, |
| 118 | + InputOutputPair{"mapValue['intVal']", "10"}, |
| 119 | + InputOutputPair{"mapValue['dblVal']", "100.5"}, |
| 120 | + InputOutputPair{"mapValue['stringVal']", "string100.5"}, |
| 121 | + InputOutputPair{"mapValue['boolValue']", "true"}, |
| 122 | + InputOutputPair{"mapValue['intVAl']", ""}, |
| 123 | + InputOutputPair{"reflectedVal['intValue']", "0"}, |
| 124 | + InputOutputPair{"reflectedVal['dblValue']", "0"}, |
| 125 | + InputOutputPair{"reflectedVal['boolValue']", "false"}, |
| 126 | + InputOutputPair{"reflectedVal['strValue']", "test string 0"}, |
| 127 | + InputOutputPair{"reflectedVal['StrValue']", ""} |
| 128 | + )); |
| 129 | + |
| 130 | +INSTANTIATE_TEST_CASE_P(DotSubscriptionTest, ExpressionSubstitutionTest, ::testing::Values( |
| 131 | + InputOutputPair{"mapValue.intVal", "10"}, |
| 132 | + InputOutputPair{"mapValue.dblVal", "100.5"}, |
| 133 | + InputOutputPair{"mapValue.stringVal", "string100.5"}, |
| 134 | + InputOutputPair{"mapValue.boolValue", "true"}, |
| 135 | + InputOutputPair{"mapValue.intVAl", ""}, |
| 136 | + InputOutputPair{"reflectedVal.intValue", "0"}, |
| 137 | + InputOutputPair{"reflectedVal.dblValue", "0"}, |
| 138 | + InputOutputPair{"reflectedVal.boolValue", "false"}, |
| 139 | + InputOutputPair{"reflectedVal.strValue", "test string 0"}, |
| 140 | + InputOutputPair{"reflectedVal.StrValue", ""} |
| 141 | + )); |
| 142 | + |
| 143 | + |
| 144 | +INSTANTIATE_TEST_CASE_P(ComplexSubscriptionTest, ExpressionSubstitutionTest, ::testing::Values( |
| 145 | +// InputOutputPair{"mapValue.reflectedList[1]['intValue']", "1"}, |
| 146 | + InputOutputPair{"reflectedVal.strValue[0]", "t"} |
| 147 | + )); |
0 commit comments