8000 Implement 'random', 'trim', 'title', 'upper', 'lower' and 'wordcount' filters by flexferrum · Pull Request #18 · jinja2cpp/Jinja2Cpp · GitHub
[go: up one dir, main page]

Skip to content

Implement 'random', 'trim', 'title', 'upper', 'lower' and 'wordcount' filters #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Bugfix and filter tests
  • Loading branch information
flexferrum committed May 25, 2018
commit 53fcda1308c488b940661980e4cbd9096e9f5b59
11 changes: 6 additions & 5 deletions src/filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ struct ValueConverterImpl : visitors::BaseVisitor<>
result = InternalValue(static_cast<double>(val));
break;
case ValueConverter::AbsMode:
result = InternalValue(abs(val));
result = InternalValue(static_cast<int64_t>(abs(val)));
break;
case ValueConverter::ToIntMode:
case ValueConverter::RoundMode:
Expand Down Expand Up @@ -818,9 +818,9 @@ struct ValueConverterImpl : visitors::BaseVisitor<>
const string* m_str;
};

struct MapAdapter : public IListAccessor
struct Map2ListAdapter : public IListAccessor
{
MapAdapter(const MapAdapter* map)
Map2ListAdapter(const MapAdapter* map)
: m_map(map)
{
}
Expand Down Expand Up @@ -849,7 +849,8 @@ struct ValueConverterImpl : visitors::BaseVisitor<>
case ValueConverter::ToIntMode:
{
char* endBuff = nullptr;
int64_t dblVal = strtoll(val.c_str(), &endBuff, static_cast<int>(GetAs<int64_t>(m_params.base)));
int base = static_cast<int>(GetAs<int64_t>(m_params.base));
int64_t dblVal = strtoll(val.c_str(), &endBuff, base);
if (*endBuff != 0)
result = m_params.defValule;
else
Expand Down Expand Up @@ -910,7 +911,7 @@ struct ValueConverterImpl : visitors::BaseVisitor<>
InternalValue operator()(const MapAdapter& val) const
{
if (m_params.mode == ValueConverter::ToListMode)
return ListAdapter([adapter = MapAdapter(&val)]() {return &adapter;});
return ListAdapter([adapter = Map2ListAdapter(&val)]() {return &adapter;});

return InternalValue();
}
Expand Down
43 changes: 43 additions & 0 deletions test/filters_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,46 @@ INSTANTIATE_TEST_CASE_P(UrlEncode, FilterGenericTest, ::testing::Values(
InputOutputPair{"'Hello World' | urlencode", "Hello+World"},
InputOutputPair{"'! # $ & ( ) * + , / : ; = ? @ [ ] %' | urlencode", "%21+%23+%24+%26+%28+%29+%2A+%2B+%2C+%2F+%3A+%3B+%3D+%3F+%40+%5B+%5D+%25"}
));

INSTANTIATE_TEST_CASE_P(Abs, FilterGenericTest, ::testing::Values(
InputOutputPair{"10 | abs", "10"},
InputOutputPair{"-10 | abs", "10"},
InputOutputPair{"10.5 | abs", "10.5"},
InputOutputPair{"-10.5 | abs", "10.5"},
InputOutputPair{"'10' | abs", ""}
));

INSTANTIATE_TEST_CASE_P(Round, FilterGenericTest, ::testing::Values(
InputOutputPair{"10 | round", "10"},
InputOutputPair{"10 | round(1)", "10"},
InputOutputPair{"10.5 | round", "11"},
InputOutputPair{"10.4 | round", "10"},
InputOutputPair{"10.6 | round", "11"},
InputOutputPair{"-10.5 | round", "-11"},
InputOutputPair{"-10.4 | round", "-10"},
InputOutputPair{"-10.6 | round", "-11"},
InputOutputPair{"10.5 | round(method='ceil')", "11"},
InputOutputPair{"10.5 | round(method='floor')", "10"},
InputOutputPair{"-10.5 | round(method='ceil')", "-11"},
InputOutputPair{"-10.5 | round(method='floor')", "-10"},
InputOutputPair{"10.44 | round(1)", "10.4"},
InputOutputPair{"10.46 | round(precision=1)", "10.5"}
));

INSTANTIATE_TEST_CASE_P(Convert, FilterGenericTest, ::testing::Values(
InputOutputPair{"10 | int", "10"},
InputOutputPair{"10 | float", "10"},
InputOutputPair{"10.5 | int", "10"},
InputOutputPair{"'10.4' | float | pprint", "10.4"},
InputOutputPair{"'100;4' | float(10.4) | pprint", "10.4"},
InputOutputPair{"'100;4' | int(10) | pprint", "10"},
InputOutputPair{"'100' | int(10) | pprint", "100"},
InputOutputPair{"'0x100' | int(10, 0) | pprint", "256"},
InputOutputPair{"'0100' | int(10, 0) | pprint", "64"},
InputOutputPair{"'100' | int(10, base=10) | pprint", "100"},
InputOutputPair{"'100' | int(10, base=2) | pprint", "4"},
InputOutputPair{"'100' | int(10, base=8) | pprint", "64"},
InputOutputPair{"'100' | int(10, base=16) | pprint", "256"},
InputOutputPair{"'100' | list | pprint", "['1', '0', '0']"},
InputOutputPair{"{'name'='itemName', 'val'='itemValue'} | list | pprint", "['name': 'itemName', 'val': 'itemValue']"}
));
0