GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/FileParser/TESTS/TEST_PARSER_TOML/main.cpp Lines: 264 264 100.0 %
Date: 2023-10-11 10:52:07 Branches: 361 363 99.4 %

Line Branch Exec Source
1
2
/***************************************
3
	Auteur : Pierre Aubert
4
	Mail : pierre.aubert@lapp.in2p3.fr
5
	Licence : CeCILL-C
6
****************************************/
7
8
#include "parser_toml.h"
9
10
///Check the value of a DicoValue
11
/**	@param mapKey : DicoValue to be checked
12
 * 	@param expectedValue : expected value
13
 * 	@return true if the value matches the expectedValue, false otherwise
14
*/
15
18
bool checkKeyMapValue(const DicoValue * mapKey, const std::string & expectedValue){
16
18
	if(mapKey == NULL){
17
1
		std::cout << "checkKeyMapValue : map NULL for expectedValue = '"<<expectedValue<<"'" << std::endl;
18
1
		return expectedValue == "";
19
	}
20
17
	std::cout << "checkKeyMapValue : map '"<<mapKey->getKey()<<"' => '"<<mapKey->getValue()<<"', expectedValue = '"<<expectedValue<<"'"  << std::endl;
21
17
	bool b(mapKey->getValue() == expectedValue);
22
17
	std::cout << "checkKeyMapValue : b = " << b << std::endl;
23
17
	return b;
24
}
25
26
///Check the value of a DicoValue
27
/**	@param mapKey : DicoValue to be checked
28
 * 	@param expectedValue : expected value
29
 * 	@return true if the value matches the expectedValue, false otherwise
30
*/
31
3
bool checkKeyMapVecValue(const DicoValue * mapKey, const std::vector<std::string> & vecExpectedValue){
32
3
	if(mapKey == NULL){
33
1
		std::cout << "checkKeyMapVecValue : map NULL for vecExpectedValue = " << std::endl;
34
1
		phoenix_print(vecExpectedValue);
35
1
		return vecExpectedValue.size() == 0lu;
36
	}
37


2
	std::cout << "checkKeyMapVecValue : map '"<<mapKey->getKey()<<"' => '"<<mapKey->getValue()<<"', vecExpectedValue = "  << std::endl;
38
2
	phoenix_print(vecExpectedValue);
39
2
	bool b(true);
40
2
	const VecDicoValue & vecValue = mapKey->getVecChild();
41
2
	b &= vecValue.size() == vecExpectedValue.size();
42

2
	std::cout << "checkKeyMapVecValue : vecValue.size() = " << vecValue.size() << ", vecExpectedValue.size() = "<< vecExpectedValue.size() << ", isOk = " << b << std::endl;
43
2
	VecDicoValue::const_iterator it(vecValue.begin());
44
2
	std::vector<std::string>::const_iterator itRef(vecExpectedValue.begin());
45


9
	while(b && it != vecValue.end() && itRef != vecExpectedValue.end()){
46
7
		b &= it->getValue() == *itRef;
47


7
		std::cout << "\tvalue = '" << it->getValue() << "', reference = '"<< *itRef << "', isOk = " << b << std::endl;
48
7
		++it;
49
7
		++itRef;
50
	}
51
2
	std::cout << "checkKeyMapVecValue : b = " << b << std::endl;
52
2
	return b;
53
}
54
55
///Call the check value with NULL pointers
56
1
void testCheckValue(){
57
1
	checkKeyMapValue(NULL, "");
58
2
	std::vector<std::string> vecNoValue;
59
1
	checkKeyMapVecValue(NULL, vecNoValue);
60
1
}
61
62
///Check the YML parser
63
/**	@return true on success, false otherwise
64
*/
65
1
bool checkParserToml(){
66
1
	std::cout << "\ncheckParserToml :" << std::endl;
67
2
	std::string fileContent("[package]\nname = \"hello_hdf5\"\ndescription = \"some string in double quotes\"\nenable_option = true\ndisable_option = false\n\n");
68
2
	std::string tomlFile("test.toml");
69
1
	bool b(true);
70
1
	b &= saveFileContent(tomlFile, fileContent);
71
72
1
	DicoValue dico;
73
1
	b &= parser_toml(dico, tomlFile);
74
1
	std::cout << "checkParserToml : output DicoValue :" << std::endl;
75
1
	dico.print();
76
77
1
	DicoValue * mapPackage = dico.getMap("package");
78
1
	bool isKeyExist = mapPackage != NULL;
79
1
	std::cout << "checkParserToml : isKeyExist = " << isKeyExist << std::endl;
80
1
	DicoValue * mapPackageName = mapPackage->getMap("name");
81
1
	b &= checkKeyMapValue(mapPackageName, "\"hello_hdf5\"");
82
83

1
	b &= phoenix_get_string(*mapPackage, "name", "SomeVar", "") == "hello_hdf5";
84

1
	b &= phoenix_get_string(*mapPackage, "undefined_var", "SomeVar", "") == "SomeVar";
85

1
	b &= phoenix_get_string(*mapPackage, "undefined_var", "", "SomeVar") == "SomeVar";
86
1
	b &= phoenix_get_string(*mapPackage, "undefined_var", "SomeVar") == "SomeVar";
87
88
1
	DicoValue * mapPackageEnableOption = mapPackage->getMap("enable_option");
89
1
	b &= checkKeyMapValue(mapPackageEnableOption, "true");
90
91
1
	DicoValue * mapPackageDisableOption = mapPackage->getMap("disable_option");
92
1
	b &= checkKeyMapValue(mapPackageDisableOption, "false");
93
94
1
	std::cout << "checkParserToml : b = " << b << std::endl;
95
2
	return b;
96
}
97
98
///Check the embeded dico
99
/**	@return true on success, false otherwise
100
*/
101
1
bool checkTomlList(){
102
1
	std::cout << "checkTomlList :" << std::endl;
103
2
	std::string fileContent("[package]\nname = \"hello_hdf5\"\ndescription = 'some string in simple quotes'\nsome_list = [\"one\", \"two\", \"three\"]\nlist_value = [1, 2, 3, 4]\n\n[dependencies]\nhdf5 = \"0.8.1\"\nndarray = '0.15.6'\n\n");
104
105
2
	std::string tomlFile("test_toml_list.toml");
106
1
	bool b(true);
107
1
	b &= saveFileContent(tomlFile, fileContent);
108
109
2
	DicoValue dico;
110
1
	b &= parser_toml(dico, tomlFile);
111
1
	std::cout << "checkTomlList : output DicoValue :" << std::endl;
112
1
	dico.print();
113
114
1
	DicoValue * mapPackage = dico.getMap("package");
115
1
	bool isKeyExist = mapPackage != NULL;
116
1
	std::cout << "checkTomlList : isKeyExist = " << isKeyExist << std::endl;
117
1
	DicoValue * mapPackageName = mapPackage->getMap("name");
118
1
	b &= checkKeyMapValue(mapPackageName, "\"hello_hdf5\"");
119
1
	DicoValue * mapPackageDescription = mapPackage->getMap("description");
120
1
	b &= checkKeyMapValue(mapPackageDescription, "'some string in simple quotes'");
121
122
1
	DicoValue * mapSomeListKey = mapPackage->getMap("some_list");
123
1
	bool isSomeListExist = mapSomeListKey != NULL;
124
1
	std::cout << "checkTomlList : isSomeListExist = " << isSomeListExist << std::endl;
125
126
2
	std::vector<std::string> vecExpectedValue;
127
1
	vecExpectedValue.push_back("\"one\"");
128
1
	vecExpectedValue.push_back("\"two\"");
129
1
	vecExpectedValue.push_back("\"three\"");
130
1
	b &= checkKeyMapVecValue(mapSomeListKey, vecExpectedValue);
131
132
1
	DicoValue * mapOtherListKey = mapPackage->getMap("list_value");
133
1
	bool isOtherListExist = mapOtherListKey != NULL;
134
1
	std::cout << "checkTomlList : isOtherListExist = " << isOtherListExist << std::endl;
135
136
1
	std::vector<std::string> vecOtherExpectedValue;
137
1
	vecOtherExpectedValue.push_back("1");
138
1
	vecOtherExpectedValue.push_back("2");
139
1
	vecOtherExpectedValue.push_back("3");
140
1
	vecOtherExpectedValue.push_back("4");
141
1
	b &= checkKeyMapVecValue(mapOtherListKey, vecOtherExpectedValue);
142
143
1
	DicoValue * mapDependencies = dico.getMap("dependencies");
144
1
	DicoValue * mapDependenciesHdf5 = mapDependencies->getMap("hdf5");
145
1
	b &= checkKeyMapValue(mapDependenciesHdf5, "\"0.8.1\"");
146
1
	DicoValue * mapDependenciesNdarray = mapDependencies->getMap("ndarray");
147
1
	b &= checkKeyMapValue(mapDependenciesNdarray, "'0.15.6'");
148
149
150
1
	std::cout << "checkTomlList : b = " << b << std::endl;
151
2
	return b;
152
}
153
154
///Check the embeded dico
155
/**	@return true on success, false otherwise
156
*/
157
1
bool checkTomlEmptyList(){
158
1
	std::cout << "checkTomlEmptyList :" << std::endl;
159
2
	std::string fileContent("[package]\nsome_list = []\n\n");
160
161
2
	std::string tomlFile("test_toml_empty_list.toml");
162
1
	bool b(true);
163
1
	b &= saveFileContent(tomlFile, fileContent);
164
165
1
	DicoValue dico;
166
1
	b &= parser_toml(dico, tomlFile);
167
1
	std::cout << "checkTomlEmptyList : output DicoValue :" << std::endl;
168
1
	dico.print();
169
170
1
	DicoValue * mapPackage = dico.getMap("package");
171
1
	bool isKeyExist = mapPackage != NULL;
172
1
	std::cout << "checkTomlEmptyList : isKeyExist = " << isKeyExist << std::endl;
173
174
1
	DicoValue * mapSomeListKey = mapPackage->getMap("some_list");
175
1
	bool isSomeListExist = mapSomeListKey != NULL;
176
1
	std::cout << "checkTomlEmptyList : isSomeListExist = " << isSomeListExist << std::endl;
177
1
	b &= mapSomeListKey->getVecChild().size() == 0lu;
178
179
1
	std::cout << "checkTomlEmptyList : b = " << b << std::endl;
180
2
	return b;
181
}
182
183
184
///Check the embeded dico
185
/**	@return true on success, false otherwise
186
*/
187
1
bool checkTomNestedlList(){
188
1
	std::cout << "checkTomlNestedList :" << std::endl;
189
2
	std::string fileContent("[package]\nsome_nested_list = [1, 2, [3, 4]]\n\n");
190
191
2
	std::string tomlFile("test_toml_nested_list.toml");
192
1
	bool b(true);
193
1
	b &= saveFileContent(tomlFile, fileContent);
194
195
1
	DicoValue dico;
196
1
	b &= parser_toml(dico, tomlFile);
197
1
	std::cout << "checkTomlNestedList : output DicoValue :" << std::endl;
198
1
	dico.print();
199
200
1
	DicoValue * mapPackage = dico.getMap("package");
201
1
	bool isKeyExist = mapPackage != NULL;
202
1
	std::cout << "checkTomlNestedList : isKeyExist = " << isKeyExist << std::endl;
203
1
	DicoValue * mapOtherListKey = mapPackage->getMap("some_nested_list");
204
1
	bool isOtherListExist = mapOtherListKey != NULL;
205
1
	std::cout << "checkTomlNestedList : isOtherListExist = " << isOtherListExist << std::endl;
206
207
1
	const VecDicoValue & vecValue = mapOtherListKey->getVecChild();
208
1
	b &= vecValue.size() == 3lu;
209
1
	b &= vecValue[0].getValue() == "1";
210
1
	b &= vecValue[1].getValue() == "2";
211
212
1
	const DicoValue & subList = vecValue[2];
213
1
	const VecDicoValue & vecSubValue = subList.getVecChild();
214
1
	b &= vecSubValue.size() == 2lu;
215
1
	b &= vecSubValue[0].getValue() == "3";
216
1
	b &= vecSubValue[1].getValue() == "4";
217
218
1
	std::cout << "checkTomlNestedList : b = " << b << std::endl;
219
2
	return b;
220
}
221
222
223
///Check the embeded dico
224
/**	@return true on success, false otherwise
225
*/
226
1
bool checkTomNestedlMap(){
227
1
	std::cout << "checkTomNestedlMap :" << std::endl;
228
2
	std::string fileContent("[first.second]\nsome_value = 42\n\n");
229
230
2
	std::string tomlFile("test_toml_nested_map.toml");
231
1
	bool b(true);
232
1
	b &= saveFileContent(tomlFile, fileContent);
233
234
1
	DicoValue dico;
235
1
	b &= parser_toml(dico, tomlFile);
236
1
	std::cout << "checkTomNestedlMap : output DicoValue :" << std::endl;
237
1
	dico.print();
238
239
1
	DicoValue * mapFirst = dico.getMap("first");
240
1
	bool isKeyExist = mapFirst != NULL;
241
1
	std::cout << "checkTomNestedlMap : isKeyExist = " << isKeyExist << std::endl;
242
1
	DicoValue * mapSecond = mapFirst->getMap("second");
243
1
	DicoValue * mapOtherListKey = mapSecond->getMap("some_value");
244
1
	b &= checkKeyMapValue(mapOtherListKey, "42");
245
246
1
	std::cout << "checkTomNestedlMap : b = " << b << std::endl;
247
2
	return b;
248
}
249
250
///Check the embeded dico
251
/**	@return true on success, false otherwise
252
*/
253
1
bool checkTomTable(){
254
1
	std::cout << "checkTomTable :" << std::endl;
255
2
	std::string fileContent("[[program]]\nname = \"shadok\"\nage = 42\n\n[[program]]\nname = \"gibi\"\nage = 23\n\n");
256
257
2
	std::string tomlFile("test_toml_table.toml");
258
1
	bool b(true);
259
1
	b &= saveFileContent(tomlFile, fileContent);
260
261
1
	DicoValue dico;
262
1
	b &= parser_toml(dico, tomlFile);
263
1
	std::cout << "checkTomTable : output DicoValue :" << std::endl;
264
1
	dico.print();
265
1
	DicoValue * mapProgram = dico.getMap("program");
266
1
	bool isKeyExist = mapProgram != NULL;
267
1
	std::cout << "checkTomTable : isKeyExist = " << isKeyExist << std::endl;
268
1
	const VecDicoValue & vecProgramValue = mapProgram->getVecChild();
269
1
	b &= vecProgramValue.size() == 2lu;
270
1
	const DicoValue * mapProgram0Name = vecProgramValue[0].getMap("name");
271
1
	b &= checkKeyMapValue(mapProgram0Name, "\"shadok\"");
272
1
	const DicoValue * mapProgram0Age = vecProgramValue[0].getMap("age");
273
1
	b &= checkKeyMapValue(mapProgram0Age, "42");
274
275
1
	const DicoValue * mapProgram1Name = vecProgramValue[1].getMap("name");
276
1
	b &= checkKeyMapValue(mapProgram1Name, "\"gibi\"");
277
1
	const DicoValue * mapProgram1Age = vecProgramValue[1].getMap("age");
278
1
	b &= checkKeyMapValue(mapProgram1Age, "23");
279
280
1
	std::cout << "checkTomTable : b = " << b << std::endl;
281
2
	return b;
282
}
283
284
///Check the compact dico
285
/**	@return true on success, false otherwise
286
*/
287
1
bool checkTomlCompactDico(){
288
1
	std::cout << "checkTomlCompactDico :" << std::endl;
289
2
	std::string fileContent("[program]\nname = {url = \"someUrl\"}\nage = 42\n\n");
290
291
2
	std::string tomlFile("test_toml_compact_dico.toml");
292
1
	bool b(true);
293
1
	b &= saveFileContent(tomlFile, fileContent);
294
295
1
	DicoValue dico;
296
1
	b &= parser_toml(dico, tomlFile);
297
1
	std::cout << "checkTomlCompactDico : output DicoValue :" << std::endl;
298
1
	dico.print();
299
1
	DicoValue * mapProgram = dico.getMap("program");
300
1
	bool isKeyExist = mapProgram != NULL;
301
1
	std::cout << "checkTomlCompactDico : isKeyExist = " << isKeyExist << std::endl;
302
1
	const DicoValue * mapName = mapProgram->getMap("name");
303
1
	const DicoValue * mapNameUrl = mapName->getMap("url");
304
1
	b &= checkKeyMapValue(mapNameUrl, "\"someUrl\"");
305
1
	const DicoValue * mapAge = mapProgram->getMap("age");
306
1
	b &= checkKeyMapValue(mapAge, "42");
307
308
1
	std::cout << "checkTomTable : b = " << b << std::endl;
309
2
	return b;
310
}
311
312
///Check the compact dico
313
/**	@return true on success, false otherwise
314
*/
315
1
bool checkTomlCompactDico2(){
316
1
	std::cout << "checkTomlCompactDico :" << std::endl;
317
2
	std::string fileContent("[program]\nname = {url = \"someUrl\", version = \"1.0.0\"}\n# Some Comment\nage = 42\nemptyDico = {}\n\n");
318
319
2
	std::string tomlFile("test_toml_compact_dico.toml");
320
1
	bool b(true);
321
1
	b &= saveFileContent(tomlFile, fileContent);
322
323
1
	DicoValue dico;
324
1
	b &= parser_toml(dico, tomlFile);
325
1
	std::cout << "checkTomlCompactDico : output DicoValue :" << std::endl;
326
1
	dico.print();
327
1
	DicoValue * mapProgram = dico.getMap("program");
328
1
	bool isKeyExist = mapProgram != NULL;
329
1
	std::cout << "checkTomlCompactDico : isKeyExist = " << isKeyExist << std::endl;
330
1
	const DicoValue * mapName = mapProgram->getMap("name");
331
1
	const DicoValue * mapNameUrl = mapName->getMap("url");
332
1
	b &= checkKeyMapValue(mapNameUrl, "\"someUrl\"");
333
1
	const DicoValue * mapNameVersion = mapName->getMap("version");
334
1
	b &= checkKeyMapValue(mapNameVersion, "\"1.0.0\"");
335
1
	const DicoValue * mapAge = mapProgram->getMap("age");
336
1
	b &= checkKeyMapValue(mapAge, "42");
337
338
1
	std::cout << "checkTomTable : b = " << b << std::endl;
339
2
	return b;
340
}
341
342
///Check if the parsing of a file is OK
343
/**	@param fileName : name of the file to be created
344
 * 	@param fileContent : content of the file
345
 * 	@return true on success, false otherwise
346
*/
347
8
bool checkIsParserOk(const std::string & fileName, const std::string & fileContent){
348
8
	bool b(true);
349
8
	b &= saveFileContent(fileName, fileContent);
350
8
	DicoValue dico;
351
8
	b &= parser_toml(dico, fileName);
352
16
	return b;
353
}
354
355
///Check the YML parser
356
/**	@return true on success, false otherwise
357
*/
358
1
bool checkParserTomlFail(){
359
1
	bool b(true);
360
1
	DicoValue dico;
361
1
	b &= !parser_toml(dico, "UnexsitingFileName.toml");
362
363
1
	b &= !checkIsParserOk("unextected_token.toml", "=\n");
364
1
	b &= !checkIsParserOk("missing_equal_of_variable.toml", "[package]\nvariable value\n");
365
1
	b &= !checkIsParserOk("missing_value_of_variable.toml", "[package]\nvariable =\n");
366
1
	b &= !checkIsParserOk("unclosed_list.toml", "[package]\nlist = [42\n");
367
1
	b &= !checkIsParserOk("unclosed_empty_list.toml", "[package]\nlist = [\n");
368
1
	b &= !checkIsParserOk("unclosed_dico.toml", "[package]\ndico = {name\n");
369
1
	b &= !checkIsParserOk("unclosed_empty_dico.toml", "[package]\ndico = {\n");
370
1
	b &= checkIsParserOk("some_comment.toml", "# Some value\n[package]\nvalue = 42\n");
371
372
1
	std::cout << "checkParserTomlFail : b = " << b << std::endl;
373
2
	return b;
374
}
375
376
1
int main(int argc, char** argv){
377
1
	testCheckValue();
378
1
	bool b(true);
379
1
	b &= checkParserTomlFail();
380
1
	b &= checkParserToml();
381
1
	b &= checkTomlList();
382
1
	b &= checkTomlEmptyList();
383
1
	b &= checkTomNestedlList();
384
1
	b &= checkTomNestedlMap();
385
1
	b &= checkTomTable();
386
1
	b &= checkTomlCompactDico();
387
1
	b &= checkTomlCompactDico2();
388
1
	std::cout << "Final : " << b << std::endl;
389
1
	return b - 1;
390
}
391
392