Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Full JSON Test Suite for the example JSON grammar. #37

Merged
merged 3 commits into from Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
31 changes: 21 additions & 10 deletions examples/json.h
Expand Up @@ -41,6 +41,7 @@ extern "C"

typedef enum {
P4_JSONEntry = 1,
P4_JSONValue,
P4_JSONWhitespace,
P4_JSONObject,
P4_JSONObjectItem,
Expand Down Expand Up @@ -183,10 +184,10 @@ P4_Grammar* P4_CreateJSONGrammar() {
if (P4_Ok != P4_AddSequenceWithMembers(grammar, P4_JSONString, 3,
P4_CreateLiteral("\"", true),
P4_CreateZeroOrMore(
P4_CreateChoiceWithMembers(3,
P4_CreateRange(0x20, 0x21),
/* 0x22: " */
P4_CreateRange(0x23, 0x7f),
P4_CreateChoiceWithMembers(4,
P4_CreateRange(0x20, 0x21), /* Can't be 0x22: double quote " */
P4_CreateRange(0x23, 0x5b), /* Can't be 0x5c: escape leading \ */
P4_CreateRange(0x5d, 0x10ffff),
P4_CreateReference(P4_JSONEscape)
)
),
Expand All @@ -201,11 +202,11 @@ P4_Grammar* P4_CreateJSONGrammar() {
P4_CreateLiteral("[", true),
P4_CreateZeroOrOnce(
P4_CreateSequenceWithMembers(2,
P4_CreateReference(P4_JSONEntry),
P4_CreateReference(P4_JSONValue),
P4_CreateZeroOrMore(
P4_CreateSequenceWithMembers(2,
P4_CreateLiteral(",", true),
P4_CreateReference(P4_JSONEntry)
P4_CreateReference(P4_JSONValue)
)
)
)
Expand All @@ -217,7 +218,7 @@ P4_Grammar* P4_CreateJSONGrammar() {
if (P4_Ok != P4_AddSequenceWithMembers(grammar, P4_JSONObjectItem, 3,
P4_CreateReference(P4_JSONString),
P4_CreateLiteral(":", true),
P4_CreateReference(P4_JSONEntry)
P4_CreateReference(P4_JSONValue)
))
goto finalize;

Expand All @@ -238,21 +239,31 @@ P4_Grammar* P4_CreateJSONGrammar() {
))
goto finalize;

if (P4_Ok != P4_AddSequenceWithMembers(grammar, P4_JSONEntry, 3,
if (P4_Ok != P4_AddSequenceWithMembers(grammar, P4_JSONValue, 3,
P4_CreateZeroOrMore(P4_CreateReference(P4_JSONWhitespace)),
P4_CreateChoiceWithMembers(7,
P4_CreateReference(P4_JSONObject),
P4_CreateReference(P4_JSONArray),
P4_CreateReference(P4_JSONString),
P4_CreateReference(P4_JSONNumber),
P4_CreateReference(P4_JSONTrue),
P4_CreateReference(P4_JSONFalse),
P4_CreateReference(P4_JSONNull),
P4_CreateReference(P4_JSONNumber)
P4_CreateReference(P4_JSONNull)
),
P4_CreateZeroOrMore(P4_CreateReference(P4_JSONWhitespace))
))
goto finalize;

if (P4_Ok != P4_SetGrammarRuleFlag(grammar, P4_JSONValue, P4_FLAG_LIFTED))
goto finalize;

if (P4_Ok != P4_AddSequenceWithMembers(grammar, P4_JSONEntry, 3,
P4_CreatePositive(P4_CreateRange(1, 0x10ffff)),
P4_CreateReference(P4_JSONValue),
P4_CreateNegative(P4_CreateRange(1, 0x10ffff))
))
goto finalize;

if (P4_Ok != P4_SetGrammarRuleFlag(grammar, P4_JSONEntry, P4_FLAG_LIFTED))
goto finalize;

Expand Down
2 changes: 1 addition & 1 deletion peppapeg.c
Expand Up @@ -1468,7 +1468,7 @@ P4_GetErrorMessage(P4_Source* source) {
if (source == NULL || source->errmsg == NULL)
return NULL;

return strdup(source->errmsg);
return source->errmsg;
}

P4_PRIVATE(P4_Error)
Expand Down
2 changes: 2 additions & 0 deletions peppapeg.h
Expand Up @@ -1498,6 +1498,8 @@ P4_Error P4_GetError(P4_Source*);
* if (P4_Ok != P4_Parse(grammar, source)) {
* printf("msg=%s\n", P4_GetErrorMessage(source));
* }
*
* The returned value is a reference to the internal string. Don't free it after use.
*/
P4_String P4_GetErrorMessage(P4_Source*);

Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Expand Up @@ -21,6 +21,7 @@ set(

option(ENABLE_VALGRIND "Enable vagrind memory leak report." OFF)


if (ENABLE_VALGRIND)
find_program(VALGRIND valgrind)
message("-- Valgrind found: ${VALGRIND}")
Expand Down Expand Up @@ -55,6 +56,7 @@ endforeach()

add_custom_target(
check
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/json_test_parsing ${CMAKE_CURRENT_BINARY_DIR}/json_test_parsing
COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
DEPENDS ${unity_testcases}
)
12 changes: 12 additions & 0 deletions tests/common.h
Expand Up @@ -54,4 +54,16 @@ void TEST_ADD_WHITESPACE(P4_Grammar* grammar, P4_RuleID id) {
P4_SetExpressionFlag(ws, P4_FLAG_SPACED | P4_FLAG_LIFTED);
}

char* read_file(const char* path) {
FILE* f = fopen (path, "rb");
fseek(f, 0, SEEK_END);
long length = ftell(f);
fseek (f, 0, SEEK_SET);
char* buf = calloc(length+2, sizeof(char));
TEST_ASSERT_EQUAL(length, fread(buf, sizeof(char), length, f));
buf[length] = '\0';
fclose(f);
return buf;
}

# endif
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_number_double_huge_neg_exp.json
@@ -0,0 +1 @@
[123.456e-789]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_number_huge_exp.json
@@ -0,0 +1 @@
[0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_number_neg_int_huge_exp.json
@@ -0,0 +1 @@
[-1e+9999]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_number_pos_double_huge_exp.json
@@ -0,0 +1 @@
[1.5e+9999]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_number_real_neg_overflow.json
@@ -0,0 +1 @@
[-123123e100000]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_number_real_pos_overflow.json
@@ -0,0 +1 @@
[123123e100000]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_number_real_underflow.json
@@ -0,0 +1 @@
[123e-10000000]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_number_too_big_neg_int.json
@@ -0,0 +1 @@
[-123123123123123123123123123123]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_number_too_big_pos_int.json
@@ -0,0 +1 @@
[100000000000000000000]
@@ -0,0 +1 @@
[-237462374673276894279832749832423479823246327846]
@@ -0,0 +1 @@
{"\uDFAA":0}
@@ -0,0 +1 @@
["\uDADA"]
@@ -0,0 +1 @@
["\uD888\u1234"]
Binary file not shown.
@@ -0,0 +1 @@
["日ш�"]
@@ -0,0 +1 @@
["���"]
@@ -0,0 +1 @@
["\uD800\n"]
@@ -0,0 +1 @@
["\uDd1ea"]
@@ -0,0 +1 @@
["\uD800\uD800\n"]
@@ -0,0 +1 @@
["\ud800"]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_string_invalid_surrogate.json
@@ -0,0 +1 @@
["\ud800abc"]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_string_invalid_utf-8.json
@@ -0,0 +1 @@
["�"]
@@ -0,0 +1 @@
["\uDd1e\uD834"]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_string_iso_latin_1.json
@@ -0,0 +1 @@
["�"]
@@ -0,0 +1 @@
["\uDFAA"]
@@ -0,0 +1 @@
["�"]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_string_not_in_unicode_range.json
@@ -0,0 +1 @@
["����"]
@@ -0,0 +1 @@
["��"]
@@ -0,0 +1 @@
["������"]
@@ -0,0 +1 @@
["������"]
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_string_truncated-utf-8.json
@@ -0,0 +1 @@
["��"]
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions tests/json_test_parsing/i_struct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]]]
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_1_true_without_comma.json
@@ -0,0 +1 @@
[1 true]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_a_invalid_utf8.json
@@ -0,0 +1 @@
[a�]
@@ -0,0 +1 @@
["": 1]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_comma_after_close.json
@@ -0,0 +1 @@
[""],
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_comma_and_number.json
@@ -0,0 +1 @@
[,1]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_double_comma.json
@@ -0,0 +1 @@
[1,,2]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_double_extra_comma.json
@@ -0,0 +1 @@
["x",,]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_extra_close.json
@@ -0,0 +1 @@
["x"]]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_extra_comma.json
@@ -0,0 +1 @@
["",]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_incomplete.json
@@ -0,0 +1 @@
["x"
@@ -0,0 +1 @@
[x
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_inner_array_no_comma.json
@@ -0,0 +1 @@
[3[4]]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_invalid_utf8.json
@@ -0,0 +1 @@
[�]
@@ -0,0 +1 @@
[1:2]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_just_comma.json
@@ -0,0 +1 @@
[,]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_just_minus.json
@@ -0,0 +1 @@
[-]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_missing_value.json
@@ -0,0 +1 @@
[ , ""]
3 changes: 3 additions & 0 deletions tests/json_test_parsing/n_array_newlines_unclosed.json
@@ -0,0 +1,3 @@
["a",
4
,1,
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_number_and_comma.json
@@ -0,0 +1 @@
[1,]
@@ -0,0 +1 @@
[1,,]
@@ -0,0 +1 @@
[" a"\f]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_star_inside.json
@@ -0,0 +1 @@
[*]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_array_unclosed.json
@@ -0,0 +1 @@
[""
@@ -0,0 +1 @@
[1,
3 changes: 3 additions & 0 deletions tests/json_test_parsing/n_array_unclosed_with_new_lines.json
@@ -0,0 +1,3 @@
[1,
1
,1
@@ -0,0 +1 @@
[{}
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_incomplete_false.json
@@ -0,0 +1 @@
[fals]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_incomplete_null.json
@@ -0,0 +1 @@
[nul]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_incomplete_true.json
@@ -0,0 +1 @@
[tru]
Binary file not shown.
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_++.json
@@ -0,0 +1 @@
[++1234]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_+1.json
@@ -0,0 +1 @@
[+1]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_+Inf.json
@@ -0,0 +1 @@
[+Inf]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_-01.json
@@ -0,0 +1 @@
[-01]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_-1.0..json
@@ -0,0 +1 @@
[-1.0.]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_-2..json
@@ -0,0 +1 @@
[-2.]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_-NaN.json
@@ -0,0 +1 @@
[-NaN]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_.-1.json
@@ -0,0 +1 @@
[.-1]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_.2e-3.json
@@ -0,0 +1 @@
[.2e-3]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_0.1.2.json
@@ -0,0 +1 @@
[0.1.2]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_0.3e+.json
@@ -0,0 +1 @@
[0.3e+]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_0.3e.json
@@ -0,0 +1 @@
[0.3e]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_0.e1.json
@@ -0,0 +1 @@
[0.e1]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_0_capital_E+.json
@@ -0,0 +1 @@
[0E+]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_0_capital_E.json
@@ -0,0 +1 @@
[0E]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_0e+.json
@@ -0,0 +1 @@
[0e+]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_0e.json
@@ -0,0 +1 @@
[0e]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_1.0e+.json
@@ -0,0 +1 @@
[1.0e+]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_1.0e-.json
@@ -0,0 +1 @@
[1.0e-]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_1.0e.json
@@ -0,0 +1 @@
[1.0e]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_1_000.json
@@ -0,0 +1 @@
[1 000.0]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_1eE2.json
@@ -0,0 +1 @@
[1eE2]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_2.e+3.json
@@ -0,0 +1 @@
[2.e+3]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_2.e-3.json
@@ -0,0 +1 @@
[2.e-3]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_2.e3.json
@@ -0,0 +1 @@
[2.e3]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_9.e+.json
@@ -0,0 +1 @@
[9.e+]
1 change: 1 addition & 0 deletions tests/json_test_parsing/n_number_Inf.json
@@ -0,0 +1 @@
[Inf]