Skip to content

Commit

Permalink
Adding simple bare-bones test for end-to-end integration test for jso…
Browse files Browse the repository at this point in the history
…n validation against auto-generated JSON-schema grammars.
  • Loading branch information
HanClinto committed Jun 6, 2024
1 parent 7672ade commit bf52fc7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ tests/test-grammar-parser: tests/test-grammar-parser.cpp ggml.o llama.o grammar-
$(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
$(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)

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

Expand Down
27 changes: 27 additions & 0 deletions tests/test-grammar-integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ggml.h"
#include "llama.h"
#include "grammar-parser.h"
#include "json-schema-to-grammar.h"
#include "unicode.h"
#include <cassert>
#include <string>
Expand Down Expand Up @@ -365,6 +366,31 @@ empty ::= "blah" | )""";
fprintf(stderr, " ✅︎ Passed\n");
}

static void test_json_schema() {
// Note that this is similar to the regular grammar tests,
// but we convert each json schema to a grammar before parsing.
// Otherwise, this test structure is the same.

test_grammar(
"empty schema",
// Grammar
json_schema_to_grammar(nlohmann::ordered_json::parse(
R"""(
{}
)"""
)),
// Passing strings
{
"{}",
"{\"foo\": \"bar\"}",
},
// Failing strings
{
"",
}
);
}

int main() {
fprintf(stdout, "Running grammar integration tests...\n");
test_simple_grammar();
Expand All @@ -373,6 +399,7 @@ int main() {
test_failure_missing_root();
test_failure_missing_reference();
test_failure_left_recursion();
test_json_schema();
fprintf(stdout, "All tests passed.\n");
return 0;
}

0 comments on commit bf52fc7

Please sign in to comment.