Skip to content

Commit

Permalink
Add more json schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan-wanna-M committed Oct 10, 2024
1 parent 75f7636 commit f6ee688
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/snapshots/snap_test_grammar_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,33 @@
start_name ::= string;
'''

snapshots['test_schema_with_reference_to_number 1'] = '''integer ::= #"-?(0|[1-9]\\\\d*)";
number ::= #"-?(0|[1-9]\\\\d*)(\\\\.\\\\d+)?([eE][+-]?\\\\d+)?";
string ::= #\'"([^\\\\\\\\"\\u0000-\\u001f]|\\\\\\\\["\\\\\\\\bfnrt/]|\\\\\\\\u[0-9A-Fa-f]{4})*"\';
boolean ::= "true"|"false";
null ::= "null";
array ::= array_begin (json_value (comma json_value)*)? array_end;
object ::= object_begin (string colon json_value (comma string colon json_value)*)? object_end;
json_value ::= number|string|boolean|null|array|object;
comma ::= #"[ \t
\r]*,[ \t
\r]*";
colon ::= #"[ \t
\r]*:[ \t
\r]*";
object_begin ::= #"\\\\{[ \t
\r]*";
object_end ::= #"[ \t
\r]*\\\\}";
array_begin ::= #"\\\\[[ \t
\r]*";
array_end ::= #"[ \t
\r]*\\\\]";
start ::= object_begin \'"mainProperty"\' colon start_mainProperty comma \'"numberReference"\' colon start_numberReference object_end;
start_numberReference ::= number;
start_mainProperty ::= string;
'''

snapshots['test_schema_with_top_level_anyOf 1'] = '''integer ::= #"-?(0|[1-9]\\\\d*)";
number ::= #"-?(0|[1-9]\\\\d*)(\\\\.\\\\d+)?([eE][+-]?\\\\d+)?";
string ::= #\'"([^\\\\\\\\"\\u0000-\\u001f]|\\\\\\\\["\\\\\\\\bfnrt/]|\\\\\\\\u[0-9A-Fa-f]{4})*"\';
Expand Down
24 changes: 24 additions & 0 deletions tests/test_grammar_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,30 @@ def test_schema_with_embedded_schema(snapshot):
result = JsonExtractor("start", None, combined_schema, lambda x: x).kbnf_definition
snapshot.assert_match(result)

def test_schema_with_reference_to_number(snapshot):
schema = {
"$id": "https://example.com/schemas/number-reference.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"mainProperty": {"type": "string"},
"numberReference": {"$ref": "#/$defs/numberDef"}
},
"required": ["mainProperty", "numberReference"],
"$defs": {
"numberDef": {
"type": "number",
"minimum": 0,
"maximum": 100
}
}
}

combined_schema = json_schema.create_schema(schema)
result = JsonExtractor("start", None, combined_schema, lambda x: x).kbnf_definition
snapshot.assert_match(result)


def test_schema_with_top_level_array(snapshot):
schema = {
"$id": "https://example.com/schemas/top-level-array.json",
Expand Down

0 comments on commit f6ee688

Please sign in to comment.