| 1948 | } |
| 1949 | |
| 1950 | static void test_template_cpp(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect) { |
| 1951 | t.test(name, [&tmpl, &vars, &expect](testing & t) { |
| 1952 | jinja::lexer lexer; |
| 1953 | auto lexer_res = lexer.tokenize(tmpl); |
| 1954 | |
| 1955 | jinja::program ast = jinja::parse_from_tokens(lexer_res); |
| 1956 | |
| 1957 | jinja::context ctx(tmpl); |
| 1958 | jinja::global_from_json(ctx, vars, true); |
| 1959 | |
| 1960 | jinja::runtime runtime(ctx); |
| 1961 | |
| 1962 | try { |
| 1963 | const jinja::value results = runtime.execute(ast); |
| 1964 | auto parts = runtime.gather_string_parts(results); |
| 1965 | |
| 1966 | std::string rendered; |
| 1967 | for (const auto & part : parts->as_string().parts) { |
| 1968 | rendered += part.val; |
| 1969 | } |
| 1970 | |
| 1971 | if (!t.assert_true("Template render mismatch", expect == rendered)) { |
| 1972 | t.log("Template: " + json(tmpl).dump()); |
| 1973 | t.log("Expected: " + json(expect).dump()); |
| 1974 | t.log("Actual : " + json(rendered).dump()); |
| 1975 | } |
| 1976 | } catch (const jinja::not_implemented_exception & e) { |
| 1977 | // TODO @ngxson : remove this when the test framework supports skipping tests |
| 1978 | t.log("Skipped: " + std::string(e.what())); |
| 1979 | } |
| 1980 | }); |
| 1981 | } |
| 1982 | |
| 1983 | // keep this in-sync with https://github.com/huggingface/transformers/blob/main/src/transformers/utils/chat_template_utils.py |
| 1984 | // note: we use SandboxedEnvironment instead of ImmutableSandboxedEnvironment to allow usage of in-place array methods like append() and pop() |
no test coverage detected