Skip to content

Commit cd54ad7

Browse files
author
thk123
committed
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
1 parent c817486 commit cd54ad7

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

src/json/json_parser.h

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Author: Daniel Kroening, [email protected]
1717
#include <util/json.h>
1818

1919
int yyjsonparse();
20+
void yyjsonrestart(FILE *input_file);
2021

2122
class json_parsert:public parsert
2223
{
@@ -46,6 +47,7 @@ class json_parsert:public parsert
4647
virtual void clear() override
4748
{
4849
stack=stackt();
50+
yyjsonrestart(nullptr);
4951
}
5052
};
5153

unit/json/invalid.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo

unit/json/json_parser.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*******************************************************************\
2+
3+
Module: Example Catch Tests
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#include <json/json_parser.h>
10+
#include <testing-utils/catch.hpp>
11+
12+
SCENARIO("Loading JSON files")
13+
{
14+
null_message_handlert message_handler;
15+
GIVEN("A invalid JSON file and a valid JSON file")
16+
{
17+
const std::string valid_json_path = "./json/valid.json";
18+
const std::string invalid_json_path = "./json/invalid.json";
19+
20+
WHEN("Loading the invalid JSON file")
21+
{
22+
jsont invalid_json;
23+
const auto invalid_parse_error =
24+
parse_json(invalid_json_path, message_handler, invalid_json);
25+
THEN("An error state should be returned")
26+
{
27+
REQUIRE(invalid_parse_error);
28+
REQUIRE(invalid_json.is_null());
29+
AND_WHEN("Loading the valid JSON file")
30+
{
31+
jsont valid_json;
32+
const auto valid_parse_error =
33+
parse_json(valid_json_path, message_handler, valid_json);
34+
THEN("The JSON file should be parsed correctly")
35+
{
36+
REQUIRE_FALSE(valid_parse_error);
37+
REQUIRE(valid_json.is_object());
38+
REQUIRE(valid_json.object.find("hello") != valid_json.object.end());
39+
REQUIRE(valid_json.object["hello"].value == "world");
40+
}
41+
}
42+
}
43+
}
44+
WHEN("Loading the valid JSON file")
45+
{
46+
jsont valid_json;
47+
const auto valid_parse_error =
48+
parse_json(valid_json_path, message_handler, valid_json);
49+
THEN("The JSON file should be parsed correctly")
50+
{
51+
REQUIRE_FALSE(valid_parse_error);
52+
REQUIRE(valid_json.is_object());
53+
REQUIRE(valid_json.object.find("hello") != valid_json.object.end());
54+
REQUIRE(valid_json.object["hello"].value == "world");
55+
AND_WHEN("Loading the invalid JSON file")
56+
{
57+
jsont invalid_json;
58+
const auto invalid_parse_error =
59+
parse_json(invalid_json_path, message_handler, invalid_json);
60+
THEN("An error state should be returned")
61+
{
62+
REQUIRE(invalid_parse_error);
63+
REQUIRE(invalid_json.is_null());
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}

unit/json/valid.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hello": "world"
3+
}

0 commit comments

Comments
 (0)