-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Corrected state persistence in the json parser
The parser needs a call to restart before it can be used again as otherwise it reports the last calls failure. Added a simple unit test that exhibits the problem
- Loading branch information
thk123
committed
May 16, 2018
1 parent
c817486
commit cd54ad7
Showing
4 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ Author: Daniel Kroening, [email protected] | |
#include <util/json.h> | ||
|
||
int yyjsonparse(); | ||
void yyjsonrestart(FILE *input_file); | ||
|
||
class json_parsert:public parsert | ||
{ | ||
|
@@ -46,6 +47,7 @@ class json_parsert:public parsert | |
virtual void clear() override | ||
{ | ||
stack=stackt(); | ||
yyjsonrestart(nullptr); | ||
} | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/*******************************************************************\ | ||
Module: Example Catch Tests | ||
Author: Diffblue Ltd. | ||
\*******************************************************************/ | ||
|
||
#include <json/json_parser.h> | ||
#include <testing-utils/catch.hpp> | ||
|
||
SCENARIO("Loading JSON files") | ||
{ | ||
null_message_handlert message_handler; | ||
GIVEN("A invalid JSON file and a valid JSON file") | ||
{ | ||
const std::string valid_json_path = "./json/valid.json"; | ||
const std::string invalid_json_path = "./json/invalid.json"; | ||
|
||
WHEN("Loading the invalid JSON file") | ||
{ | ||
jsont invalid_json; | ||
const auto invalid_parse_error = | ||
parse_json(invalid_json_path, message_handler, invalid_json); | ||
THEN("An error state should be returned") | ||
{ | ||
REQUIRE(invalid_parse_error); | ||
REQUIRE(invalid_json.is_null()); | ||
AND_WHEN("Loading the valid JSON file") | ||
{ | ||
jsont valid_json; | ||
const auto valid_parse_error = | ||
parse_json(valid_json_path, message_handler, valid_json); | ||
THEN("The JSON file should be parsed correctly") | ||
{ | ||
REQUIRE_FALSE(valid_parse_error); | ||
REQUIRE(valid_json.is_object()); | ||
REQUIRE(valid_json.object.find("hello") != valid_json.object.end()); | ||
REQUIRE(valid_json.object["hello"].value == "world"); | ||
} | ||
} | ||
} | ||
} | ||
WHEN("Loading the valid JSON file") | ||
{ | ||
jsont valid_json; | ||
const auto valid_parse_error = | ||
parse_json(valid_json_path, message_handler, valid_json); | ||
THEN("The JSON file should be parsed correctly") | ||
{ | ||
REQUIRE_FALSE(valid_parse_error); | ||
REQUIRE(valid_json.is_object()); | ||
REQUIRE(valid_json.object.find("hello") != valid_json.object.end()); | ||
REQUIRE(valid_json.object["hello"].value == "world"); | ||
AND_WHEN("Loading the invalid JSON file") | ||
{ | ||
jsont invalid_json; | ||
const auto invalid_parse_error = | ||
parse_json(invalid_json_path, message_handler, invalid_json); | ||
THEN("An error state should be returned") | ||
{ | ||
REQUIRE(invalid_parse_error); | ||
REQUIRE(invalid_json.is_null()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"hello": "world" | ||
} |