Skip to content

Commit b80c5d2

Browse files
committed
Moved tests yaml to tests
1 parent 2632ece commit b80c5d2

File tree

6 files changed

+111
-108
lines changed

6 files changed

+111
-108
lines changed

src/main.cpp

Lines changed: 1 addition & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,6 @@
44
#include "wsjcpp_yaml.h"
55
#include "fallen.h"
66

7-
int g_nErrors = 0;
8-
std::string g_sTestYaml =
9-
"# Some comment 1\n"
10-
"test10: one\n"
11-
"test20: two # some comment 2\n"
12-
"\n" // empty line
13-
"array30:\n"
14-
" - one31 # this field for test array30 \n"
15-
" - two32\n"
16-
" - three33\n"
17-
"\n" // empty line
18-
"array40: # some comment 3\n"
19-
" - \"one#1\" # some comment\n"
20-
" # some comment\n"
21-
" - \"two#2\"\n"
22-
"array50: # some comment 3\n"
23-
" - p1: test501\n"
24-
" p2: test502\n"
25-
" - p1: test503\n"
26-
" p2: test504\n"
27-
"\n" // empty line
28-
"map60: # some comment 4\n"
29-
" test70: opa1\n"
30-
" test80: opa2\n"
31-
;
32-
33-
void check(std::string sTestName, std::string sGotValue, std::string sExpectedValue) {
34-
if (sGotValue != sExpectedValue) {
35-
Log::err(sTestName, "Expected value: '" + sExpectedValue + "', but got: '" + sGotValue + "'");
36-
g_nErrors++;
37-
return;
38-
}
39-
Log::ok(sTestName, "fine");
40-
};
41-
42-
void check(std::string sTestName, bool bGotValue, bool bExpectedValue) {
43-
std::string sGotValue = bGotValue ? "yes" : "no";
44-
std::string sExpectedValue = bExpectedValue ? "yes" : "no";
45-
if (sGotValue != sExpectedValue) {
46-
Log::err(sTestName, "Expected value: '" + sExpectedValue + "', but got: '" + sGotValue + "'");
47-
g_nErrors++;
48-
return;
49-
}
50-
Log::ok(sTestName, "fine");
51-
};
52-
53-
547
int main(int argc, char* argv[]) {
558
std::string TAG = "MAIN";
569
std::string appName = std::string(WSJCPP_NAME);
@@ -61,67 +14,7 @@ int main(int argc, char* argv[]) {
6114
Fallen::makeDir(appLogPath);
6215
}
6316

64-
// tests
65-
66-
67-
WSJCppYAML yaml;
68-
if (yaml.loadFromString(g_sTestYaml)) {
69-
Log::throw_err(TAG, "Error parsing");
70-
return -1;
71-
}
72-
std::string sSaved = "";
73-
if (yaml.saveToString(sSaved)) {
74-
Log::info(TAG, "\n>>>>\n" + sSaved);
75-
}
76-
77-
WSJCppYAMLItem *pItem = nullptr;
78-
check("test10", yaml.getRoot()->getElement("test10")->getValue(), "one");
79-
check("test20", yaml.getRoot()->getElement("test20")->getValue(), "two");
80-
81-
pItem = yaml.getRoot()->getElement("array30");
82-
check("array30_length", pItem->getLength(), 3);
83-
pItem = yaml.getRoot()->getElement("array30")->getElement(0);
84-
check("test30_value", pItem->getValue(), "one31");
85-
check("test30_comment", pItem->getComment(), " this field for test array30 ");
86-
pItem = yaml.getRoot()->getElement("array30")->getElement(1);
87-
check("test40_value", pItem->getValue(), "two32");
88-
check("test40_comment", pItem->getComment(), "");
89-
90-
pItem = yaml.getRoot()->getElement("array40");
91-
check("array40_length", pItem->getLength(), 2);
92-
93-
pItem = yaml.getRoot()->getElement("array50");
94-
check("array50_length", pItem->getLength(), 1);
95-
96-
pItem = yaml.getRoot()->getElement("map60")->getElement("test70");
97-
check("test70_value", pItem->getValue(), "opa1");
98-
99-
pItem = yaml.getRoot()->getElement("map60")->getElement("test80");
100-
check("test80_comment", pItem->getValue(), "opa2");
101-
102-
std::string sTestExpectedYaml =
103-
"# Some comment 1\n"
104-
"test: one\n"
105-
"test2: two # some comment 2\n"
106-
"\n" // empty line
107-
"array:\n"
108-
" - one\n"
109-
" - two\n"
110-
"\n" // empty line
111-
"array2: # some comment 3\n"
112-
" - \"one#1\"\n"
113-
" - \"two#2\"\n"
114-
"\n" // empty line
115-
"map: # some comment 4\n"
116-
" test3: 1\n"
117-
" test4: 2\n"
118-
"\n" // empty line
119-
;
120-
121-
sSaved = "";
122-
if (yaml.saveToString(sSaved)) {
123-
check("yaml_save", sSaved, sTestExpectedYaml);
124-
}
17+
// TODO load and check yaml format
12518

12619
return 0;
12720
}

