-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.cpp
More file actions
352 lines (272 loc) · 8.55 KB
/
Copy pathexample_test.cpp
File metadata and controls
352 lines (272 loc) · 8.55 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "JSON_parser/json.h"
#include <gtest/gtest.h>
#pragma region "LOAD NULL"
TEST(JSON, LOAD_NULL){
using namespace BMSTU;
std::string s = "null";
std::stringstream ss;
ss.str(s);
Document first = Load(ss);
Document last = Document({});
EXPECT_EQ(first, last);
}
#pragma endregion
#pragma region "LOAD STRING"
TEST(JSON, LOADSTRING){
using namespace BMSTU;
std::stringstream ss;
ss.str("\"some_string\"");
Document first = Load(ss);
Document last = Document(std::string("some_string"));
Print(first,std::cout);
Print(last, std::cout);
EXPECT_EQ(first, last);
}
TEST(LOADSTRING, ESCAPE_SYMBOLS){
using namespace BMSTU;
std::stringstream ss;
ss.str("\"hello\\t here is the \\\"quotes\\\"\\nhere is the \\ttabulation\\\\\"");
Document first = Load(ss);
Print(first,std::cout);
Document last = Document(std::string(("hello\t here is the \"quotes\"\nhere is the \ttabulation\\")));
EXPECT_EQ(first, last);
}
TEST(LOADSTR_EXC, Unknown_escape){
using namespace BMSTU;
std::stringstream ss("\"Hello\\h\"");
try {
Document doc = BMSTU::Load(ss);
}catch (std::exception &exception){
EXPECT_STREQ(exception.what(), "Unknown \\h escape symb");
}
}
TEST(LOADSTR_EXC, EOL){
using namespace BMSTU;
std::stringstream ss("\"Hello\n\"");
try {
Document doc = BMSTU::Load(ss);
}catch (std::exception &exception){
EXPECT_STREQ(exception.what(), "Unexpected EOL");
}
}
#pragma endregion
#pragma region "LOAD BOOL"
TEST(JSON, LOADBOOL_TRUE){
using namespace BMSTU;
std::string s = "true";
std::stringstream ss;
ss.str(s);
Document first = Load(ss);
Document last = Document(true);
EXPECT_EQ(first, last);
}
TEST(JSON, LOADBOOL_FALSE){
using namespace BMSTU;
std::string s = "false";
std::stringstream ss;
ss.str(s);
Document first = Load(ss);
Document last = Document(false);
EXPECT_EQ(first, last);
}
///LOAD BOOL EXCEPTION
TEST(LOADBOOL, EXCEPTION){
using namespace BMSTU;
std::stringstream ss;
ss.str("flse");
try {
Document first = Load(ss);
} catch(std::exception &exception){
EXPECT_STREQ(exception.what(), " Failed to parse flse as bool");
}
}
#pragma endregion
#pragma region "LOAD ARRAY"
TEST(JSON, LOADARRAY){
using namespace BMSTU;
std::stringstream ss;
ss.str("[true, true, null, \"str1\", \"str2\", 233, -678, 123.532]");
Document first = Load(ss);
Document last = Document(Array{true, true, {}, std::string("str1"), std::string("str2"), 233, -678, 123.532});
EXPECT_EQ(first, last);
}
///LOAD ARRAY EXCEPTION
TEST(LOADARRAY, EXCEPTION){
using namespace BMSTU;
std::stringstream ss;
ss.str("[[]");
try{
Document doc = Load(ss);
} catch (std::exception &exception){
ASSERT_STREQ(exception.what(), "ARRAY parsing exception");
}
}
#pragma endregion
#pragma region "LOAD DICT"
TEST(JSON, LOADDICT_ARRAY_VALUE){
using namespace BMSTU;
std::stringstream ss;
ss.str("{\"key_ARRAY\" : [true, null, \"my_string\"]}");
Document first = Load(ss);
Document last = Document(Dict{{std::string("key_ARRAY"), Array{true, {}, std::string("my_string")}}});
EXPECT_EQ(first, last);
}
///LOAD DICT_STRING
TEST(JSON, LOADDICT_STRING_VALUE){
using namespace BMSTU;
std::stringstream ss;
ss.str("{\"key_STRING\" : \"my\\tstring\\nbetter\\\\than\\tyours\"}");
Document first = Load(ss);
Document last = Document(Dict{{std::string("key_STRING"),
std::string("my\tstring\nbetter\\than\tyours")}});
EXPECT_EQ(first, last);
}
///LOAD DICT_BOOL
TEST(JSON, LOADDICT_BOOL_VALUE){
using namespace BMSTU;
std::stringstream ss;
ss.str("{\"key_STRING\" : true}");
Document first = Load(ss);
Document last = Document(Dict{{std::string("key_STRING"), true}});
EXPECT_EQ(first, last);
}
///LOAD DICT_NULL
TEST(JSON, LOADDICT_NULL_VALUE){
using namespace BMSTU;
std::stringstream ss;
ss.str("{\"key_STRING\" : null}");
Document first = Load(ss);
Document last = Document(Dict{{std::string("key_STRING"), {}}});
EXPECT_EQ(first, last);
}
///LOAD DICT_NUM
TEST(JSON, LOADDICT_NUM_VALUE){
using namespace BMSTU;
std::stringstream ss;
ss.str("{\"key_STRING\" : -123.7645}");
Document first = Load(ss);
Document last = Document(Dict{{std::string("key_STRING"), -123.7645}});
EXPECT_EQ(first, last);
}
///can't add more than one VALUE
TEST(LOADDICT_EXC, MORE_VALUE){
using namespace BMSTU;
std::stringstream ss;
ss.str("{\"key_STRING\" : null, true}");
try {
Document first = Load(ss);
} catch(std::exception &exception){
EXPECT_STREQ(exception.what(), "can't add more than one VALUE");
}
}
///NO_DOUBLE_AT
TEST(LOADDICT_EXC, NO_DOUBLE_AT){
using namespace BMSTU;
std::stringstream ss, ss1;
ss.str("{\"key_STRING\"} : true");
try {
Document first = Load(ss);
} catch(std::exception &exception){
EXPECT_STREQ(exception.what(), " : is expected but } has been founded");
}
}
TEST(LOADDICT_EXC, NO_VALUE){
std::stringstream ss("{\"wadwd\" :}");
try{
BMSTU::Document doc = BMSTU::Load(ss);
} catch (std::exception &exception){
ASSERT_STREQ(exception.what(), "No value");
}
}
TEST(LOADDICT_EXC, parsing_error){
using namespace BMSTU;
std::stringstream ss("{{");
try {
Document doc = BMSTU::Load(ss);
}catch (std::exception &exception){
EXPECT_STREQ(exception.what(), "DICT parsing exception");
}
}
#pragma endregion
#pragma region "LOAD NUMBER"
TEST(JSON, LOADNUMBER){
using namespace BMSTU;
std::stringstream ss;
ss.str("[123e-1, +12, +123.532, -165, -0.532456]");
Document first = Load(ss);
Document last = Document(Array{123e-1, 12, 123.532, -165, -0.532456});
EXPECT_EQ(first, last);
}
///NO_DIGIT
TEST(NUM_EXCEPTION, NO_DIGIT){
///так как switch в Loadnode по дефолту уходит в LoadNum, тогда любой неправильно
///введенный символ уходит в ошибку no_digit
using namespace BMSTU;
std::stringstream ss;
ss.str("-.10.1");
try{
Document doc = Load(ss);
} catch (std::exception &exception){
ASSERT_STREQ(exception.what(), "digit Error");
}
}
TEST(NUM_EXCEPTION, NUM_ERROR){
using namespace BMSTU;
std::stringstream ss("1.9770e+308");
try{
Document doc = Load(ss);
} catch (std::exception &exception){
ASSERT_STREQ(exception.what(), " number error ");
}
}
#pragma endregion
///LOAD NODE EXCEPTION NOT EOF
TEST(LOADNODE, Unexpected_EOF){
using namespace BMSTU;
std::ifstream input("false path");
try {
Document doc = BMSTU::Load(input);
}catch (std::exception &exception){
EXPECT_STREQ(exception.what(), " Error unexpected EOF ");
}
}
///PRINT
TEST(JSON, Print){
using namespace BMSTU;
std::stringstream ss;
ss.str("[{\"BOOL\" : [{\"TRUE\" : true}, {\"FALSE\" : false}]},"
"{\"NULL\" : null}, {\"NUMBERS\" : [{\"INT\" : 12}, {\"DOUBLE\" : 12.52},"
"{\"NEGATIVE\" : -123.5}, {\"EXPONENT\" : -13e-10}]}, "
"{\"STRING\" : \"my\\\\\\n\\tstring\"}, "
"{\"ARRAY\" : [{\"ARRAY_STRING\" : [\"string1\", \"string2\"]}, "
"{\"ARRAY_NUM\" : [1,-2,2.542]}]}, {\"DICT\" : \"dict\"}]");
Document doc = Load(ss);
Print(doc,std::cout);
}
///NODE METHODS
TEST(JSON, NODE_METHODS){
using namespace BMSTU;
std::stringstream stream_int, stream_double, stream_bool, stream_null, stream_Array, stream_Dict;
stream_int.str("12");
stream_double.str("14.653");
stream_bool.str("true");
stream_Array.str("[1,2,3]");
stream_Dict.str("{\"key\" : \"value\"}");
stream_null.str("null");
int value = LoadNode(stream_int).AsInt();
EXPECT_EQ(value, 12);
std::stringstream int_error("12.421");
EXPECT_ANY_THROW(int error = LoadNode(int_error).AsInt());
double val_double = LoadNode(stream_double).AsDouble();
EXPECT_EQ(val_double, 14.653);
std::stringstream double_error("true");
EXPECT_ANY_THROW(double error = LoadNode(double_error).AsDouble());
bool val_bool = LoadNode(stream_bool).AsBool();
EXPECT_TRUE(val_bool);
std::stringstream bool_error("12.421");
EXPECT_ANY_THROW(bool error = LoadNode(bool_error).AsBool());
bool is_null = LoadNode(stream_null).IsNull();
EXPECT_TRUE(is_null);
Array val_Array = LoadNode(stream_Array).AsArray();
Dict val_Dict = LoadNode(stream_Dict).AsDict();
}