From 039426a696432f176c25a97003b9c5217e861b69 Mon Sep 17 00:00:00 2001 From: Xuan Son Nguyen Date: Fri, 21 Aug 2026 22:29:03 +0200 Subject: [PATCH] migrate tests --- common/json-schema-to-grammar.cpp | 8 ++++++-- common/json.h | 5 +++++ tests/test-chat-auto-parser.cpp | 2 +- tests/test-chat-peg-parser.cpp | 22 +++++++++++----------- tests/test-chat-template.cpp | 8 ++++---- tests/test-chat.cpp | 4 ++-- tests/test-grammar-integration.cpp | 4 ++-- tests/test-jinja.cpp | 4 ++-- tests/test-json-schema-to-grammar.cpp | 8 ++++---- tests/test-model-resolution.cpp | 6 +++--- 10 files changed, 40 insertions(+), 31 deletions(-) diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp index 0912cdd5d8..1d06768440 100644 --- a/common/json-schema-to-grammar.cpp +++ b/common/json-schema-to-grammar.cpp @@ -919,7 +919,11 @@ public: return _add_rule(rule_name, _resolve_ref(schema["$ref"].get())); } if (schema.contains("oneOf") || schema.contains("anyOf")) { - std::vector alt_schemas = schema.contains("oneOf") ? schema["oneOf"].get>() : schema["anyOf"].get>(); + const json & alts = schema.contains("oneOf") ? schema.at("oneOf") : schema.at("anyOf"); + std::vector alt_schemas; + for (const auto & alt : alts) { + alt_schemas.push_back(alt); + } return _add_rule(rule_name, _generate_union_rule(name, alt_schemas)); } if (schema_type.is_array()) { @@ -1238,7 +1242,7 @@ std::string json_schema_to_grammar(const common_json & schema, bool force_gbnf) (void)force_gbnf; #endif // LLAMA_USE_LLGUIDANCE return build_grammar([&](const common_grammar_builder & callbacks) { - auto copy = common_json_raw(schema); + auto copy = schema; callbacks.resolve_refs(copy); callbacks.add_schema("", copy); }); diff --git a/common/json.h b/common/json.h index 1b2f8adbbc..4a9f5a82f7 100644 --- a/common/json.h +++ b/common/json.h @@ -173,6 +173,11 @@ class common_json { return contains(key) ? at(key).get() : std::string(def); } + // a JSON default needs no get(), it is already the right type + common_json value(const std::string & key, const common_json & def) const { + return contains(key) ? at(key) : def; + } + void assign(const common_json_value & val); void set(const common_json_item & item); void push_back(const common_json_value & val); diff --git a/tests/test-chat-auto-parser.cpp b/tests/test-chat-auto-parser.cpp index 2209dcac84..f98e281086 100644 --- a/tests/test-chat-auto-parser.cpp +++ b/tests/test-chat-auto-parser.cpp @@ -2157,7 +2157,7 @@ static void test_tagged_args_with_embedded_quotes(testing & t) { for (const auto & tool_def : tools) { if (!tool_def.contains("function")) { continue; } const auto & function = tool_def.at("function"); - std::string name = function.at("name"); + std::string name = function.at("name").get(); const auto & params = function.at("parameters"); if (!params.contains("properties") || !params.at("properties").is_object()) { continue; } diff --git a/tests/test-chat-peg-parser.cpp b/tests/test-chat-peg-parser.cpp index 3ab7a67b6a..432b67870c 100644 --- a/tests/test-chat-peg-parser.cpp +++ b/tests/test-chat-peg-parser.cpp @@ -11,9 +11,9 @@ #include #include -#include "nlohmann/json.hpp" +#include "json.h" -using json = nlohmann::ordered_json; +using json = common_json; static json create_tools(); static void test_example_native(testing & t); @@ -400,13 +400,13 @@ static void test_example_qwen3_coder(testing & t) { std::vector tool_parsers; for (const auto & def : tools) { auto function = def.at("function"); - std::string name = function.at("name"); + std::string name = function.at("name").get(); auto parameters = function.at("parameters"); auto properties = parameters.at("properties"); std::set required_properties; if (function.contains("required")) { - function.at("required").get_to(required_properties); + required_properties = function.at("required").get>(); } std::vector arg_parsers; @@ -661,8 +661,8 @@ void test_command7_parser_compare(testing & t) { "5. Provide a detailed cost breakdown that includes accommodation, transportation, meals, and entry fees " "to attractions."; - std::vector> tool_calls = { - { "call_0", "plan_trip", nlohmann::json::parse(R"({ + std::vector> tool_calls = { + { "call_0", "plan_trip", common_json::parse(R"({ "destination": "Japan", "duration": 14, "budget": 4000, @@ -686,16 +686,16 @@ void test_command7_parser_compare(testing & t) { if (!tool_calls.empty()) { tokens.emplace_back("<|START_ACTION|>"); - auto json = nlohmann::json::array(); + auto json = common_json::array(); for (const auto & tc : tool_calls) { - auto tc_json = nlohmann::json::object(); + auto tc_json = common_json::object(); tc_json["tool_call_id"] = std::get<0>(tc); tc_json["tool_name"] = std::get<1>(tc); tc_json["parameters"] = std::get<2>(tc); json.push_back(tc_json); } - auto tokenized = simple_tokenize(json.dump(-1, ' ', true)); + auto tokenized = simple_tokenize(json.dump(-1)); tokens.insert(tokens.end(), tokenized.begin(), tokenized.end()); tokens.emplace_back("<|END_ACTION|>"); @@ -737,7 +737,7 @@ static void test_prefix_tool_names(testing & t) { { { "arg1", { { "type", "integer" } } }, } }, - { "required", { "arg1" } }, + { "required", json::array({ "arg1" }) }, } }, } } }; @@ -757,7 +757,7 @@ static void test_prefix_tool_names(testing & t) { { "arg1", { { "type", "integer" } } }, { "arg2", { { "type", "integer" } } }, } }, - { "required", { "arg1" } }, + { "required", json::array({ "arg1" }) }, } }, } } }; diff --git a/tests/test-chat-template.cpp b/tests/test-chat-template.cpp index 6a6292cd01..bcc574afe9 100644 --- a/tests/test-chat-template.cpp +++ b/tests/test-chat-template.cpp @@ -7,7 +7,7 @@ #include #include -#include +#include "json.h" #undef NDEBUG #include @@ -20,7 +20,7 @@ #include "jinja/lexer.h" #include "jinja/caps.h" -using json = nlohmann::ordered_json; +using json = common_json; static int main_automated_tests(void); @@ -304,8 +304,8 @@ void run_single(const std::string& contents, json input, bool use_common, bool d if (input.contains("eos_token")) { eos_token = input["eos_token"].get(); } - nlohmann::ordered_json msgs_json = input["messages"]; - nlohmann::ordered_json tools_json = input["tools"]; + common_json msgs_json = input["messages"]; + common_json tools_json = input["tools"]; auto messages = common_chat_msgs_parse_oaicompat(msgs_json); auto tools = common_chat_tools_parse_oaicompat(tools_json); auto output = format_using_common(contents, bos_token, eos_token, messages, tools); diff --git a/tests/test-chat.cpp b/tests/test-chat.cpp index c4670da853..7918f0ffcf 100644 --- a/tests/test-chat.cpp +++ b/tests/test-chat.cpp @@ -19,12 +19,12 @@ #include #include #include -#include +#include "json.h" #include #include #include -using json = nlohmann::ordered_json; +using json = common_json; static std::ostream & operator<<(std::ostream & os, const common_chat_msg_diff & diff) { os << "{ content_delta: " << diff.content_delta << "; "; diff --git a/tests/test-grammar-integration.cpp b/tests/test-grammar-integration.cpp index 4d5d13dd0d..eb4b7c78f5 100644 --- a/tests/test-grammar-integration.cpp +++ b/tests/test-grammar-integration.cpp @@ -7,13 +7,13 @@ #include "../src/unicode.h" #include "../src/llama-grammar.h" -#include +#include "json.h" #include #include #include -using json = nlohmann::ordered_json; +using json = common_json; static llama_grammar * build_grammar_with_root(const std::string & grammar_str, const char * grammar_root) { return llama_grammar_init_impl(nullptr, grammar_str.c_str(), grammar_root, false, nullptr, 0, nullptr, 0); diff --git a/tests/test-jinja.cpp b/tests/test-jinja.cpp index 1eb2a062b7..4b68d55383 100644 --- a/tests/test-jinja.cpp +++ b/tests/test-jinja.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include "json.h" #include "subproc.h" #include "jinja/runtime.h" @@ -14,7 +14,7 @@ #include "testing.h" -using json = nlohmann::ordered_json; +using json = common_json; static void test_template(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect); diff --git a/tests/test-json-schema-to-grammar.cpp b/tests/test-json-schema-to-grammar.cpp index 74b57cf1b6..214dbe1993 100755 --- a/tests/test-json-schema-to-grammar.cpp +++ b/tests/test-json-schema-to-grammar.cpp @@ -6,7 +6,7 @@ #include "../src/llama-grammar.h" -#include +#include "json.h" #include #include @@ -1442,7 +1442,7 @@ static void test_resolves_to_string() { auto test = [](const std::string & name, const std::string & schema_str, bool expected) { fprintf(stderr, "- %s\n", name.c_str()); common_schema_info info; - auto schema = nlohmann::ordered_json::parse(schema_str); + auto schema = common_json::parse(schema_str); info.resolve_refs(schema); bool result = info.resolves_to_string(schema); if (result != expected) { @@ -1517,7 +1517,7 @@ int main() { test_all("C++", [](const TestCase & tc) { try { - tc.verify(json_schema_to_grammar(nlohmann::ordered_json::parse(tc.schema), true)); + tc.verify(json_schema_to_grammar(common_json::parse(tc.schema), true)); tc.verify_status(SUCCESS); } catch (const std::invalid_argument & ex) { fprintf(stderr, "Error: %s\n", ex.what()); @@ -1531,7 +1531,7 @@ int main() { auto run = [](const TestCase & tc) { fprintf(stderr, "- %s\n", tc.name.c_str()); try { - tc.verify(json_schema_to_grammar(nlohmann::ordered_json::parse(tc.schema), true)); + tc.verify(json_schema_to_grammar(common_json::parse(tc.schema), true)); tc.verify_status(SUCCESS); } catch (const std::invalid_argument & ex) { fprintf(stderr, "Error: %s\n", ex.what()); diff --git a/tests/test-model-resolution.cpp b/tests/test-model-resolution.cpp index 2437eeec60..80b02fb70a 100644 --- a/tests/test-model-resolution.cpp +++ b/tests/test-model-resolution.cpp @@ -9,7 +9,7 @@ #include "http.h" #include "log.h" -#include +#include "json.h" #include #include @@ -55,7 +55,7 @@ static const char * COMMIT = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; static void serve_repos(httplib::Server & server) { server.Get(R"(/api/models/(.+)/refs)", [](const httplib::Request & req, httplib::Response & res) { if (g_repos.count(req.matches[1])) { - res.set_content(nlohmann::json{{"branches", {{{"name", "main"}, {"targetCommit", COMMIT}}}}}.dump(), + res.set_content(common_json{{"branches", {{{"name", "main"}, {"targetCommit", COMMIT}}}}}.dump(), "application/json"); } else { res.status = 404; @@ -66,7 +66,7 @@ static void serve_repos(httplib::Server & server) { res.status = 404; return; } - auto files = nlohmann::json::array(); + auto files = common_json::array(); size_t i = 0; for (const auto & p : g_repos[req.matches[1]]) { char oid[41];