Permalink
Browse files

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...
1 parent 3ed259f commit e1c6be38601220fa77b7dce7e2c0e003d49594a7 @filippo committed Oct 16, 2011
Showing with 32 additions and 5 deletions.
  1. +12 −1 src/sgte_parse.erl
  2. +5 −1 test/src/eunit_compile.erl
  3. +5 −1 test/src/eunit_render.erl
  4. +5 −1 test/src/eunit_render_bin.erl
  5. +5 −1 test/src/eunit_render_str.erl
View
@@ -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;
@@ -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"),
@@ -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()),
@@ -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()),
@@ -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()),

0 comments on commit e1c6be3

Please sign in to comment.