Skip to content

Commit d1c76a7

Browse files
committed
Fixed #3 Add test for removeElement and getLength (array)
1 parent 8e44b09 commit d1c76a7

21 files changed

+253
-150
lines changed

src/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ int main(int argc, char* argv[]) {
1515
WsjcppCore::makeDir(appLogPath);
1616
}
1717
WsjcppYaml yaml;
18-
if (!yaml.loadFromFile("./wsjcpp.yml")) {
19-
WsjcppLog::err(TAG, "Could not read data from file");
18+
std::string sError;
19+
if (!yaml.loadFromFile("./wsjcpp.yml", sError)) {
20+
WsjcppLog::err(TAG, "Could not read data from file: " + sError);
2021
return -1;
2122
}
2223

src/wsjcpp_yaml.cpp

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ std::string WsjcppYamlItem::getForLogFormat() {
575575

576576
WsjcppYamlParsebleLine::WsjcppYamlParsebleLine(int nLine) {
577577
TAG = "WsjcppYamlParsebleLine(line:" + std::to_string(nLine) + ")";
578-
m_nLine = nLine;
578+
m_nLineNumber = nLine;
579579
}
580580

581581
// ---------------------------------------------------------------------
@@ -588,7 +588,7 @@ WsjcppYamlParsebleLine::WsjcppYamlParsebleLine()
588588
// ---------------------------------------------------------------------
589589

590590
int WsjcppYamlParsebleLine::getLineNumber() {
591-
return m_nLine;
591+
return m_nLineNumber;
592592
}
593593

594594
// ---------------------------------------------------------------------
@@ -617,6 +617,12 @@ std::string WsjcppYamlParsebleLine::getComment() {
617617

618618
// ---------------------------------------------------------------------
619619

620+
bool WsjcppYamlParsebleLine::hasComment() {
621+
return m_bHasComment;
622+
}
623+
624+
// ---------------------------------------------------------------------
625+
620626
std::string WsjcppYamlParsebleLine::getName() {
621627
return m_sName;
622628
}
@@ -653,15 +659,29 @@ bool WsjcppYamlParsebleLine::isEmptyValue() {
653659

654660
// ---------------------------------------------------------------------
655661

656-
void WsjcppYamlParsebleLine::parseLine(const std::string &sLine) {
662+
bool WsjcppYamlParsebleLine::isEmptyLine() {
663+
return m_bEmptyLine;
664+
}
665+
666+
// ---------------------------------------------------------------------
667+
668+
bool WsjcppYamlParsebleLine::parseLine(const std::string &sLine, std::string &sError) {
657669
// reset variables
658670
m_bArrayItem = false;
659671
m_sPrefix = "";
660672
m_sComment = "";
661673
m_sName = "";
662674
m_sValue = "";
675+
m_bHasComment = false;
663676
m_bNameHasQuotes = false;
664677
m_bValueHasQuotes = false;
678+
m_bEmptyLine = false;
679+
std::string sLineTrim = sLine;
680+
sLineTrim = WsjcppCore::trim(sLineTrim);
681+
if (sLineTrim.length() == 0) {
682+
m_bEmptyLine = true;
683+
return true;
684+
}
665685

666686
WsjcppYamlParserLineStates state = WsjcppYamlParserLineStates::NO;
667687
for (int i = 0; i < sLine.length(); i++) {
@@ -670,6 +690,7 @@ void WsjcppYamlParsebleLine::parseLine(const std::string &sLine) {
670690
m_sPrefix += c;
671691
} else if (c == '#' && (state == WsjcppYamlParserLineStates::NO || state == WsjcppYamlParserLineStates::VALUE)) {
672692
state = WsjcppYamlParserLineStates::COMMENT;
693+
m_bHasComment = true;
673694
} else if (state == WsjcppYamlParserLineStates::COMMENT) {
674695
if (c != '\r') {
675696
m_sComment += c;
@@ -714,7 +735,8 @@ void WsjcppYamlParsebleLine::parseLine(const std::string &sLine) {
714735
if (state == WsjcppYamlParserLineStates::STRING
715736
|| state == WsjcppYamlParserLineStates::ESCAPING
716737
) {
717-
WsjcppLog::throw_err(TAG, "wrong format");
738+
sError = "Line has wrong format.";
739+
return false;
718740
}
719741

720742
// split name and value
@@ -728,7 +750,7 @@ void WsjcppYamlParsebleLine::parseLine(const std::string &sLine) {
728750
}
729751
}*/
730752

731-
WsjcppCore::trim(m_sName);
753+
m_sName = WsjcppCore::trim(m_sName);
732754
if (m_sName.length() > 0 && m_sName[0] == '"') {
733755
m_bNameHasQuotes = true;
734756
m_sName = removeStringDoubleQuotes(m_sName);
@@ -740,7 +762,8 @@ void WsjcppYamlParsebleLine::parseLine(const std::string &sLine) {
740762
m_sValue = removeStringDoubleQuotes(m_sValue);
741763
}
742764

743-
WsjcppCore::trim(m_sComment);
765+
m_sComment = WsjcppCore::trim(m_sComment);
766+
return true;
744767
}
745768

746769
// ---------------------------------------------------------------------
@@ -776,10 +799,13 @@ std::string WsjcppYamlParsebleLine::removeStringDoubleQuotes(const std::string &
776799
// WsjcppYamlParserStatus
777800

778801
void WsjcppYamlParserStatus::logUnknownLine(const std::string &sPrefix) {
779-
WsjcppLog::warn(sPrefix, "Unknown line (" + std::to_string(placeInFile.getNumberOfLine()) + "): '" + placeInFile.getLine() + "' \n"
780-
+ "Current Intent: " + std::to_string(nIntent) + "\n"
781-
+ "Current Item(line: " + std::to_string(pCurItem->getPlaceInFile().getNumberOfLine()) + "): '" + pCurItem->getPlaceInFile().getLine() + "'"
782-
+ "Current Item(filename: " + pCurItem->getPlaceInFile().getFilename() + "'"
802+
WsjcppLog::warn(sPrefix, "\n"
803+
" error:\n"
804+
" desc: \"unknown_line\"\n"
805+
" line_number: " + std::to_string(pCurItem->getPlaceInFile().getNumberOfLine()) + "\n"
806+
" line: \"" + placeInFile.getLine() + "\"\n"
807+
" intent: " + std::to_string(nIntent) + "\n"
808+
" filename: \"" + pCurItem->getPlaceInFile().getFilename() + "\""
783809
);
784810
}
785811

@@ -788,6 +814,7 @@ void WsjcppYamlParserStatus::logUnknownLine(const std::string &sPrefix) {
788814

789815
WsjcppYaml::WsjcppYaml() {
790816
m_pRoot = new WsjcppYamlItem(nullptr, WsjcppYamlPlaceInFile(), WSJCPP_YAML_ITEM_MAP);
817+
TAG = "WsjcppYaml";
791818
}
792819

793820
// ---------------------------------------------------------------------
@@ -798,12 +825,12 @@ WsjcppYaml::~WsjcppYaml() {
798825

799826
// ---------------------------------------------------------------------
800827

801-
bool WsjcppYaml::loadFromFile(const std::string &sFileName) {
828+
bool WsjcppYaml::loadFromFile(const std::string &sFileName, std::string &sError) {
802829
std::string sTextContent;
803830
if (!WsjcppCore::readTextFile(sFileName, sTextContent)) {
804831
return false;
805832
}
806-
return parse(sFileName, sTextContent);
833+
return parse(sFileName, sTextContent, sError);
807834
}
808835

809836
// ---------------------------------------------------------------------
@@ -818,14 +845,8 @@ bool WsjcppYaml::saveToFile(const std::string &sFileName) {
818845

819846
// ---------------------------------------------------------------------
820847

821-
bool WsjcppYaml::loadFromString(const std::string &sBuffer) {
822-
return false;
823-
}
824-
825-
// ---------------------------------------------------------------------
826-
827-
bool WsjcppYaml::loadFromString(std::string &sBuffer) {
828-
return parse("", sBuffer);
848+
bool WsjcppYaml::loadFromString(const std::string &sBufferName, const std::string &sBuffer, std::string &sError) {
849+
return parse(sBufferName, sBuffer, sError);
829850
}
830851

831852
// ---------------------------------------------------------------------
@@ -864,7 +885,7 @@ std::vector<std::string> WsjcppYaml::splitToLines(const std::string &sBuffer) {
864885

865886
// ---------------------------------------------------------------------
866887

867-
bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer) {
888+
bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer, std::string &sError) {
868889
std::vector<std::string> vLines = this->splitToLines(sBuffer);
869890
WsjcppYamlParserStatus st;
870891
st.pCurItem = m_pRoot; // TODO recreate again new root element
@@ -877,25 +898,32 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer)
877898
// WsjcppLog::info(TAG, "Line(" + std::to_string(nLine) + ") '" + st.sLine + "'");
878899
st.placeInFile.setNumberOfLine(nLine);
879900
st.line = WsjcppYamlParsebleLine(nLine);
880-
st.line.parseLine(st.placeInFile.getLine());
901+
if (!st.line.parseLine(st.placeInFile.getLine(), sError)) {
902+
return false;
903+
}
881904

882905
bool isEmptyName = st.line.isEmptyName();
883906
bool isEmptyValue = st.line.isEmptyValue();
884907
bool isArrayItem = st.line.isArrayItem();
885908
int nLineIntent = st.line.getIntent();
886909
int nDiffIntent = nLineIntent - st.nIntent;
887-
888-
// TODO check comment
889-
/*if (isEmptyName && isEmptyValue && isArrayItem) {
910+
911+
if (st.line.isEmptyLine()) {
912+
WsjcppYamlItem *pItem = new WsjcppYamlItem(
913+
st.pCurItem, st.placeInFile,
914+
WsjcppYamlItemType::WSJCPP_YAML_ITEM_EMPTY
915+
);
916+
st.pCurItem->appendElement(pItem);
890917
continue;
891-
}*/
918+
}
892919

893920
while (nDiffIntent < 0) {
894921
st.pCurItem = st.pCurItem->getParent();
895922
st.nIntent = st.nIntent - 2;
896923
nDiffIntent = nLineIntent - st.nIntent;
897924
if (st.pCurItem == nullptr) {
898-
WsjcppLog::throw_err(TAG, "cur item is nullptr");
925+
sError = "Current item is nullptr";
926+
return false;
899927
}
900928
}
901929

src/wsjcpp_yaml.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,20 @@ class WsjcppYamlParsebleLine {
144144
int getIntent(); // prefix length
145145
bool isArrayItem();
146146
std::string getComment();
147+
bool hasComment();
147148
std::string getName();
148149
bool hasNameDoubleQuotes();
149150
bool isEmptyName();
150151
std::string getValue();
151152
bool hasValueDoubleQuotes();
152153
bool isEmptyValue();
154+
bool isEmptyLine();
153155

154-
void parseLine(const std::string &sLine);
156+
bool parseLine(const std::string &sLine, std::string &sError);
155157

156158
private:
157159
std::string TAG;
158-
int m_nLine;
160+
int m_nLineNumber;
159161

160162
std::string m_sPrefix;
161163
bool m_bArrayItem;
@@ -164,6 +166,8 @@ class WsjcppYamlParsebleLine {
164166
std::string m_sValue;
165167
bool m_bNameHasQuotes;
166168
bool m_bValueHasQuotes;
169+
bool m_bHasComment;
170+
bool m_bEmptyLine;
167171

168172
std::string removeStringDoubleQuotes(const std::string &sValue);
169173
};
@@ -185,10 +189,9 @@ class WsjcppYaml {
185189
public:
186190
WsjcppYaml();
187191
~WsjcppYaml();
188-
bool loadFromFile(const std::string &sFileName);
192+
bool loadFromFile(const std::string &sFileName, std::string &sError);
189193
bool saveToFile(const std::string &sFileName);
190-
bool loadFromString(const std::string &sBuffer);
191-
bool loadFromString(std::string &sBuffer);
194+
bool loadFromString(const std::string &sBufferName, const std::string &sBuffer, std::string &sError);
192195
bool saveToString(std::string &sBuffer);
193196

194197
WsjcppYamlItem *getRoot();
@@ -198,8 +201,9 @@ class WsjcppYaml {
198201
private:
199202
std::string TAG;
200203

204+
// TODO replace to WsjcppCore::split()
201205
std::vector<std::string> splitToLines(const std::string &sBuffer);
202-
bool parse(const std::string &sFileName, const std::string &sBuffer);
206+
bool parse(const std::string &sFileName, const std::string &sBuffer, std::string &sError);
203207
void process_sameIntent_hasName_emptyValue_arrayItem(WsjcppYamlParserStatus &st);
204208
void process_sameIntent_hasName_emptyValue_noArrayItem(WsjcppYamlParserStatus &st);
205209
void process_sameIntent_hasName_hasValue_arrayItem(WsjcppYamlParserStatus &st);

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_comm
4343
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp")
4444
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp")
4545
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_remove_element_for_map.cpp")
46+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_remove_element_in_array.cpp")
4647

4748
# required-libraries
4849
list (APPEND WSJCPP_LIBRARIES "-lpthread")

unit-tests.wsjcpp/src/unit_test_line_parser.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,15 @@ void UnitTestLineParser::executeTest() {
7777

7878
for (int i = 0; i < vTestLines.size(); i++) {
7979
LineTest test = vTestLines[i];
80-
80+
std::string tagline = "{line:" + std::to_string(test.nNumberOfTest) + ": '" + test.sLine + "'}";
81+
8182
WsjcppYamlParsebleLine line(test.nNumberOfTest);
82-
line.parseLine(test.sLine);
83+
std::string sError;
84+
if (!compare(tagline + ", parseLine", line.parseLine(test.sLine, sError), true)) {
85+
WsjcppLog::err(tagline + ", parseLine", sError);
86+
return;
87+
}
8388

84-
std::string tagline = "{line:" + std::to_string(test.nNumberOfTest) + ": '" + test.sLine + "'}";
8589
compare(tagline + ", prefix", line.getPrefix(), test.sPrefix);
8690
compare(tagline + ", arrayitem", line.isArrayItem(), test.isArrayItem);
8791
compare(tagline + ", name", line.getName(), test.sName);

unit-tests.wsjcpp/src/unit_test_remove_element_for_map.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ void UnitTestRemoveElementForMap::executeTest() {
5555
;
5656

5757
WsjcppYaml yaml;
58-
if (!compare("Error parsing", yaml.loadFromString(sTestYaml), true)) {
58+
std::string sError;
59+
if (!compare("Error parsing", yaml.loadFromString("rm_elem_in_map", sTestYaml, sError), true)) {
60+
WsjcppLog::err(TAG, sError);
5961
return;
6062
}
6163

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
#include <wsjcpp_unit_tests.h>
3+
#include <wsjcpp_core.h>
4+
#include <wsjcpp_yaml.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestRemoveElementInArray
8+
9+
class UnitTestRemoveElementInArray : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestRemoveElementInArray();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestRemoveElementInArray)
18+
19+
UnitTestRemoveElementInArray::UnitTestRemoveElementInArray()
20+
: WsjcppUnitTestBase("UnitTestRemoveElementInArray") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestRemoveElementInArray::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestRemoveElementInArray::executeTest() {
33+
std::string sTestYaml =
34+
"# Some comment 1\n"
35+
" \n"
36+
"arr1: \n"
37+
"\n"
38+
" - name: i1\n"
39+
" var2: 2\n"
40+
" - name: i2\n"
41+
" - name: i3\n"
42+
"\n" // empty line
43+
;
44+
45+
WsjcppYaml yaml;
46+
std::string sError;
47+
if (!compare("Error parsing", yaml.loadFromString("rm_elem_in_arr", sTestYaml, sError), true)) {
48+
WsjcppLog::err(TAG, sError);
49+
return;
50+
}
51+
52+
53+
WsjcppYamlItem *pArr1 = yaml.getRoot()->getElement("arr1");
54+
compare("arr1 len", pArr1->getLength(), 3);
55+
compare("arr1 name0 ", pArr1->getElement(0)->getElement("name")->getValue(), "i1");
56+
compare("arr1 name1 ", pArr1->getElement(1)->getElement("name")->getValue(), "i2");
57+
compare("arr1 name2 ", pArr1->getElement(2)->getElement("name")->getValue(), "i3");
58+
59+
pArr1->removeElement(1);
60+
61+
compare("arr1 len", pArr1->getLength(), 2);
62+
compare("arr1 name0 ", pArr1->getElement(0)->getElement("name")->getValue(), "i1");
63+
compare("arr1 name1 ", pArr1->getElement(1)->getElement("name")->getValue(), "i3");
64+
}
65+
66+
// ---------------------------------------------------------------------
67+
68+
bool UnitTestRemoveElementInArray::doAfterTest() {
69+
// do somethig after test
70+
return true;
71+
}
72+
73+

unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ void UnitTestYamlParserAll::executeTest() {
5959
;
6060

6161
WsjcppYaml yaml;
62-
if (!compare("Error parsing", yaml.loadFromString(sTestYaml), true)) {
62+
std::string sError;
63+
if (!compare("Error parsing", yaml.loadFromString("parse_all", sTestYaml, sError), true)) {
64+
WsjcppLog::err(TAG, sError);
6365
return;
6466
}
6567

0 commit comments

Comments
 (0)