-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_template.cpp
More file actions
134 lines (118 loc) · 3.99 KB
/
Copy pathtest_template.cpp
File metadata and controls
134 lines (118 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <cppunit/extensions/HelperMacros.h>
#include <sstream>
#include <utility>
#include <unordered_map>
#define private public
#define protected public
#include "template.h"
class Template_test : public CppUnit::TestFixture {
public:
void test_compile()
{
std::istringstream stream("Th{{end }} {{ verb }}: {{\n what}}!");
try {
Template tpl(stream);
std::vector<Template::Block> blocks{
Template::Block(Template::DATA, "Th"),
Template::Block(Template::VARIABLE, "end"),
Template::Block(Template::DATA, " "),
Template::Block(Template::VARIABLE, "verb"),
Template::Block(Template::DATA, ": "),
Template::Block(Template::VARIABLE, "what"),
Template::Block(Template::DATA, "!"),
};
CPPUNIT_ASSERT(tpl.m_Blocks == blocks);
}
catch(TemplateError &e)
{
CPPUNIT_FAIL((std::string("Got exception: ") + e.what()).c_str());
}
}
void test_render()
{
try {
std::istringstream stream("Th{{end }} {{ verb }}: {{ what}}!");
Template tpl(stream);
typedef std::pair<std::string, std::string> Item;
std::ostringstream oss;
tpl.render(
oss,
std::unordered_map<std::string, std::string>{
Item("end", "is"),
Item("verb", "is"),
Item("what", "a house")
});
CPPUNIT_ASSERT(oss.str() == "This is: a house!");
oss.str("");
tpl.render(
oss,
{
"end", "ese",
"verb", "are",
"what", "trees"
});
CPPUNIT_ASSERT(oss.str() == "These are: trees!");
oss.str("");
stream.str("Test2{");
Template(stream).render(oss);
CPPUNIT_ASSERT(oss.str() == "Test2{");
oss.str("");
stream.str("Test2{and stuff");
Template(stream).render(oss);
CPPUNIT_ASSERT(oss.str() == "Test2{and stuff");
}
catch(TemplateError &e)
{
CPPUNIT_FAIL((std::string("Got exception: ") + e.what()).c_str());
}
}
void do_error_test(const std::string &expr, bool should_fail)
{
std::istringstream stream(expr);
if(should_fail)
CPPUNIT_ASSERT_THROW(Template a(stream), TemplateError);
else
CPPUNIT_ASSERT_NO_THROW(Template a(stream));
}
void test_errors()
{
do_error_test("This{{", true);
do_error_test("This{ {is}}", false);
do_error_test("This{{is} }", true);
do_error_test("This{{is}}", false);
do_error_test("This{{is}", true);
do_error_test("This{{is", true);
}
void test_missing_var()
{
try {
typedef std::pair<std::string, std::string> Item;
std::istringstream stream("Ceci {{is}} un {{test}}");
Template tpl(stream);
std::ostringstream oss;
tpl.render(
oss,
std::unordered_map<std::string, std::string>{
Item("is", "est"),
Item("test", "essai")
});
CPPUNIT_ASSERT_THROW(
tpl.render(oss, {"is", "est"}),
TemplateError);
CPPUNIT_ASSERT_THROW(
tpl.render(oss),
TemplateError);
}
catch(TemplateError &e)
{
CPPUNIT_FAIL((std::string("Got exception: ") + e.what()).c_str());
}
}
CPPUNIT_TEST_SUITE(Template_test);
CPPUNIT_TEST(test_compile);
CPPUNIT_TEST(test_render);
CPPUNIT_TEST(test_errors);
CPPUNIT_TEST(test_missing_var);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(Template_test);