MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / test_template_cpp

Function test_template_cpp

subprojects/llama.cpp/tests/test-jinja.cpp:1756–1787  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1754}
1755
1756static void test_template_cpp(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect) {
1757 t.test(name, [&tmpl, &vars, &expect](testing & t) {
1758 jinja::lexer lexer;
1759 auto lexer_res = lexer.tokenize(tmpl);
1760
1761 jinja::program ast = jinja::parse_from_tokens(lexer_res);
1762
1763 jinja::context ctx(tmpl);
1764 jinja::global_from_json(ctx, vars, true);
1765
1766 jinja::runtime runtime(ctx);
1767
1768 try {
1769 const jinja::value results = runtime.execute(ast);
1770 auto parts = runtime.gather_string_parts(results);
1771
1772 std::string rendered;
1773 for (const auto & part : parts->as_string().parts) {
1774 rendered += part.val;
1775 }
1776
1777 if (!t.assert_true("Template render mismatch", expect == rendered)) {
1778 t.log("Template: " + json(tmpl).dump());
1779 t.log("Expected: " + json(expect).dump());
1780 t.log("Actual : " + json(rendered).dump());
1781 }
1782 } catch (const jinja::not_implemented_exception & e) {
1783 // TODO @ngxson : remove this when the test framework supports skipping tests
1784 t.log("Skipped: " + std::string(e.what()));
1785 }
1786 });
1787}
1788
1789// keep this in-sync with https://github.com/huggingface/transformers/blob/main/src/transformers/utils/chat_template_utils.py
1790// note: we use SandboxedEnvironment instead of ImmutableSandboxedEnvironment to allow usage of in-place array methods like append() and pop()

Callers 1

test_templateFunction · 0.85

Calls 12

parse_from_tokensFunction · 0.85
global_from_jsonFunction · 0.85
stringClass · 0.85
gather_string_partsMethod · 0.80
assert_trueMethod · 0.80
testMethod · 0.65
logMethod · 0.65
tokenizeMethod · 0.45
executeMethod · 0.45
as_stringMethod · 0.45
dumpMethod · 0.45
whatMethod · 0.45

Tested by

no test coverage detected