src/wsjcpp_test.cpp

Whitespace-only changes.

src/wsjcpp_test.h

Whitespace-only changes.

unit-tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ list (APPEND WSJCPP_SOURCES "./src/unit_tests.h")
2929
list (APPEND WSJCPP_SOURCES "./src/unit_tests.cpp")
3030
list (APPEND WSJCPP_SOURCES "./src/unit_test_line_parser.h")
3131
list (APPEND WSJCPP_SOURCES "./src/unit_test_line_parser.cpp")
32+
list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser.h")
33+
list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser.cpp")
3234
list (APPEND WSJCPP_SOURCES "./src/main.cpp")
3335

3436
include_directories(${WSJCPP_INCLUDE_DIRS})
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "unit_test_yaml_parser.h"
2+
#include <vector>
3+
#include <iostream>
4+
#include <fallen.h>
5+
#include <wsjcpp_yaml.h>
6+
7+
REGISTRY_UNIT_TEST(UnitTestYamlParser)
8+
9+
UnitTestYamlParser::UnitTestYamlParser()
10+
: UnitTestBase("UnitTestYamlParser") {
11+
//
12+
}
13+
14+
// ---------------------------------------------------------------------
15+
16+
void UnitTestYamlParser::init() {
17+
// nothing
18+
}
19+
20+
// ---------------------------------------------------------------------
21+
22+
bool UnitTestYamlParser::run() {
23+
24+
std::string g_sTestYaml =
25+
"# Some comment 1\n"
26+
"test10: one\n"
27+
"test20: two # some comment 2\n"
28+
"\n" // empty line
29+
"array30:\n"
30+
" - one31 # this field for test array30 \n"
31+
" - two32\n"
32+
" - three33\n"
33+
"\n" // empty line
34+
"array40: # some comment 3\n"
35+
" - \"one#1\" # some comment\n"
36+
" # some comment\n"
37+
" - \"two#2\"\n"
38+
"array50: # some comment 3\n"
39+
" - p1: test501\n"
40+
" p2: test502\n"
41+
" - p1: test503\n"
42+
" p2: test504\n"
43+
"\n" // empty line
44+
"map60: # some comment 4\n"
45+
" test70: opa1\n"
46+
" test80: opa2\n"
47+
;
48+
49+
bool bTestSuccess = true;
50+
51+
WSJCppYAML yaml;
52+
if (yaml.loadFromString(g_sTestYaml)) {
53+
Log::throw_err(TAG, "Error parsing");
54+
return -1;
55+
}
56+
std::string sSaved = "";
57+
if (yaml.saveToString(sSaved)) {
58+
Log::info(TAG, "\n>>>>\n" + sSaved);
59+
}
60+
61+
WSJCppYAMLItem *pItem = nullptr;
62+
compareS(bTestSuccess, "test10", yaml.getRoot()->getElement("test10")->getValue(), "two");
63+
compareS(bTestSuccess, "test20", yaml.getRoot()->getElement("test20")->getValue(), "two");
64+
65+
pItem = yaml.getRoot()->getElement("array30");
66+
compareN(bTestSuccess, "array30_length", pItem->getLength(), 3);
67+
68+
pItem = yaml.getRoot()->getElement("array30")->getElement(0);
69+
compareS(bTestSuccess, "test30_value", pItem->getValue(), "one31");
70+
compareS(bTestSuccess, "test30_comment", pItem->getComment(), " this field for test array30 ");
71+
pItem = yaml.getRoot()->getElement("array30")->getElement(1);
72+
compareS(bTestSuccess, "test40_value", pItem->getValue(), "two32");
73+
compareS(bTestSuccess, "test40_comment", pItem->getComment(), "");
74+
75+
pItem = yaml.getRoot()->getElement("array40");
76+
compareN(bTestSuccess, "array40_length", pItem->getLength(), 2);
77+
78+
pItem = yaml.getRoot()->getElement("array50");
79+
compareN(bTestSuccess, "array50_length", pItem->getLength(), 1);
80+
81+
pItem = yaml.getRoot()->getElement("map60")->getElement("test70");
82+
compareS(bTestSuccess, "test70_value", pItem->getValue(), "opa1");
83+
84+
pItem = yaml.getRoot()->getElement("map60")->getElement("test80");
85+
compareS(bTestSuccess, "test80_comment", pItem->getValue(), "opa2");
86+
87+
sSaved = "";
88+
if (yaml.saveToString(sSaved)) {
89+
compareS(bTestSuccess, "yaml_save", sSaved, g_sTestYaml);
90+
} else {
91+
Log::err(TAG, "Could not save to string");
92+
bTestSuccess = false;
93+
}
94+
return bTestSuccess;
95+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UNIT_TEST_YAML_PARSER_H
2+
#define UNIT_TEST_YAML_PARSER_H
3+
4+
#include <unit_tests.h>
5+
6+
class UnitTestYamlParser : public UnitTestBase {
7+
public:
8+
UnitTestYamlParser();
9+
virtual void init();
10+
virtual bool run();
11+
};
12+
13+
#endif // UNIT_TEST_YAML_PARSER_H

0 commit comments

Comments
 (0)