From 321bec7366f96a67bb2ab8bc2ea3784919781d57 Mon Sep 17 00:00:00 2001 From: MM Zeeman Date: Tue, 7 Jun 2022 11:25:12 +0200 Subject: [PATCH] Compile false as false (#33) --- src/filter_template_compiler_test.erl | 2 ++ src/template_compiler_expr.erl | 2 +- test/template_compiler_expr_SUITE.erl | 13 +++++++++++++ test/test-data/literals.tpl | 8 ++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/test-data/literals.tpl diff --git a/src/filter_template_compiler_test.erl b/src/filter_template_compiler_test.erl index dc8b46a..639e7eb 100644 --- a/src/filter_template_compiler_test.erl +++ b/src/filter_template_compiler_test.erl @@ -30,6 +30,8 @@ template_compiler_test(Value, _Context) when is_integer(Value) -> template_compiler_test(Value, _Context) -> 2*z_convert:to_integer(iolist_to_binary(Value)). +template_compiler_test(A, <<"w">>, _Context) -> + io_lib:format("~w", [A]); template_compiler_test(A, B, _Context) -> [ z_convert:to_binary(A), diff --git a/src/template_compiler_expr.erl b/src/template_compiler_expr.erl index 833f1fe..4073596 100644 --- a/src/template_compiler_expr.erl +++ b/src/template_compiler_expr.erl @@ -33,7 +33,7 @@ compile({ast, Ast}, _CState, Ws) -> compile(true, _CState, Ws) -> {Ws, erl_syntax:atom(true)}; compile(false, _CState, Ws) -> - {Ws, erl_syntax:atom(true)}; + {Ws, erl_syntax:atom(false)}; compile(undefined, _CState, Ws) -> {Ws, erl_syntax:atom(undefined)}; compile({string_literal, SrcPos, Text}, _CState, Ws) when is_binary(Text) -> diff --git a/test/template_compiler_expr_SUITE.erl b/test/template_compiler_expr_SUITE.erl index 66f35c6..2366d4f 100644 --- a/test/template_compiler_expr_SUITE.erl +++ b/test/template_compiler_expr_SUITE.erl @@ -20,6 +20,7 @@ groups() -> [expr_test ,expr_op_test ,expr_filter + ,expr_literals ,expr_nested ,expr_autoid ,expr_map @@ -70,6 +71,18 @@ expr_filter(_Config) -> <<"*1:2*1:3:2*">> = iolist_to_binary(Bin2), ok. +expr_literals(_Config) -> + {ok, Bin1} = template_compiler:render("literals.tpl", #{}, [], undefined), + <<"true +false +undefined +42 +<<104,101,108,108,111,32,119,111,114,108,100>> +atom +[a,b,c] +#{<<97>> => 1,<<98>> => 2}\n">>, iolist_to_binary(Bin1), + ok. + expr_nested(_Config) -> Vars1 = #{ a => #{ diff --git a/test/test-data/literals.tpl b/test/test-data/literals.tpl new file mode 100644 index 0000000..7d9ec5b --- /dev/null +++ b/test/test-data/literals.tpl @@ -0,0 +1,8 @@ +{{ true | template_compiler_test:"w" }} +{{ false | template_compiler_test:"w" }} +{{ undefined | template_compiler_test:"w" }} +{{ 42 | template_compiler_test:"w" }} +{{ "hello world" | template_compiler_test:"w" }} +{{ `atom` | template_compiler_test:"w" }} +{{ [`a`, `b`, `c`] | template_compiler_test:"w" }} +{{ %{ a : 1, b: 2 } | template_compiler_test:"w" }}