Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix #3 : $apply f val$ can be written $val:f$
apply at the moment accepts only one argument so also the ":" works with only one argument
  • Loading branch information
Filippo Pacini committed Oct 16, 2011
1 parent 3ed259f commit e1c6be3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
13 changes: 12 additions & 1 deletion src/sgte_parse.erl
Expand Up @@ -221,7 +221,18 @@ parse_key([H|T], _InEncoding, Line) ->
P = until(fun is_dollar/1),
case P([H|T]) of
{ok, Token, LinesParsed, Rest} ->
{ok, {attribute, Token, Line}, LinesParsed, Rest};
case Token of
[Attr] ->
T1 = atom_to_list(Attr),
case string:tokens(T1, ":") of
[Val, Fun] ->
{ok, {apply, {[list_to_atom(Fun)], [list_to_atom(Val)]}, Line}, LinesParsed, Rest};
_ ->
{ok, {attribute, Token, Line}, LinesParsed, Rest}
end;
_ ->
{ok, {attribute, Token, Line}, LinesParsed, Rest}
end;
{error, Reason} ->
{error, {attribute, Reason, Line}}
end;
Expand Down
6 changes: 5 additions & 1 deletion test/src/eunit_compile.erl
Expand Up @@ -37,7 +37,11 @@ tmpl$ baz",

apply_test_() ->
{ok, C} = sgte:compile("foo $apply bar myVar$ baz"),
?_assert(C =:= [<<"foo ">>, {apply, {[bar], [myVar]}, 1}, <<" baz">>]).
{ok, C1} = sgte:compile("foo $myVar:bar$ baz"),
[?_assert(C =:= [<<"foo ">>, {apply, {[bar], [myVar]}, 1}, <<" baz">>]),
?_assert(C1 =:= [<<"foo ">>, {apply, {[bar], [myVar]}, 1}, <<" baz">>]),
?_assert(C =:= C1)].


map_test_() ->
{ok, C} = sgte:compile("foo $map bar varList$ baz"),
Expand Down
6 changes: 5 additions & 1 deletion test/src/eunit_render.erl
Expand Up @@ -43,9 +43,13 @@ include_test_() ->
apply_test_() ->
F = fun(L) -> lists:nth(2, L) end,
{ok, C} = sgte:compile("foo $apply second myList$ baz"),
{ok, C1} = sgte:compile("foo $myList:second$ baz"),
Res = sgte:render(C, [{second, F}, {myList, ["1", "2", "3"]}]),
Res1 = sgte:render(C1, [{second, F}, {myList, ["1", "2", "3"]}]),
ResultStr = [<<"foo ">>, "2", <<" baz">>],
?_assert(Res =:= ResultStr).
[?_assert(Res =:= ResultStr),
?_assert(Res1 =:= ResultStr),
?_assert(Res =:= Res1)].

simpleif_test_() ->
{ok, C} = sgte:compile(simple_if()),
Expand Down
6 changes: 5 additions & 1 deletion test/src/eunit_render_bin.erl
Expand Up @@ -43,9 +43,13 @@ include_test_() ->
apply_test_() ->
F = fun(L) -> lists:nth(2, L) end,
{ok, C} = sgte:compile("foo $apply second myList$ baz"),
{ok, C1} = sgte:compile("foo $myList:second$ baz"),
Res = sgte:render_bin(C, [{second, F}, {myList, ["1", "2", "3"]}]),
Res1 = sgte:render_bin(C1, [{second, F}, {myList, ["1", "2", "3"]}]),
ResultStr = [<<"foo ">>, <<"2">>, <<" baz">>],
?_assert(Res =:= ResultStr).
[?_assert(Res =:= ResultStr),
?_assert(Res1 =:= ResultStr),
?_assert(Res =:= Res1)].

simpleif_test_() ->
{ok, C} = sgte:compile(simple_if()),
Expand Down
6 changes: 5 additions & 1 deletion test/src/eunit_render_str.erl
Expand Up @@ -43,9 +43,13 @@ include_test_() ->
apply_test_() ->
F = fun(L) -> lists:nth(2, L) end,
{ok, C} = sgte:compile("foo $apply second myList$ baz"),
{ok, C1} = sgte:compile("foo $myList:second$ baz"),
Res = sgte:render_str(C, [{second, F}, {myList, ["1", "2", "3"]}]),
Res1 = sgte:render_str(C1, [{second, F}, {myList, ["1", "2", "3"]}]),
ResultStr = "foo 2 baz",
?_assert(Res =:= ResultStr).
[?_assert(Res =:= ResultStr),
?_assert(Res1 =:= ResultStr),
?_assert(Res =:= Res1)].

simpleif_test_() ->
{ok, C} = sgte:compile(simple_if()),
Expand Down

0 comments on commit e1c6be3

Please sign in to comment.