Skip to content

Commit 6e8cd41

Browse files
committed
Code aggregation and add missing tests.
1 parent f52705a commit 6e8cd41

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

src/bbmustache.erl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,7 @@ compile_impl([{'#', Keys, Tags, Source} | T], Data, Result, State) ->
287287
_ when is_list(Value) ->
288288
compile_impl(T, Data, lists:foldl(fun(X, Acc) -> compile_impl(Tags, X, Acc, NestedState) end,
289289
Result, Value), State);
290-
_ when Value =:= false ->
291-
compile_impl(T, Data, Result, State);
292-
_ when Value =:= nil ->
293-
compile_impl(T, Data, Result, State);
294-
_ when Value =:= <<"">> ->
290+
_ when Value =:= false; Value =:= nil; Value =:= <<"">> ->
295291
compile_impl(T, Data, Result, State);
296292
_ when is_function(Value, 2) ->
297293
Ret = Value(Source, fun(Text) -> render(Text, Data, State#?MODULE.options) end),
@@ -301,7 +297,7 @@ compile_impl([{'#', Keys, Tags, Source} | T], Data, Result, State) ->
301297
end;
302298
compile_impl([{'^', Keys, Tags} | T], Data, Result, State) ->
303299
Value = get_data_recursive(Keys, Data, false, State),
304-
case Value =:= [] orelse Value =:= false orelse Value =:= nil orelse Value =:= "" of
300+
case Value =:= [] orelse Value =:= false orelse Value =:= nil orelse Value =:= <<"">> of
305301
true -> compile_impl(T, Data, compile_impl(Tags, Data, Result, State), State);
306302
false -> compile_impl(T, Data, Result, State)
307303
end;

test/bbmustache_tests.erl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -215,23 +215,23 @@ context_stack_test_() ->
215215
?_assertEqual(<<"">>,
216216
bbmustache:render(<<"{{#parent}}aaa{{parent.child}}bbb{{/parent}}">>,
217217
[{"parent", []}],
218-
[raise_on_context_miss]))},
219-
{"It hides content in # tag that is specified as \"\"",
220-
?_assertEqual(<<"">>,
221-
bbmustache:render(<<"{{#content}}hello world{{/content}}">>,
222-
[{"content", ""}]))},
223-
{"It hides content in # tag that is specified as \"\"",
224-
?_assertEqual(<<"">>,
225-
bbmustache:render(<<"{{#content}}hello world{{/content}}">>,
226-
[{"content", nil}]))},
227-
{"It shows content in ^ tag that is specified as \"\"",
228-
?_assertEqual(<<"hello world">>,
229-
bbmustache:render(<<"{{^content}}hello world{{/content}}">>,
230-
[{"content", ""}]))},
231-
{"It shows content in ^ tag that is specified as \"\"",
232-
?_assertEqual(<<"hello world">>,
233-
bbmustache:render(<<"{{^content}}hello world{{/content}}">>,
234-
[{"content", nil}]))}
218+
[raise_on_context_miss]))}
219+
].
220+
221+
shows_or_hides_content_test_() ->
222+
[
223+
{"It hides content in # tag that is specified as empty list, empty binary, nil or false",
224+
fun() ->
225+
lists:foreach(fun(X) ->
226+
?assertEqual(<<"">>, bbmustache:render(<<"{{#content}}hello world{{/content}}">>, [{"content", X}]))
227+
end, ["", <<"">>, nil, false])
228+
end},
229+
{"It show content in ^ tag that is specified as empty list, empty binary, nil or false",
230+
fun() ->
231+
lists:foreach(fun(X) ->
232+
?assertEqual(<<"hello world">>, bbmustache:render(<<"{{^content}}hello world{{/content}}">>, [{"content", X}]))
233+
end, ["", <<"">>, nil, false])
234+
end}
235235
].
236236

237237
escape_fun_test_() ->

0 commit comments

Comments
 (0)