-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
284 lines (237 loc) · 11.1 KB
/
test.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "include/raft/RaftMessageImpl.h"
#include "include/log/Log.h"
#include "include/templateEngine/Loader.h"
#include "include/templateEngine/TemplateEngine.h"
#include "include/json/Json.h"
#define TEST(text, func){ PrintColor4(" [Test]: %s [TESTING]\n",text); func(); PrintColor3(" [Test]: %s [Ok]\n",text)}
#define ASSERT_EQ(left, right){ assert(left==right); }
#define ASSERT_NEQ(left, right){ assert(left!=right); }
#define ASSERT_TRUE(value){ assert(value); }
#define ASSERT_FALSE(value){ assert(!value); }
#define ASSERT_EQ_FLOAT(left, right) {assert(abs(left-right)<0.0001);}
void should_encode_json() {
Serialization::Json jsonNull(nullptr);
ASSERT_EQ(jsonNull.ToString(), "null")
Serialization::Json jsonTrue(true);
ASSERT_EQ(jsonTrue.ToString(), "true");
ASSERT_TRUE(Serialization::Json(true));
Serialization::Json jsonFalse(false);
ASSERT_EQ(jsonFalse.ToString(), "false");
ASSERT_FALSE(Serialization::Json(false));
Serialization::Json jsonStr(std::string("hello, world"));
ASSERT_EQ(jsonStr.ToString(), "\"hello, world\"");
Serialization::Json jsonStr1("hello, world");
ASSERT_EQ(jsonStr1.ToString(), "\"hello, world\"");
Serialization::Json jsonInteger(10086);
ASSERT_EQ(jsonInteger.ToString(), "10086");
Serialization::Json jsonInteger1(-10086);
ASSERT_EQ(jsonInteger1.ToString(), "-10086");
Serialization::Json jsonFloat(3.1415926);
// ASSERT_EQ(jsonFloat.ToString(), "3.1415926");
}
void should_decode_json() {
Serialization::Json jsonNull;
ASSERT_EQ(jsonNull.FromString("null"), nullptr);
Serialization::Json jsonTrue;
ASSERT_EQ((bool) jsonTrue.FromString("true"), true);
Serialization::Json jsonFalse;
ASSERT_EQ((bool) jsonFalse.FromString("false"), false);
Serialization::Json jsonStr;
ASSERT_EQ(jsonStr.FromString("\"hello, world\""), std::string("hello, world"));
Serialization::Json jsonInteger;
ASSERT_EQ((int) jsonInteger.FromString("10086"), 10086);
Serialization::Json jsonInteger1;
ASSERT_EQ((int) jsonInteger1.FromString("-10086"), -10086);
Serialization::Json jsonFloat;
ASSERT_EQ_FLOAT((double) jsonFloat.FromString("3.1415926"), 3.1415926);
Serialization::Json jsonFloat1;
ASSERT_EQ_FLOAT((double) jsonFloat1.FromString("-3.1415926"), -3.1415926);
Serialization::Json jsonFloat3;
ASSERT_EQ_FLOAT((double) jsonFloat3.FromString("0.7"), 0.7);
Serialization::Json jsonFloat2;
ASSERT_EQ_FLOAT((double) jsonFloat2.FromString("3.14e-4"), 3.14e-4);
Serialization::Json jsonArray;
const Serialization::Json &json = jsonArray.FromString("[1,\"hello\",true]");
ASSERT_EQ(json.GetType(), Serialization::Type::Array);
ASSERT_EQ(json.GetSize(), 3);
ASSERT_EQ(json[0]->GetType(), Serialization::Type::Integer);
ASSERT_EQ((int) *json[0], 1);
ASSERT_EQ(json[1]->GetType(), Serialization::Type::String);
ASSERT_EQ((std::string) *json[1], "hello");
ASSERT_EQ(json[2]->GetType(), Serialization::Type::Boolean);
ASSERT_EQ((bool) *json[2], true);
}
void should_encode_decode_raft_message() {
auto *raftMessage = new Raft::RaftMessageImpl();
raftMessage->type = Raft::Type::RequestVote;
raftMessage->requestVoteDetails.candidateId = 2;
raftMessage->requestVoteDetails.term = 2;
raftMessage->nodeId = 10086;
raftMessage->contentLength = 1024;
char *encodeMessage = raftMessage->EncodeMessage();
ASSERT_NEQ(*encodeMessage, '\0');
std::shared_ptr<Raft::RaftMessageImpl> message = raftMessage->DecodeMessage(encodeMessage);
ASSERT_EQ(message->type, Raft::Type::RequestVote);
ASSERT_EQ(message->requestVoteDetails.candidateId, 2);
ASSERT_EQ(message->requestVoteDetails.term, 2);
LogError("%d\n", message->nodeId)
ASSERT_EQ(message->nodeId, 10086);
ASSERT_EQ(message->contentLength, 1024);
}
void should_decode_http_message_header() {
auto *httpMessage = new Http::HttpMessageImpl();
char *msg = "GET / HTTP/1.1\r\n"
"Host: 192.241.213.46:6880\r\n"
"Upgrade-Insecure-Requests: 1\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8\r\n"
"Accept-Language: en-us\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"Connection: keep-alive\r\n\r\n"
"xxx";
std::shared_ptr<Http::HttpMessageImpl> message = httpMessage->DecodeMessage(msg);
ASSERT_EQ(message->request.httpMethod, Http::GET);
ASSERT_EQ(message->request.uri, "/");
ASSERT_EQ(message->request.protocol, "HTTP/1.1");
ASSERT_EQ(message->request.header["Host"], "192.241.213.46:6880");
ASSERT_EQ(message->request.header["Upgrade-Insecure-Requests"], "1");
ASSERT_EQ(message->request.header["Accept"], "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
ASSERT_EQ(message->request.header["User-Agent"], "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8");
ASSERT_EQ(message->request.header["Accept-Language"], "en-us");
ASSERT_EQ(message->request.header["Accept-Encoding"], "gzip, deflate");
ASSERT_EQ(message->request.header["Connection"], "keep-alive");
ASSERT_EQ(message->request.body, "xxx");
}
void should_render_html_template() {
Loader::FileLoader fileLoader;
Template::TemplateEngine templateEngine(fileLoader);
templateEngine.Load("www/test/test.html");
templateEngine.Set("text", "Hello, world");
templateEngine.SetBlock("items").Repeat(3);
const char *title[3] = {"title1", "title2", "title3"};
const char *detail[3] = {"detail1", "detail2", "detail3"};
for (int i = 0; i < 3; i++) {
templateEngine.SetBlock("items")[i].Set("title", title[i]);
templateEngine.SetBlock("items")[i].Set("text", "Lorem Ipsum");
Template::Block &block = templateEngine.SetBlock("items")[i].SetBlock("detailBlock");
block.Set("detail", detail[i]);
if (i == 0) {
block.Disable();
}
}
std::stringbuf buf;
std::ostream sout(&buf);
templateEngine.Render(sout);
ASSERT_EQ(buf.str(), "<html xmlns=\"http://www.w3.org/1999/html\" xmlns=\"http://www.w3.org/1999/html\" lang=\"html\">\n"
"<body>\n"
"<p>Items:</p>\n"
"\n"
"<div>\n"
" <h4>Title: title1</h4>\n"
" <p>Text: Lorem Ipsum</p>\n"
" \n"
"</div>\n"
"\n"
"<div>\n"
" <h4>Title: title2</h4>\n"
" <p>Text: Lorem Ipsum</p>\n"
" \n"
" <div>\n"
" <p>Detail: detail2</p>\n"
" </div>\n"
" \n"
"</div>\n"
"\n"
"<div>\n"
" <h4>Title: title3</h4>\n"
" <p>Text: Lorem Ipsum</p>\n"
" \n"
" <div>\n"
" <p>Detail: detail3</p>\n"
" </div>\n"
" \n"
"</div>\n"
"\n"
"</body>\n"
"</html>\n");
}
void should_render_multi_html_template() {
Loader::MemoryLoader memoryLoader;
Template::TemplateEngine templateEngine(memoryLoader);
memoryLoader.Add("header", "<div>{{ title }}</div>\n{% include body %}\n<p>Hello, World</p>");
memoryLoader.Add("body", "<p>Hi,I am {{ name }}</p>");
templateEngine.Load("header");
templateEngine.Set("title", "my title");
templateEngine.Set("name", "nero yang");
std::stringbuf buf;
std::ostream sout(&buf);
templateEngine.Render(sout);
ASSERT_EQ(buf.str(), "<div>my title</div>\n"
"<p>Hi,I am nero yang</p>\n"
"<p>Hello, World</p>");
}
void should_render_multi_html_file_template() {
Loader::FileLoader fileLoader;
Template::TemplateEngine templateEngine(fileLoader);
templateEngine.Load("www/test/test1.html");
templateEngine.Set("text", "Hello, world");
templateEngine.Set("name", "nero yang");
templateEngine.SetBlock("items").Repeat(3);
const char *title[3] = {"title1", "title2", "title3"};
const char *detail[3] = {"detail1", "detail2", "detail3"};
for (int i = 0; i < 3; i++) {
templateEngine.SetBlock("items")[i].Set("title", title[i]);
templateEngine.SetBlock("items")[i].Set("text", "Lorem Ipsum");
Template::Block &block = templateEngine.SetBlock("items")[i].SetBlock("detailBlock");
block.Set("detail", detail[i]);
if (i == 0) {
block.Disable();
}
}
std::stringbuf buf;
std::ostream sout(&buf);
templateEngine.Render(sout);
ASSERT_EQ(buf.str(), "<html xmlns=\"http://www.w3.org/1999/html\" xmlns=\"http://www.w3.org/1999/html\" lang=\"html\">\n"
"<body>\n"
"<p>Items:</p>\n"
"\n"
"<div>\n"
" <h4>Title: title1</h4>\n"
" <p>Text: Lorem Ipsum</p>\n"
" \n"
"</div>\n"
"\n"
"<div>\n"
" <h4>Title: title2</h4>\n"
" <p>Text: Lorem Ipsum</p>\n"
" \n"
" <div>\n"
" <p>Detail: detail2</p>\n"
" </div>\n"
" \n"
"</div>\n"
"\n"
"<div>\n"
" <h4>Title: title3</h4>\n"
" <p>Text: Lorem Ipsum</p>\n"
" \n"
" <div>\n"
" <p>Detail: detail3</p>\n"
" </div>\n"
" \n"
"</div>\n"
"\n"
"<p>Hi,I am nero yang</p>\n"
"\n"
"</body>\n"
"</html>\n");
}
void test() {
TEST("should_encode_json", should_encode_json)
TEST("should_decode_json", should_decode_json)
// TEST("should_encode_raft_message", should_encode_decode_raft_message)
TEST("should_decode_http_message_header", should_decode_http_message_header)
TEST("should_render_html_template", should_render_html_template)
TEST("should_render_multi_html_template", should_render_multi_html_template)
TEST("should_render_multi_html_file_template", should_render_multi_html_file_template)
}