GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/FileParser/TESTS/TEST_PARSER_TOML/main.cpp Lines: 191 191 100.0 %
Date: 2024-09-10 03:06:26 Branches: 464 466 99.6 %

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 "phoenix_assert.h"
9
#include "parser_toml.h"
10
11
///Check the value of a DicoValue
12
/**	@param mapKey : DicoValue to be checked
13
 * 	@param expectedValue : expected value
14
 * 	@return true if the value matches the expectedValue, false otherwise
15
*/
16
18
bool checkKeyMapValue(const DicoValue * mapKey, const std::string & expectedValue){
17
18
	if(mapKey == NULL){
18
1
		std::cout << "checkKeyMapValue : map NULL for expectedValue = '"<<expectedValue<<"'" << std::endl;
19
1
		return expectedValue == "";
20
	}
21
// 	std::cout << "checkKeyMapValue : map '"<<mapKey->getKey()<<"' => '"<<mapKey->getValue()<<"', expectedValue = '"<<expectedValue<<"'"  << std::endl;
22
17
	bool b(mapKey->getValue() == expectedValue);
23
// 	std::cout << "checkKeyMapValue : b = " << b << std::endl;
24
17
	return b;
25
}
26
27
///Check the value of a DicoValue
28
/**	@param mapKey : DicoValue to be checked
29
 * 	@param expectedValue : expected value
30
 * 	@return true if the value matches the expectedValue, false otherwise
31
*/
32
3
bool checkKeyMapVecValue(const DicoValue * mapKey, const std::vector<std::string> & vecExpectedValue){
33
3
	if(mapKey == NULL){
34
1
		std::cout << "checkKeyMapVecValue : map NULL for vecExpectedValue = " << std::endl;
35
1
		phoenix_print(vecExpectedValue);
36
1
		return vecExpectedValue.size() == 0lu;
37
	}
38
// 	std::cout << "checkKeyMapVecValue : map '"<<mapKey->getKey()<<"' => '"<<mapKey->getValue()<<"', vecExpectedValue = "  << std::endl;
39
2
	phoenix_print(vecExpectedValue);
40
2
	bool b(true);
41
2
	const VecDicoValue & vecValue = mapKey->getVecChild();
42
2
	b &= vecValue.size() == vecExpectedValue.size();
43
// 	std::cout << "checkKeyMapVecValue : vecValue.size() = " << vecValue.size() << ", vecExpectedValue.size() = "<< vecExpectedValue.size() << ", isOk = " << b << std::endl;
44
2
	VecDicoValue::const_iterator it(vecValue.begin());
45
2
	std::vector<std::string>::const_iterator itRef(vecExpectedValue.begin());
46


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


7
		std::cout << "\tvalue = '" << it->getValue() << "', reference = '"<< *itRef << "', isOk = " << b << std::endl;
49
7
		++it;
50
7
		++itRef;
51
	}
52
// 	std::cout << "checkKeyMapVecValue : b = " << b << std::endl;
53
2
	return b;
