Skip to content

Commit b02dc1d

Browse files
author
LE, Duc-Anh
committed
CLDEPARSER-19 #time 30m
1 parent 25c1341 commit b02dc1d

File tree

8 files changed

+85
-39
lines changed

8 files changed

+85
-39
lines changed

CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ install(TARGETS cldeparser-static cldeparser
7373
set(EXTRA_DIR "Extra")
7474
set(TEST_SOURCE
7575
Test/main.cpp
76-
Test/case01.hpp
77-
Test/JsonParserInstance.cpp Test/JsonParserInstance.h Test/JsonTestFixture.cpp Test/JsonTestFixture.h)
76+
Test/Case00_FeatureTest.hpp
77+
Test/JsonParserInstance.cpp
78+
Test/JsonParserInstance.h
79+
Test/JsonTestFixture.cpp
80+
Test/JsonTestFixture.h
81+
Test/Case01_OtherTest.hpp)
7882

7983
include_directories(${EXTRA_DIR}/gtest-1.7.0/include)
8084
LINK_DIRECTORIES(${EXTRA_DIR}/gtest-1.7.0/lib)

Source/Parsing/Json/JsonValue.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ namespace CLDEParser {
1212

1313
switch ((JsonValueType) id) {
1414

15-
case JsonValueType::null:
15+
case JsonValueType::Null:
1616
return "null";
1717

18-
case JsonValueType::string:
18+
case JsonValueType::String:
1919
return strValue;
2020

21-
case JsonValueType::integer:
21+
case JsonValueType::Integer:
2222
return std::to_string(intValue);
2323

24-
case JsonValueType::real:
24+
case JsonValueType::Real:
2525
return std::to_string(doubleValue);
2626

27-
case JsonValueType::boolean:
27+
case JsonValueType::Boolean:
2828
return std::to_string(boolValue);
2929

30-
case JsonValueType::entity:
30+
case JsonValueType::Entity:
3131
return sptrJsonEntity->CopyToString();
3232
}
3333
}

