| 1754 | } |
| 1755 | |
| 1756 | static 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() |
no test coverage detected