54
}
55
56
///Call the check value with NULL pointers
57
1
void testCheckValue(){
58
1
	checkKeyMapValue(NULL, "");
59
2
	std::vector<std::string> vecNoValue;
60
1
	checkKeyMapVecValue(NULL, vecNoValue);
61
1
}
62
63
///Check the YML parser
64
1
void checkParserToml(){
65
2
	std::string fileContent("[package]\nname = \"hello_hdf5\"\ndescription = \"some string in double quotes\"\nenable_option = true\ndisable_option = false\n\n");
66
2
	std::string tomlFile("test.toml");
67

1
	phoenix_assert(saveFileContent(tomlFile, fileContent));
68
69
1
	DicoValue dico;
70

1
	phoenix_assert(parser_toml(dico, tomlFile));
71
// 	std::cout << "checkParserToml : output DicoValue :" << std::endl;
72
// 	dico.print();
73
74
1
	DicoValue * mapPackage = dico.getMap("package");
75
// 	bool isKeyExist = mapPackage != NULL;
76
// 	std::cout << "checkParserToml : isKeyExist = " << isKeyExist << std::endl;
77
1
	DicoValue * mapPackageName = mapPackage->getMap("name");
78

1
	phoenix_assert(checkKeyMapValue(mapPackageName, "\"hello_hdf5\""));
79
80


1
	phoenix_assert(phoenix_get_string(*mapPackage, "name", "SomeVar", "") == "hello_hdf5");
81


1
	phoenix_assert(phoenix_get_string(*mapPackage, "undefined_var", "SomeVar", "") == "SomeVar");
82


1
	phoenix_assert(phoenix_get_string(*mapPackage, "undefined_var", "", "SomeVar") == "SomeVar");
83

1
	phoenix_assert(phoenix_get_string(*mapPackage, "undefined_var", "SomeVar") == "SomeVar");
84
85
1
	DicoValue * mapPackageEnableOption = mapPackage->getMap("enable_option");
86

1
	phoenix_assert(checkKeyMapValue(mapPackageEnableOption, "true"));
87
88
1
	DicoValue * mapPackageDisableOption = mapPackage->getMap("disable_option");
89

1
	phoenix_assert(checkKeyMapValue(mapPackageDisableOption, "false"));
90
1
}
91
92
///Check the embeded dico
93
1
void checkTomlList(){
94
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");
95
96
2
	std::string tomlFile("test_toml_list.toml");
97

1
	phoenix_assert(saveFileContent(tomlFile, fileContent));
98
99
2
	DicoValue dico;
100

1
	phoenix_assert(parser_toml(dico, tomlFile));
101
// 	std::cout << "checkTomlList : output DicoValue :" << std::endl;
102
// 	dico.print();
103
104
1
	DicoValue * mapPackage = dico.getMap("package");
105
// 	bool isKeyExist = mapPackage != NULL;
106
// 	std::cout << "checkTomlList : isKeyExist = " << isKeyExist << std::endl;
107
1
	DicoValue * mapPackageName = mapPackage->getMap("name");
108

1
	phoenix_assert(checkKeyMapValue(mapPackageName, "\"hello_hdf5\""));
109
1
	DicoValue * mapPackageDescription = mapPackage->getMap("description");
110

1
	phoenix_assert(checkKeyMapValue(mapPackageDescription, "'some string in simple quotes'"));
111
112
1
	DicoValue * mapSomeListKey = mapPackage->getMap("some_list");
113
// 	bool isSomeListExist = mapSomeListKey != NULL;
114
// 	std::cout << "checkTomlList : isSomeListExist = " << isSomeListExist << std::endl;
115
116
2
	std::vector<std::string> vecExpectedValue;
117
1
	vecExpectedValue.push_back("\"one\"");
118
1
	vecExpectedValue.push_back("\"two\"");
119
1
	vecExpectedValue.push_back("\"three\"");
120

1
	phoenix_assert(checkKeyMapVecValue(mapSomeListKey, vecExpectedValue));
121
122
1
	DicoValue * mapOtherListKey = mapPackage->getMap("list_value");
123
// 	bool isOtherListExist = mapOtherListKey != NULL;
124
// 	std::cout << "checkTomlList : isOtherListExist = " << isOtherListExist << std::endl;
125
126
1
	std::vector<std::string> vecOtherExpectedValue;
127
1
	vecOtherExpectedValue.push_back("1");
128
1
	vecOtherExpectedValue.push_back("2");
129
1
	vecOtherExpectedValue.push_back("3");
130
1
	vecOtherExpectedValue.push_back("4");
131

1
	phoenix_assert(checkKeyMapVecValue(mapOtherListKey, vecOtherExpectedValue));
132
133
1
	DicoValue * mapDependencies = dico.getMap("dependencies");
134
1
	DicoValue * mapDependenciesHdf5 = mapDependencies->getMap("hdf5");
135

1
	phoenix_assert(checkKeyMapValue(mapDependenciesHdf5, "\"0.8.1\""));
136
1
	DicoValue * mapDependenciesNdarray = mapDependencies->getMap("ndarray");
137

1
	phoenix_assert(checkKeyMapValue(mapDependenciesNdarray, "'0.15.6'"));
138
1
}
139
140
///Check the embeded dico
141
1
void checkTomlEmptyList(){
142
2
	std::string fileContent("[package]\nsome_list = []\n\n");
143
144
2
	std::string tomlFile("test_toml_empty_list.toml");
145

1
	phoenix_assert(saveFileContent(tomlFile, fileContent));
146
147
1
	DicoValue dico;
148

1
	phoenix_assert(parser_toml(dico, tomlFile));
149
// 	std::cout << "checkTomlEmptyList : output DicoValue :" << std::endl;
150
// 	dico.print();
151
152
1
	DicoValue * mapPackage = dico.getMap("package");
153
// 	bool isKeyExist = mapPackage != NULL;
154
// 	std::cout << "checkTomlEmptyList : isKeyExist = " << isKeyExist << std::endl;
155
156
1
	DicoValue * mapSomeListKey = mapPackage->getMap("some_list");
157
// 	bool isSomeListExist = mapSomeListKey != NULL;
158
// 	std::cout << "checkTomlEmptyList : isSomeListExist = " << isSomeListExist << std::endl;
159

1
	phoenix_assert(mapSomeListKey->getVecChild().size() == 0lu);
160
1
}
161
162
163
///Check the embeded dico
164
1
void checkTomNestedlList(){
165
2
	std::string fileContent("[package]\nsome_nested_list = [1, 2, [3, 4]]\n\n");
166
167
2
	std::string tomlFile("test_toml_nested_list.toml");
168

1
	phoenix_assert(saveFileContent(tomlFile, fileContent));
169
170
1
	DicoValue dico;
171

1
	phoenix_assert(parser_toml(dico, tomlFile));
172
// 	std::cout << "checkTomlNestedList : output DicoValue :" << std::endl;
173
// 	dico.print();
174
175
1
	DicoValue * mapPackage = dico.getMap("package");
176
// 	bool isKeyExist = mapPackage != NULL;
177
// 	std::cout << "checkTomlNestedList : isKeyExist = " << isKeyExist << std::endl;
178
1
	DicoValue * mapOtherListKey = mapPackage->getMap("some_nested_list");
179
// 	bool isOtherListExist = mapOtherListKey != NULL;
180
// 	std::cout << "checkTomlNestedList : isOtherListExist = " << isOtherListExist << std::endl;
181
182
1
	const VecDicoValue & vecValue = mapOtherListKey->getVecChild();
183

1
	phoenix_assert(vecValue.size() == 3lu);
184

1
	phoenix_assert(vecValue[0].getValue() == "1");
185

1
	phoenix_assert(vecValue[1].getValue() == "2");
186
187
1
	const DicoValue & subList = vecValue[2];
188
1
	const VecDicoValue & vecSubValue = subList.getVecChild();
189

1
	phoenix_assert(vecSubValue.size() == 2lu);
190

1
	phoenix_assert(vecSubValue[0].getValue() == "3");
191

1
	phoenix_assert(vecSubValue[1].getValue() == "4");
192
1
}
193
194
195
///Check the embeded dico
196
1
void checkTomNestedlMap(){
197
2
	std::string fileContent("[first.second]\nsome_value = 42\n\n");
198
199
2
	std::string tomlFile("test_toml_nested_map.toml");
200

1
	phoenix_assert(saveFileContent(tomlFile, fileContent));
201
202
1
	DicoValue dico;
203

1
	phoenix_assert(parser_toml(dico, tomlFile));
204
// 	std::cout << "checkTomNestedlMap : output DicoValue :" << std::endl;
205
// 	dico.print();
206
207
1
	DicoValue * mapFirst = dico.getMap("first");
208
// 	bool isKeyExist = mapFirst != NULL;
209
// 	std::cout << "checkTomNestedlMap : isKeyExist = " << isKeyExist << std::endl;
210
1
	DicoValue * mapSecond = mapFirst->getMap("second");
211
1
	DicoValue * mapOtherListKey = mapSecond->getMap("some_value");
212

1
	phoenix_assert(checkKeyMapValue(mapOtherListKey, "42"));
213
1
}
214
215
///Check the embeded dico
216
1
void checkTomTable(){
217
2
	std::string fileContent("[[program]]\nname = \"shadok\"\nage = 42\n\n[[program]]\nname = \"gibi\"\nage = 23\n\n");
218
219
2
	std::string tomlFile("test_toml_table.toml");
220

1
	phoenix_assert(saveFileContent(tomlFile, fileContent));
221
222
1
	DicoValue dico;
223

1
	phoenix_assert(parser_toml(dico, tomlFile));
224
// 	std::cout << "checkTomTable : output DicoValue :" << std::endl;
225
// 	dico.print();
226
1
	DicoValue * mapProgram = dico.getMap("program");
227
// 	bool isKeyExist = mapProgram != NULL;
228
// 	std::cout << "checkTomTable : isKeyExist = " << isKeyExist << std::endl;
229
1
	const VecDicoValue & vecProgramValue = mapProgram->getVecChild();
230

1
	phoenix_assert(vecProgramValue.size() == 2lu);
231
1
	const DicoValue * mapProgram0Name = vecProgramValue[0].getMap("name");
232

1
	phoenix_assert(checkKeyMapValue(mapProgram0Name, "\"shadok\""));
233
1
	const DicoValue * mapProgram0Age = vecProgramValue[0].getMap("age");
234

1
	phoenix_assert(checkKeyMapValue(mapProgram0Age, "42"));
235
236
1
	const DicoValue * mapProgram1Name = vecProgramValue[1].getMap("name");
237

1
	phoenix_assert(checkKeyMapValue(mapProgram1Name, "\"gibi\""));
238
1
	const DicoValue * mapProgram1Age = vecProgramValue[1].getMap("age");
239

1
	phoenix_assert(checkKeyMapValue(mapProgram1Age, "23"));
240
1
}
241
242
///Check the compact dico
243
1
void checkTomlCompactDico(){
244
2
	std::string fileContent("[program]\nname = {url = \"someUrl\"}\nage = 42\n\n");
245
246
2
	std::string tomlFile("test_toml_compact_dico.toml");
247

1
	phoenix_assert(saveFileContent(tomlFile, fileContent));
248
249
1
	DicoValue dico;
250

1
	phoenix_assert(parser_toml(dico, tomlFile));
251
// 	std::cout << "checkTomlCompactDico : output DicoValue :" << std::endl;
252
// 	dico.print();
253
1
	DicoValue * mapProgram = dico.getMap("program");
254
// 	bool isKeyExist = mapProgram != NULL;
255
// 	std::cout << "checkTomlCompactDico : isKeyExist = " << isKeyExist << std::endl;
256
1
	const DicoValue * mapName = mapProgram->getMap("name");
257
1
	const DicoValue * mapNameUrl = mapName->getMap("url");
258

1
	phoenix_assert(checkKeyMapValue(mapNameUrl, "\"someUrl\""));
259
1
	const DicoValue * mapAge = mapProgram->getMap("age");
260

1
	phoenix_assert(checkKeyMapValue(mapAge, "42"));
261
1
}
262
263
///Check the compact dico
264
1
void checkTomlCompactDico2(){
265
2
	std::string fileContent("[program]\nname = {url = \"someUrl\", version = \"1.0.0\"}\n# Some Comment\nage = 42\nemptyDico = {}\n\n");
266
2
	std::string tomlFile("test_toml_compact_dico.toml");
267

1
	phoenix_assert(saveFileContent(tomlFile, fileContent));
268
1
	DicoValue dico;
269

1
	phoenix_assert(parser_toml(dico, tomlFile));
270
// 	std::cout << "checkTomlCompactDico : output DicoValue :" << std::endl;
271
// 	dico.print();
272
1
	DicoValue * mapProgram = dico.getMap("program");
273
// 	bool isKeyExist = mapProgram != NULL;
274
// 	std::cout << "checkTomlCompactDico : isKeyExist = " << isKeyExist << std::endl;
275
1
	const DicoValue * mapName = mapProgram->getMap("name");
276
1
	const DicoValue * mapNameUrl = mapName->getMap("url");
277

1
	phoenix_assert(checkKeyMapValue(mapNameUrl, "\"someUrl\""));
278
1
	const DicoValue * mapNameVersion = mapName->getMap("version");
279

1
	phoenix_assert(checkKeyMapValue(mapNameVersion, "\"1.0.0\""));
280
1
	const DicoValue * mapAge = mapProgram->getMap("age");
281

1
	phoenix_assert(checkKeyMapValue(mapAge, "42"));
282
1
}
283
284
///Check if the parsing of a file is OK
285
/**	@param fileName : name of the file to be created
286
 * 	@param fileContent : content of the file
287
 * 	@return true on success, false otherwise
288
*/
289
8
bool checkIsParserOk(const std::string & fileName, const std::string & fileContent){
290
8
	bool b(true);
291
8
	b &= saveFileContent(fileName, fileContent);
292
8
	DicoValue dico;
293
8
	b &= parser_toml(dico, fileName);
294
16
	return b;
295
}
296
297
///Check the YML parser
298
1
void checkParserTomlFail(){
299
1
	DicoValue dico;
300

1
	phoenix_assert(!parser_toml(dico, "UnexsitingFileName.toml"));
301

1
	phoenix_assert(!checkIsParserOk("unextected_token.toml", "=\n"));
302

1
	phoenix_assert(!checkIsParserOk("missing_equal_of_variable.toml", "[package]\nvariable value\n"));
303

1
	phoenix_assert(!checkIsParserOk("missing_value_of_variable.toml", "[package]\nvariable =\n"));
304

1
	phoenix_assert(!checkIsParserOk("unclosed_list.toml", "[package]\nlist = [42\n"));
305

1
	phoenix_assert(!checkIsParserOk("unclosed_empty_list.toml", "[package]\nlist = [\n"));
306

1
	phoenix_assert(!checkIsParserOk("unclosed_dico.toml", "[package]\ndico = {name\n"));
307

1
	phoenix_assert(!checkIsParserOk("unclosed_empty_dico.toml", "[package]\ndico = {\n"));
308

1
	phoenix_assert(checkIsParserOk("some_comment.toml", "# Some value\n[package]\nvalue = 42\n"));
309
1
}
310
311
1
int main(int argc, char** argv){
312
1
	testCheckValue();
313
1
	checkParserTomlFail();
314
1
	checkParserToml();
315
1
	checkTomlList();
316
1
	checkTomlEmptyList();
317
1
	checkTomNestedlList();
318
1
	checkTomNestedlMap();
319
1
	checkTomTable();
320
1
	checkTomlCompactDico();
321
1
	checkTomlCompactDico2();
322
1
	return 0;
323
}
324
325