Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions common/jinja/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ lexer_result lexer::tokenize(const std::string & source) {

// Check for numbers following the unary operator
std::string num = consume_while(is_integer);
if (pos < src.size() && src[pos] == '.' && pos + 1 < src.size() && is_integer(src[pos + 1])) {
++pos; // Consume '.'
std::string frac = consume_while(is_integer);
num += "." + frac;
}
std::string value = std::string(1, ch) + num;
token::type t = num.empty() ? token::unary_operator : token::numeric_literal;
// JJ_DEBUG("consumed unary operator or numeric literal: '%s'", value.c_str());
Expand Down
1 change: 1 addition & 0 deletions common/jinja/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ const func_builtins & value_bool_t::get_builtins() const {
bool val = args.get_pos(0)->as_bool();
return mk_val<value_string>(val ? "True" : "False");
}},
{"tojson", tojson},
};
return builtins;
}
Expand Down
60 changes: 60 additions & 0 deletions tests/test-jinja.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,66 @@ static void test_literals(testing & t) {
json::object(),
"1"
);

test_template(t, "integer|abs",
"{{ -42 | abs }}",
json::object(),
"42"
);

test_template(t, "integer|float",
"{{ 42 | float }}",
json::object(),
"42.0"
);

test_template(t, "integer|tojson",
"{{ 42 | tojson }}",
json::object(),
"42"
);

test_template(t, "float|abs",
"{{ -3.14 | abs }}",
json::object(),
"3.14"
);

test_template(t, "float|int",
"{{ 3.14 | int }}",
json::object(),
"3"
);

test_template(t, "float|tojson",
"{{ 3.14 | tojson }}",
json::object(),
"3.14"
);

test_template(t, "string|tojson",
"{{ 'hello' | tojson }}",
json::object(),
"\"hello\""
);

test_template(t, "boolean|int",
"{{ true | int }}",
json::object(),
"1"
);

test_template(t, "boolean|float",
"{{ true | float }}",
json::object(),
"1.0"
);

test_template(t, "boolean|tojson",
"{{ true | tojson }}",
json::object(),
"true"
);
}

static void test_comments(testing & t) {
Expand Down
Loading