Skip to content

Commit bf52fc7

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 7672ade commit bf52fc7

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
@@ -987,7 +987,7 @@ tests/test-grammar-parser: tests/test-grammar-parser.cpp ggml.o llama.o grammar-
987987
$(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
988988
$(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
989989

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

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>
@@ -365,6 +366,31 @@ empty ::= "blah" | )""";
365366
fprintf(stderr, " ✅︎ Passed\n");
366367
}
367368

369+
static void test_json_schema() {
370+
// Note that this is similar to the regular grammar tests,
371+
// but we convert each json schema to a grammar before parsing.
372+
// Otherwise, this test structure is the same.
373+
374+
test_grammar(
375+
"empty schema",
376+
// Grammar
377+
json_schema_to_grammar(nlohmann::ordered_json::parse(
378+
R"""(
379+
{}
380+
)"""
381+
)),
382+
// Passing strings
383+
{
384+
"{}",
385+
"{\"foo\": \"bar\"}",
386+
},
387+
// Failing strings
388+
{
389+
"",
390+
}
391+
);
392+
}
393+
368394
int main() {
369395
fprintf(stdout, "Running grammar integration tests...\n");
370396
test_simple_grammar();
@@ -373,6 +399,7 @@ int main() {
373399
test_failure_missing_root();
374400
test_failure_missing_reference();
375401
test_failure_left_recursion();
402+
test_json_schema();
376403
fprintf(stdout, "All tests passed.\n");
377404
return 0;
378405
}

0 commit comments

Comments
 (0)