Skip to content

Commit 2b174dd

Browse files
committed
Adding simple bare-bones test for end-to-end integration test for json validation against auto-generated JSON-schema grammars.
1 parent 557b653 commit 2b174dd

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ tests/test-grammar-parser: tests/test-grammar-parser.cpp ggml.o llama.o grammar-
10511051
$(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
10521052
$(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
10531053

1054-
tests/test-grammar-integration: tests/test-grammar-integration.cpp ggml.o llama.o grammar-parser.o $(OBJS)
1054+
tests/test-grammar-integration: tests/test-grammar-integration.cpp json-schema-to-grammar.o ggml.o llama.o grammar-parser.o $(OBJS)
10551055
$(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
10561056
$(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
10571057

tests/test-grammar-integration.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "ggml.h"
88
#include "llama.h"
99
#include "grammar-parser.h"
10+
#include "json-schema-to-grammar.h"
1011
#include "unicode.h"
1112
#include <cassert>
1213
#include <string>
@@ -468,6 +469,31 @@ empty ::= "blah" | )""";
468469
fprintf(stderr, " ✅︎ Passed\n");
469470
}
470471

472+
static void test_json_schema() {
473+
// Note that this is similar to the regular grammar tests,
474+
// but we convert each json schema to a grammar before parsing.
475+
// Otherwise, this test structure is the same.
476+
477+
test_grammar(
478+
"empty schema",
479+
// Grammar
480+
json_schema_to_grammar(nlohmann::ordered_json::parse(
481+
R"""(
482+
{}
483+
)"""
484+
)),
485+
// Passing strings
486+
{
487+
"{}",
488+
"{\"foo\": \"bar\"}",
489+
},
490+
// Failing strings
491+
{
492+
"",
493+
}
494+
);
495+
}
496+
471497
int main() {
472498
fprintf(stdout, "Running grammar integration tests...\n");
473499
test_simple_grammar();
@@ -477,6 +503,7 @@ int main() {
477503
test_failure_missing_root();
478504
test_failure_missing_reference();
479505
test_failure_left_recursion();
506+
test_json_schema();
480507
fprintf(stdout, "All tests passed.\n");
481508
return 0;
482509
}

0 commit comments

Comments
 (0)