Skip to content

Commit 425cd20

Browse files
committed
Expression substitution test refactor
1 parent ff69e8f commit 425cd20

File tree

3 files changed

+95
-160
lines changed

3 files changed

+95
-160
lines changed

test/expressions_test.cpp

Lines changed: 62 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,10 @@
44
#include "gtest/gtest.h"
55

66
#include "jinja2cpp/template.h"
7+
#include "test_tools.h"
78

89
using namespace jinja2;
910

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-
9611
TEST(ExpressionsTest, BinaryMathOperations)
9712
{
9813
std::string source = R"(
@@ -141,31 +56,6 @@ Hello World rain
14156
EXPECT_STREQ(expectedResult.c_str(), result.c_str());
14257
}
14358

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-
16959
TEST(ExpressionsTest, IfExpression)
17060
{
17161
std::string source = R"(
@@ -194,34 +84,64 @@ TEST(ExpressionsTest, IfExpression)
19484
EXPECT_STREQ(expectedResult.c_str(), result.c_str());
19585
}
19686

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+
));

test/filters_test.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,14 @@
66

77
using namespace jinja2;
88

9-
struct FilterGenericTestTag;
10-
using FilterGenericTest = InputOutputPairTest<FilterGenericTestTag>;
9+
SUBSTITUION_TEST_P(FilterGenericTest)
1110

1211
struct ListIteratorTestTag;
1312
using ListIteratorTest = InputOutputPairTest<ListIteratorTestTag>;
1413

1514
struct GroupByTestTag;
1615
using FilterGroupByTest = InputOutputPairTest<GroupByTestTag>;
1716

18-
TEST_P(FilterGenericTest, Test)
19-
{
20-
auto& testParam = GetParam();
21-
std::string source = "{{" + testParam.tpl + "}}";
22-
23-
Template tpl;
24-
ASSERT_TRUE(tpl.Load(source));
25-
26-
std::string result = tpl.RenderAsString(PrepareTestData());
27-
std::cout << result << std::endl;
28-
std::string expectedResult = testParam.result;
29-
EXPECT_EQ(expectedResult, result);
30-
}
31-
3217
TEST_P(ListIteratorTest, Test)
3318
{
3419
auto& testParam = GetParam();

test/test_tools.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <gtest/gtest.h>
55
#include <jinja2cpp/reflected_value.h>
6+
#include <jinja2cpp/template.h>
67

78
struct InputOutputPair
89
{
@@ -16,8 +17,8 @@ struct InputOutputPair
1617
}
1718
};
1819

19-
template<typename Tag>
20-
class InputOutputPairTest : public ::testing::TestWithParam<InputOutputPair>
20+
template<typename Tag, typename Base = ::testing::TestWithParam<InputOutputPair>>
21+
class InputOutputPairTest : public Base
2122
{
2223
};
2324

@@ -89,6 +90,35 @@ inline jinja2::ValuesMap PrepareTestData()
8990
};
9091
}
9192

93+
class SubstitutionTestBase : public ::testing::TestWithParam<InputOutputPair>
94+
{
95+
protected:
96+
void PerformTest(const InputOutputPair& testParam)
97+
{
98+
std::string source = "{{" + testParam.tpl + "}}";
99+
100+
jinja2::Template tpl;
101+
ASSERT_TRUE(tpl.Load(source));
102+
103+
std::string result = tpl.RenderAsString(PrepareTestData());
104+
std::cout << result << std::endl;
105+
std::string expectedResult = testParam.result;
106+
EXPECT_EQ(expectedResult, result);
107+
}
108+
};
109+
110+
struct SubstitutionGenericTestTag;
111+
using SubstitutionGenericTest = InputOutputPairTest<SubstitutionGenericTestTag>;
112+
113+
#define SUBSTITUION_TEST_P(TestName) \
114+
struct TestName##Tag; \
115+
using TestName = InputOutputPairTest<TestName##Tag, SubstitutionTestBase>;\
116+
TEST_P(TestName, Test) \
117+
{ \
118+
auto& testParam = GetParam(); \
119+
PerformTest(testParam); \
120+
}
121+
92122
namespace jinja2
93123
{
94124
template<>

0 commit comments

Comments
 (0)