Source/Parsing/Json/JsonValue.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ namespace CLDEParser {
1313
namespace Json {
1414

1515
enum class JsonValueType {
16-
null = 0,
17-
string = 1,
18-
integer = 2,
19-
real = 3,
20-
boolean = 4,
21-
entity = 5
16+
Null = 0,
17+
String = 1,
18+
Integer = 2,
19+
Real = 3,
20+
Boolean = 4,
21+
Entity = 5
2222
};
2323

2424
struct JsonValue : public Common::IPrintable {
@@ -31,6 +31,9 @@ namespace CLDEParser {
3131

3232
// IPrintable
3333
std::string CopyToString() const override;
34+
35+
// Locals
36+
JsonValueType Type() { return (JsonValueType) id; }
3437
};
3538

3639
using SPtrJsonValue = std::shared_ptr<JsonValue>;

Source/Parsing/Json/JsonValueFactory.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,48 @@ namespace CLDEParser {
1010

1111
SPtrJsonValue JsonValueFactory::CreateInteger(std::string const &value) {
1212
SPtrJsonValue sptrValue = std::make_shared<JsonValue>();
13-
sptrValue->id = (int) JsonValueType::integer;
13+
sptrValue->id = (int) JsonValueType::Integer;
1414
sptrValue->intValue = std::stoi(value);
1515
return sptrValue;
1616
}
1717

1818
SPtrJsonValue JsonValueFactory::CreateDouble(std::string const &value) {
1919
SPtrJsonValue sptrValue = std::make_shared<JsonValue>();
20-
sptrValue->id = (int) JsonValueType::real;
20+
sptrValue->id = (int) JsonValueType::Real;
2121
sptrValue->doubleValue = std::stod(value);
2222
return sptrValue;
2323
}
2424

2525
SPtrJsonValue JsonValueFactory::CreateString(std::string const &value) {
2626
SPtrJsonValue sptrValue = std::make_shared<JsonValue>();
27-
sptrValue->id = (int) JsonValueType::string;
27+
sptrValue->id = (int) JsonValueType::String;
2828
sptrValue->strValue = value;
2929
return sptrValue;
3030
}
3131

3232
SPtrJsonValue JsonValueFactory::CreateBooleanTrue() {
3333
SPtrJsonValue sptrValue = std::make_shared<JsonValue>();
34-
sptrValue->id = (int) JsonValueType::boolean;
34+
sptrValue->id = (int) JsonValueType::Boolean;
3535
sptrValue->boolValue = true;
3636
return sptrValue;
3737
}
3838

3939
SPtrJsonValue JsonValueFactory::CreateBooleanFalse() {
4040
SPtrJsonValue sptrValue = std::make_shared<JsonValue>();
41-
sptrValue->id = (int) JsonValueType::boolean;
41+
sptrValue->id = (int) JsonValueType::Boolean;
4242
sptrValue->boolValue = false;
4343
return sptrValue;
4444
}
4545

4646
SPtrJsonValue JsonValueFactory::CreateNull() {
4747
SPtrJsonValue sptrValue = std::make_shared<JsonValue>();
48-
sptrValue->id = (int) JsonValueType::null;
48+
sptrValue->id = (int) JsonValueType::Null;
4949
return sptrValue;
5050
}
5151

5252
SPtrJsonValue JsonValueFactory::CreateEntityValue(SPtrJsonEntity const &sptrJsonEntity) {
5353
SPtrJsonValue sptrValue = std::make_shared<JsonValue>();
54-
sptrValue->id = (int) JsonValueType::entity;
54+
sptrValue->id = (int) JsonValueType::Entity;
5555
sptrValue->sptrJsonEntity = sptrJsonEntity;
5656
return sptrValue;
5757
}

Test/Case00_FeatureTest.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef CLDEPARSER_TEST_CASE00_FEATURETEST_HPP
2+
#define CLDEPARSER_TEST_CASE00_FEATURETEST_HPP
3+
4+
#include "JsonTestFixture.h"
5+
6+
namespace CLDEParser {
7+
namespace Test {
8+
9+
TEST_F(JsonTestFixture, empty_object) {
10+
std::string example{};
11+
auto sptrJson = parserInstance().Parse(example);
12+
ASSERT_TRUE(sptrJson.get() == nullptr);
13+
}
14+
15+
TEST_F(JsonTestFixture, simple_string) {
16+
std::string example{"{\"v\": \"1\" }"};
17+
auto sptrJson = parserInstance().Parse(example);
18+
auto sptrJsonObject = std::dynamic_pointer_cast<Parsing::Json::JsonObject>(sptrJson);
19+
ASSERT_TRUE(sptrJson.get() != nullptr);
20+
ASSERT_TRUE(sptrJson->Type() == Parsing::Json::JsonEntityType::Object);
21+
ASSERT_TRUE(sptrJsonObject->GetValue("v")->Type() == Parsing::Json::JsonValueType::String);
22+
ASSERT_TRUE(sptrJsonObject->GetValue("v")->strValue.compare("1") == 0);
23+
}
24+
25+
// TODO: space tester
26+
27+
TEST_F(JsonTestFixture, simple_int) {
28+
std::string example{"{\"v\": 1 }"};
29+
auto sptrJson = parserInstance().Parse(example);
30+
auto sptrJsonObject = std::dynamic_pointer_cast<Parsing::Json::JsonObject>(sptrJson);
31+
ASSERT_TRUE(sptrJson.get() != nullptr);
32+
ASSERT_TRUE(sptrJson->Type() == Parsing::Json::JsonEntityType::Object);
33+
ASSERT_TRUE(sptrJsonObject->GetValue("v")->Type() == Parsing::Json::JsonValueType::Integer);
34+
ASSERT_TRUE(sptrJsonObject->GetValue("v")->intValue == 1);
35+
}
36+
}
37+
}
38+
39+
#endif//CLDEPARSER_TEST_CASE00_FEATURETEST_HPP

Test/Case01_OtherTest.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Created by LE, Duc Anh on 8/25/15.
3+
//
4+
5+
#ifndef CLDEPARSER_TEST_CASE01_OTHERTEST_HPP
6+
#define CLDEPARSER_TEST_CASE01_OTHERTEST_HPP
7+
8+
#include "JsonTestFixture.h"
9+
10+
namespace CLDEParser {
11+
namespace Test {
12+
13+
14+
}
15+
}
16+
17+
#endif //CLDEPARSER_TEST_CASE01_OTHERTEST_HPP

Test/case01.hpp

Lines changed: 0 additions & 17 deletions
This file was deleted.

Test/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <iostream>
66
#include <gtest/gtest.h>
7-
#include "case01.hpp"
7+
#include "Case00_FeatureTest.hpp"
88

99
int main(int argc, char *argv[]) {
1010
::testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)