提交 869035a6 编写于 作者: N Niels

added compliance test cases

上级 9f9d293b
......@@ -3631,7 +3631,7 @@ class basic_json
static const unsigned char yybm[] =
{
0, 64, 64, 64, 64, 64, 64, 64,
64, 96, 96, 64, 64, 96, 64, 64,
64, 32, 32, 64, 64, 32, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
96, 64, 0, 64, 64, 64, 64, 64,
......@@ -3914,11 +3914,26 @@ basic_json_parser_25:
basic_json_parser_26:
yyaccept = 0;
yych = *(m_marker = ++m_cursor);
if (yych <= 0x00)
if (yych <= '\n')
{
if (yych <= 0x00)
{
goto basic_json_parser_19;
}
if (yych <= 0x08)
{
goto basic_json_parser_31;
}
goto basic_json_parser_19;
}
goto basic_json_parser_31;
else
{
if (yych == '\r')
{
goto basic_json_parser_19;
}
goto basic_json_parser_31;
}
basic_json_parser_27:
++m_cursor;
{
......@@ -3939,7 +3954,7 @@ basic_json_parser_31:
{
goto basic_json_parser_30;
}
if (yych <= 0x00)
if (yych <= '\r')
{
goto basic_json_parser_32;
}
......@@ -4395,7 +4410,7 @@ basic_json_parser_59:
m_buffer.erase(0, static_cast<size_t>(offset_start));
std::string line;
std::getline(*m_stream, line);
m_buffer += line;
m_buffer += "\n" + line; // add line with newline symbol
m_content = reinterpret_cast<const lexer_char_t*>(m_buffer.c_str());
m_start = m_content;
......@@ -4641,6 +4656,9 @@ basic_json_parser_59:
return result;
}
// no comma is expected here
unexpect(lexer::token_type::value_separator);
// otherwise: parse key-value pairs
do
{
......@@ -4707,6 +4725,9 @@ basic_json_parser_59:
return result;
}
// no comma is expected here
unexpect(lexer::token_type::value_separator);
// otherwise: parse values
do
{
......@@ -4796,11 +4817,8 @@ basic_json_parser_59:
default:
{
std::string error_msg = "parse error - unexpected \'";
error_msg += m_lexer.get_token();
error_msg += "\' (";
error_msg += lexer::token_type_name(last_token) + ")";
throw std::invalid_argument(error_msg);
// the last token was unexpected
unexpect(last_token);
}
}
......@@ -4830,6 +4848,18 @@ basic_json_parser_59:
}
}
inline void unexpect(typename lexer::token_type t) const
{
if (t == last_token)
{
std::string error_msg = "parse error - unexpected \'";
error_msg += m_lexer.get_token();
error_msg += "\' (";
error_msg += lexer::token_type_name(last_token) + ")";
throw std::invalid_argument(error_msg);
}
}
private:
/// levels of recursion
int depth = 0;
......
......@@ -3669,7 +3669,7 @@ class basic_json
// string
quotation_mark = [\"];
escape = [\\];
unescaped = [^\"\\\000];
unescaped = [^\"\\\000\t\n\r];
single_escaped = [\"\\/bfnrt];
unicode_escaped = [u][0-9a-fA-F]{4};
escaped = escape (single_escaped | unicode_escaped);
......@@ -3701,7 +3701,7 @@ class basic_json
m_buffer.erase(0, static_cast<size_t>(offset_start));
std::string line;
std::getline(*m_stream, line);
m_buffer += line;
m_buffer += "\n" + line; // add line with newline symbol
m_content = reinterpret_cast<const lexer_char_t*>(m_buffer.c_str());
m_start = m_content;
......@@ -3947,6 +3947,9 @@ class basic_json
return result;
}
// no comma is expected here
unexpect(lexer::token_type::value_separator);
// otherwise: parse key-value pairs
do
{
......@@ -4013,6 +4016,9 @@ class basic_json
return result;
}
// no comma is expected here
unexpect(lexer::token_type::value_separator);
// otherwise: parse values
do
{
......@@ -4102,11 +4108,8 @@ class basic_json
default:
{
std::string error_msg = "parse error - unexpected \'";
error_msg += m_lexer.get_token();
error_msg += "\' (";
error_msg += lexer::token_type_name(last_token) + ")";
throw std::invalid_argument(error_msg);
// the last token was unexpected
unexpect(last_token);
}
}
......@@ -4136,6 +4139,18 @@ class basic_json
}
}
inline void unexpect(typename lexer::token_type t) const
{
if (t == last_token)
{
std::string error_msg = "parse error - unexpected \'";
error_msg += m_lexer.get_token();
error_msg += "\' (";
error_msg += lexer::token_type_name(last_token) + ")";
throw std::invalid_argument(error_msg);
}
}
private:
/// levels of recursion
int depth = 0;
......
"A JSON payload should be an object or array, not a string."
\ No newline at end of file
{"Extra value after close": true} "misplaced quoted value"
\ No newline at end of file
{"Illegal expression": 1 + 2}
\ No newline at end of file
{"Illegal invocation": alert()}
\ No newline at end of file
{"Numbers cannot have leading zeroes": 013}
\ No newline at end of file
{"Numbers cannot be hex": 0x14}
\ No newline at end of file
["Illegal backslash escape: \x15"]
\ No newline at end of file
[\naked]
\ No newline at end of file
["Illegal backslash escape: \017"]
\ No newline at end of file
[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]
\ No newline at end of file
{"Missing colon" null}
\ No newline at end of file
["Unclosed array"
\ No newline at end of file
{"Double colon":: null}
\ No newline at end of file
{"Comma instead of colon", null}
\ No newline at end of file
["Colon instead of comma": false]
\ No newline at end of file
["Bad value", truth]
\ No newline at end of file
['single quote']
\ No newline at end of file
[" tab character in string "]
\ No newline at end of file
["tab\ character\ in\ string\ "]
\ No newline at end of file
["line
break"]
\ No newline at end of file
["line\
break"]
\ No newline at end of file
[0e]
\ No newline at end of file
{unquoted_key: "keys must be quoted"}
\ No newline at end of file
[0e+]
\ No newline at end of file
[0e+-1]
\ No newline at end of file
{"Comma instead if closing brace": true,
\ No newline at end of file
["mismatch"}
\ No newline at end of file
["extra comma",]
\ No newline at end of file
["double extra comma",,]
\ No newline at end of file
[ , "<-- missing value"]
\ No newline at end of file
["Comma after the close"],
\ No newline at end of file
["Extra close"]]
\ No newline at end of file
{"Extra comma": true,}
\ No newline at end of file
[
"JSON Test Pattern pass1",
{"object with 1 member":["array with 1 element"]},
{},
[],
-42,
true,
false,
null,
{
"integer": 1234567890,
"real": -9876.543210,
"e": 0.123456789e-12,
"E": 1.234567890E+34,
"": 23456789012E66,
"zero": 0,
"one": 1,
"space": " ",
"quote": "\"",
"backslash": "\\",
"controls": "\b\f\n\r\t",
"slash": "/ & \/",
"alpha": "abcdefghijklmnopqrstuvwyz",
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
"digit": "0123456789",
"0123456789": "digit",
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
"true": true,
"false": false,
"null": null,
"array":[ ],
"object":{ },
"address": "50 St. James Street",
"url": "http://www.JSON.org/",
"comment": "// /* <!-- --",
"# -- --> */": " ",
" s p a c e d " :[1,2 , 3
,
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
"quotes": "&#34; \u0022 %22 0x22 034 &#x22;",
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
: "A key can be any string"
},
0.5 ,98.6
,
99.44
,
1066,
1e1,
0.1e1,
1e-1,
1e00,2e+00,2e-00
,"rosebud"]
\ No newline at end of file
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
\ No newline at end of file
{
"JSON Test Pattern pass3": {
"The outermost value": "must be an object or array.",
"In this test": "It is an object."
}
}
......@@ -7398,16 +7398,17 @@ TEST_CASE("parser class")
// exotic test cases for full coverage
{
{
std::stringstream ss;
ss << "\"\\u000\n1\"";
CHECK(json::parser(ss).parse().get<json::string_t>() == "\x01");
}
{
std::stringstream ss;
ss << "\"\\u00\n01\"";
CHECK(json::parser(ss).parse().get<json::string_t>() == "\x01");
}
// that one got illegal
//{
// std::stringstream ss;
// ss << "\"\\u000\n1\"";
// CHECK(json::parser(ss).parse().get<json::string_t>() == "\x01");
//}
//{
// std::stringstream ss;
// ss << "\"\\u00\n01\"";
// CHECK(json::parser(ss).parse().get<json::string_t>() == "\x01");
//}
}
CHECK(json::parser("\"\\u0001\"").parse().get<json::string_t>() == "\x01");
......@@ -8439,6 +8440,73 @@ TEST_CASE("concepts")
}
}
TEST_CASE("JSON compliance")
{
// test cases are from http://json.org/JSON_checker/
SECTION("expected failures")
{
for (auto filename :
{
//"test/json_tests/fail1.json",
"test/json_tests/fail2.json",
"test/json_tests/fail3.json",
"test/json_tests/fail4.json",
"test/json_tests/fail5.json",
"test/json_tests/fail6.json",
"test/json_tests/fail7.json",
"test/json_tests/fail8.json",
"test/json_tests/fail9.json",
"test/json_tests/fail10.json",
"test/json_tests/fail11.json",
"test/json_tests/fail12.json",
"test/json_tests/fail13.json",
"test/json_tests/fail14.json",
"test/json_tests/fail15.json",
"test/json_tests/fail16.json",
"test/json_tests/fail17.json",
//"test/json_tests/fail18.json",
"test/json_tests/fail19.json",
"test/json_tests/fail20.json",
"test/json_tests/fail21.json",
"test/json_tests/fail22.json",
"test/json_tests/fail23.json",
"test/json_tests/fail24.json",
"test/json_tests/fail25.json",
"test/json_tests/fail26.json",
"test/json_tests/fail27.json",
"test/json_tests/fail28.json",
"test/json_tests/fail29.json",
"test/json_tests/fail30.json",
"test/json_tests/fail31.json",
"test/json_tests/fail32.json",
"test/json_tests/fail33.json"
})
{
CAPTURE(filename);
json j;
std::ifstream f(filename);
CHECK_THROWS_AS(j << f, std::invalid_argument);
}
}
SECTION("expected passes")
{
for (auto filename :
{
"test/json_tests/pass1.json",
"test/json_tests/pass2.json",
"test/json_tests/pass3.json"
})
{
CAPTURE(filename);
json j;
std::ifstream f(filename);
CHECK_NOTHROW(j << f);
}
}
}
TEST_CASE("regression tests")
{
SECTION("issue #60 - Double quotation mark is not parsed correctly")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册