Browse Source

Check for regressions of the functional tests.

Andreas Stenius 11 years ago
parent
commit
69c224c3a8

+ 8 - 0
tests/expect/autoescape

@@ -0,0 +1,8 @@
+
+    This is escaped: <b>bold</b>
+    
+    This is not escaped: <b>bold</b>
+
+    This is escaped: &lt;b&gt;bold&lt;/b&gt;
+    
+

+ 15 - 0
tests/expect/comment

@@ -0,0 +1,15 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+    <title>Test Comment</title>								 
+  </head>
+  <body>
+	
+	bla
+	
+        blue
+        
+        black
+  </body>
+</html>

+ 23 - 0
tests/expect/custom_call

@@ -0,0 +1,23 @@
+>>>> before custom call tag 'comment'
+before
+
+<ul>
+
+	<li>preset-apple</li>
+
+	<li>preset-banana</li>
+
+	<li>preset-coconut</li>
+
+</ul>
+
+after
+>>>> after custom call tag 'comment'
+
+>>>> before custom call tag 'if'
+One but not two:  two 
+Two but not one:  one 
+One: 
+None: 
+
+>>>> after custom call tag 'if'

+ 33 - 0
tests/expect/custom_tag

@@ -0,0 +1,33 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+    <title>Test variable</title>								 
+  </head>
+  <body>
+	before
+	<object id="myvideo" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="800" height="600">
+	<param name="movie" value="/static/mediaplayer.swf">
+    <param name="allowfullscreen" value="true">
+    <param name="menu" value="false">
+    <param name="flashvars" value="file=/myvid.mp4&image=/mypic.jpg">
+    <!--[if !IE]>-->
+    <object type="application/x-shockwave-flash" data="/static/mediaplayer.swf" width="800" height="620">
+    <param name="allowfullscreen" value="true">
+    <param name="menu" value="false">
+    <param name="flashvars" value="file=/myvid.mp4&image=/mypic.jpg">
+    <!--<![endif]-->
+    <h2>To view the Video:</h2>
+    <p>
+    	<a href="http://www.adobe.com/go/getflashplayer">
+        	<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player">
+    	</a>
+    </p>
+    <!--[if !IE]>-->
+    </object>
+    <!--<![endif]-->
+</object>
+
+	after
+  </body>
+</html>

+ 47 - 0
tests/expect/cycle

@@ -0,0 +1,47 @@
+before
+
+<ul>
+
+<li>1. 1 - Apple</li>
+
+<li>2. 2 - Banana</li>
+
+<li>3. 3 - Cherry</li>
+
+<li>4. 4 - Apple</li>
+
+<li>5. 5 - Banana</li>
+
+<li>6. 6 - Cherry</li>
+
+<li>7. 7 - Apple</li>
+
+<li>8. 8 - Banana</li>
+
+<li>9. 9 - Cherry</li>
+
+<li>10. 10 - Apple</li>
+
+<li>11. 11 - Banana</li>
+
+<li>12. 12 - Cherry</li>
+
+<li>13. 13 - Apple</li>
+
+<li>14. 14 - Banana</li>
+
+<li>15. 15 - Cherry</li>
+
+<li>16. 16 - Apple</li>
+
+<li>17. 17 - Banana</li>
+
+<li>18. 18 - Cherry</li>
+
+<li>19. 19 - Apple</li>
+
+<li>20. 20 - Banana</li>
+
+</ul>
+
+after

+ 11 - 0
tests/expect/extends

@@ -0,0 +1,11 @@
+base-barstring
+
+base template
+
+replacing the base title
+
+more of base template
+
+replacing the base content - variable: test-barstring after variable 
+
+end of base template

+ 14 - 0
tests/expect/extends_path

@@ -0,0 +1,14 @@
+base-barstring
+
+base2 template
+
+replacing the base title
+block title 2 from  base1 
+
+more of base2 template
+
+replacing the base content - variable: test-barstring after variable 
+
+block content2 in base 2, should pass through
+
+end of base2 template

+ 10 - 0
tests/expect/extends_path2

@@ -0,0 +1,10 @@
+pre content
+
+
+start_content
+This is include1
+
+end_content
+
+
+post

+ 45 - 0
tests/expect/filters

@@ -0,0 +1,45 @@
+Add: 2 + 2 = 4
+
+Capfirst: Capitalized
+
+Centered:
+<pre>
+       center       
+</pre>
+
+Date format:  Thu, 24 Jul 1975 00:00:00 +0100
+DateTime format: Thu, 24 Jul 1975 07:13:01 +0100
+
+Escape JS: \u0022 \u0027
+
+First letter: f
+
+Fix ampersands: &amp;
+
+Force_escape: &lt;b&gt;&lt;/b&gt;
+
+Joined: eins, zwei, drei
+
+Last: t
+
+Length: 3
+
+Length is 2?: false
+
+Left adjust: 
+<pre>
+left                
+</pre>
+
+Line breaks: Line 1<br />Line 2<br />Line 3
+
+Lowercase: lowercase
+
+Right adjust:
+<pre>
+               right
+</pre>
+
+Uppercase: UPPERCASE
+
+URL Encode: Let%27s%20go%21

+ 13 - 0
tests/expect/for

@@ -0,0 +1,13 @@
+before
+
+<ul>
+
+<li>1. apple</li>
+
+<li>2. banana</li>
+
+<li>3. coconut</li>
+
+</ul>
+
+after

+ 7 - 0
tests/expect/for_list

@@ -0,0 +1,7 @@
+
+More than one apple is called "apples". Only $1 each!
+
+More than one banana is called "bananas". Only $2 each!
+
+More than one coconut is called "coconuts". Only $500 each!
+

+ 7 - 0
tests/expect/for_list_preset

@@ -0,0 +1,7 @@
+
+More than one apple is called "apples".
+
+More than one banana is called "bananas".
+
+More than one coconut is called "coconuts".
+

+ 13 - 0
tests/expect/for_preset

@@ -0,0 +1,13 @@
+before
+
+<ul>
+
+	<li>preset-apple</li>
+
+	<li>preset-banana</li>
+
+	<li>preset-coconut</li>
+
+</ul>
+
+after

+ 13 - 0
tests/expect/for_records

@@ -0,0 +1,13 @@
+before
+
+<ul>
+
+	<li><a href="http://amazon.com">Amazon</a></li>
+
+	<li><a href="http://google.com">Google</a></li>
+
+	<li><a href="http://microsoft.com">Microsoft</a></li>
+
+</ul>
+
+after

+ 23 - 0
tests/expect/for_records_preset

@@ -0,0 +1,23 @@
+before
+
+<ul>
+
+	<li><a href="http://canon.com">Canon</a></li>
+
+	<li><a href="http://leica.com">Leica</a></li>
+
+	<li><a href="http://nikon.com">Nikon</a></li>
+
+</ul>
+
+<ul>
+
+	<li><a href="http://amazon.com">Amazon (preset)</a></li>
+
+	<li><a href="http://google.com">Google (preset)</a></li>
+
+	<li><a href="http://microsoft.com">Microsoft (preset)</a></li>
+
+</ul>
+
+after

+ 7 - 0
tests/expect/for_tuple

@@ -0,0 +1,7 @@
+
+One apple, two apples!
+
+One banana, two bananas!
+
+One coconut, two coconuts!
+

+ 4 - 0
tests/expect/if

@@ -0,0 +1,4 @@
+One but not two:  one 
+Two but not one:  two 
+One:  one 
+None: 

+ 4 - 0
tests/expect/if_preset

@@ -0,0 +1,4 @@
+One but not two:  one 
+Two but not one:  two 
+One:  one 
+None: 

+ 28 - 0
tests/expect/ifequal

@@ -0,0 +1,28 @@
+
+if: var1="foo" and var2="foo" are equal
+
+
+
+if: var1="foo" and var2="foo" are equal
+
+
+
+
+
+else: var1="foo" and var3="bar" are not equal
+
+
+
+if: "foo" and "foo" are equal
+
+
+
+else: "foo" and "bar" are not equal
+
+
+
+if: 99 and 99 are equal
+
+
+
+else: 77 and 99 are not equal

+ 28 - 0
tests/expect/ifequal_preset

@@ -0,0 +1,28 @@
+
+if: var1="foo" and var2="foo" are equal
+
+
+
+if: var1="foo" and var2="foo" are equal
+
+
+
+
+
+else: var1="foo" and var3="bar" are not equal
+
+
+
+if: "foo" and "foo" are equal
+
+
+
+else: "foo" and "bar" are not equal
+
+
+
+if: 99 and 99 are equal
+
+
+
+else: 77 and 99 are not equal

+ 28 - 0
tests/expect/ifnotequal

@@ -0,0 +1,28 @@
+
+
+
+else: var1="foo" and var2="foo" are not equal
+
+
+
+if: var1="foo" and var3="bar" are equal
+
+
+
+if: var1="foo" and var3="bar" are equal
+
+
+
+else: "foo" and "foo" are not equal
+
+
+
+if: "foo" and "bar" are equal
+
+
+
+else: 99 and 99 are not equal
+
+
+
+if: 77 and 99 are equal

+ 28 - 0
tests/expect/ifnotequal_preset

@@ -0,0 +1,28 @@
+
+
+
+else: var1="foo" and var2="foo" are not equal
+
+
+
+if: var1="foo" and var3="bar" are equal
+
+
+
+if: var1="foo" and var3="bar" are equal
+
+
+
+else: "foo" and "foo" are not equal
+
+
+
+if: "foo" and "bar" are equal
+
+
+
+else: 99 and 99 are not equal
+
+
+
+if: 77 and 99 are equal

+ 2 - 0
tests/expect/include

@@ -0,0 +1,2 @@
+Including another file: This is included! foostring1
+

+ 12 - 0
tests/expect/include_path

@@ -0,0 +1,12 @@
+main file
+
+This is template 1.
+
+test-barstring
+
+
+This is template 2
+
+
+
+base-barstring

+ 14 - 0
tests/expect/include_template

@@ -0,0 +1,14 @@
+Including another template: base-barstring
+
+base template
+
+base title
+
+more of base template
+
+base content
+
+end of base template
+
+
+test variable: test-barstring

+ 11 - 0
tests/expect/now

@@ -0,0 +1,11 @@
+Expected format : Thu, 21 Dec 2000 16:01:07 +0200
+Got : Fri, 13 Sep 2013 10:19:05 +0200
+
+Expected format : 27th February 2008 01:24
+Got : 13th September 2013 10:19
+
+Expected format : It is the 4th of September 2007
+Got : It is the 13th of September 2013
+
+Expected format : ''
+Got : ''

+ 2 - 0
tests/expect/ssi

@@ -0,0 +1,2 @@
+{{ "Don't evaluate me!" }}
+

+ 1 - 0
tests/expect/trans

@@ -0,0 +1 @@
+Example String

+ 5 - 0
tests/expect/var

@@ -0,0 +1,5 @@
+before varriable1
+foostring1
+after variable1
+foostring2
+after variable2 (HTML-comment-wrapped)

+ 9 - 0
tests/expect/var_preset

@@ -0,0 +1,9 @@
+one
+foostring1
+two
+preset-var1
+three
+foostring2
+four
+preset-var2
+five

+ 82 - 70
tests/src/erlydtl_functional_tests.erl

@@ -159,11 +159,11 @@ setup("trans") ->
 setup("locale") ->
 setup("locale") ->
     {ok, _RenderVars = [{locale, "ru"}]};
     {ok, _RenderVars = [{locale, "ru"}]};
 setup("custom_tag1") ->
 setup("custom_tag1") ->
-    {ok, [{a, <<"a1">>}], [{locale, ru}], [<<"b1">>, <<"\n">>]};
+    {ok, [{a, <<"a1">>}], [{locale, ru}], <<"b1\n">>};
 setup("custom_tag2") ->
 setup("custom_tag2") ->
-    {ok, [{a, <<"a1">>}], [{locale, ru}, {foo, bar}], [<<"b2">>, <<"\n">>]};
+    {ok, [{a, <<"a1">>}], [{locale, ru}, {foo, bar}], <<"b2\n">>};
 setup("custom_tag3") ->
 setup("custom_tag3") ->
-    {ok, [{a, <<"a1">>}], [{locale, ru}], [<<"b3">>, <<"\n">>]};
+    {ok, [{a, <<"a1">>}], [{locale, ru}], <<"b3\n">>};
 setup("ssi") ->
 setup("ssi") ->
     RenderVars = [{path, filename:absname(filename:join(["tests", "input", "ssi_include.html"]))}],
     RenderVars = [{path, filename:absname(filename:join(["tests", "input", "ssi_include.html"]))}],
     {ok, RenderVars};
     {ok, RenderVars};
@@ -182,19 +182,26 @@ setup(_) ->
 
 
 run_tests() ->    
 run_tests() ->    
     io:format("Running functional tests...~n"),
     io:format("Running functional tests...~n"),
-    case filelib:ensure_dir(filename:join([templates_outdir(), "foo"])) of
-        ok ->
+    case [filelib:ensure_dir(
+            filename:join([templates_dir(Dir), "foo"]))
+          || Dir <- ["output", "expect"]] -- [ok,ok]
+    of
+        [] ->
             case fold_tests() of
             case fold_tests() of
                 {N, []}->
                 {N, []}->
                     Msg = lists:concat(["All ", N, " functional tests passed~n~n"]),
                     Msg = lists:concat(["All ", N, " functional tests passed~n~n"]),
                     io:format(Msg),
                     io:format(Msg),
                     {ok, Msg};
                     {ok, Msg};
-                {_, Errs} ->
-                    io:format("Errors: ~p~n~n",[Errs]),
+                {N, Errs} ->
+                    io:format(
+                      "~b / ~b functional tests failed.~nErrors: ~n",
+                      [length(Errs), N]),
+                    [io:format("  ~s [~s] ~s~n", [Name, Error, Reason])
+                     || {Name, Error, Reason} <- Errs],
                     failed
                     failed
             end;
             end;
-        {error, Reason} ->
-            io:format("Error: ~p~n~n", [Reason]),
+        Err ->
+            [io:format("Ensure dir failed: ~p~n~n", [Reason]) || {error, Reason} <- Err],
             failed
             failed
     end.
     end.
 
 
@@ -209,97 +216,102 @@ run_test(Name) ->
 
 
 fold_tests() ->
 fold_tests() ->
     lists:foldl(fun(Name, {AccCount, AccErrs}) ->
     lists:foldl(fun(Name, {AccCount, AccErrs}) ->
-                case test_compile_render(Name) of
-                    ok -> 
-                        {AccCount + 1, AccErrs};
-                    {error, Reason} -> 
-                        {AccCount + 1, [{Name, Reason} | AccErrs]}
-                end
-        end, {0, []}, test_list()
-    ).
+                        Res = case catch test_compile_render(Name) of
+                                  ok -> {AccCount + 1, AccErrs};
+                                  {'EXIT', Reason} ->
+                                      {AccCount + 1, [{Name, crash,
+                                                       io_lib:format("~p", [Reason])}
+                                                      | AccErrs]};
+                                  {Error, Reason} ->
+                                      {AccCount + 1, [{Name, Error, Reason}
+                                                      | AccErrs]}
+                              end,
+                        io:format("~n"), Res
+                end, {0, []}, test_list()).
 
 
 test_compile_render(Name) ->  
 test_compile_render(Name) ->  
     File = filename:join([templates_docroot(), Name]),
     File = filename:join([templates_docroot(), Name]),
     Module = "example_" ++ Name,
     Module = "example_" ++ Name,
+    io:format(" Template: ~p, ... ", [Name]),
     case setup_compile(Name) of
     case setup_compile(Name) of
         {CompileStatus, CompileVars} ->
         {CompileStatus, CompileVars} ->
             Options = [
             Options = [
                 {vars, CompileVars}, 
                 {vars, CompileVars}, 
                 {force_recompile, true},
                 {force_recompile, true},
                 {custom_tags_modules, [erlydtl_custom_tags]}],
                 {custom_tags_modules, [erlydtl_custom_tags]}],
-            io:format(" Template: ~p, ... compiling ... ", [Name]),
+            io:format("compiling ... "),
             case erlydtl:compile(File, Module, Options) of
             case erlydtl:compile(File, Module, Options) of
                 ok ->
                 ok ->
-                    case CompileStatus of
-                        ok -> test_render(Name, list_to_atom(Module));
-                        _ -> {error, "compiling should have failed :" ++ File}
+                    if CompileStatus =:= ok -> test_render(Name, list_to_atom(Module));
+                       true ->
+                            io:format("missing error"),
+                            {error, "compiling should have failed :" ++ File}
                     end;
                     end;
                 {error, Err} ->
                 {error, Err} ->
-                    case CompileStatus of
-                        error ->
-                            io:format("~n"),  
-                            ok;
-                        _ ->
-                            io:format("~nCompile errror: ~p~n",[Err]), 
-                            Err
+                    if CompileStatus =:= error -> io:format("ok");
+                       true -> 
+                            io:format("failed"),
+                            {compile_error, io_lib:format("~p", [Err])}
                     end
                     end
             end;
             end;
-        skip ->
-            ok;
-        _ ->
-            {error, "no 'setup' clause defined for this test"}
+        skip -> io:format("skipped")
     end.
     end.
 
 
-
 test_render(Name, Module) ->
 test_render(Name, Module) ->
     File = filename:join([templates_docroot(), Name]),
     File = filename:join([templates_docroot(), Name]),
     {RenderStatus, Vars, Opts, RenderResult} =
     {RenderStatus, Vars, Opts, RenderResult} =
         case setup(Name) of
         case setup(Name) of
-            {RS, V}       -> {RS, V, [], undefined};
-            {RS, V, O}    -> {RS, V, O, undefined};
+            {RS, V}       -> {RS, V, [], get_expected_result(Name)};
+            {RS, V, O}    -> {RS, V, O, get_expected_result(Name)};
             {RS, V, O, R} -> {RS, V, O, R}
             {RS, V, O, R} -> {RS, V, O, R}
         end,
         end,
+    io:format("rendering ... "), 
     case catch Module:render(Vars, Opts) of
     case catch Module:render(Vars, Opts) of
-        {ok, Data} ->
-            io:format("rendering~n"), 
-            case RenderStatus of
-                ok ->
-                    case RenderResult of
-                        undefined ->
-                            {File, _} = Module:source(),
-                            OutFile = filename:join([templates_outdir(), filename:basename(File)]),
-                            case file:open(OutFile, [write]) of
-                                {ok, IoDev} ->
-                                    file:write(IoDev, Data),
-                                    file:close(IoDev),
-                                    ok;
-                                Err ->
-                                    Err
+        {ok, Output} ->
+            Data = iolist_to_binary(Output),
+            if RenderStatus =:= ok ->
+                    if RenderResult =:= undefined ->
+                            Devs = [begin 
+                                        FileName = filename:join([templates_dir(Dir), Name]),
+                                        {ok, IoDev} = file:open(FileName, [write]),
+                                        IoDev
+                                    end || Dir <- ["output", "expect"]],
+                            try
+                                [file:write(IoDev, Data) || IoDev <- Devs],
+                                io:format("~n    #### NOTE: created new expected output file: \"tests/expect/~s\"."
+                                          "~n    Please verify contents.", [Name])
+                            after
+                                [file:close(IoDev) || IoDev <- Devs]
                             end;
                             end;
-                        _ when Data =:= RenderResult ->
-                            ok;
-                        _ ->
-                            {error, lists:flatten(io_lib:format("Test ~s failed\n"
-                                "Expected: ~p\n"
-                                "Value:    ~p\n", [Name, RenderResult, Data]))}
-                        end;
-                _ ->
-                    {error, "rendering should have failed :" ++ File}
+                       RenderResult =:= Data ->
+                            io:format("ok");
+                       true ->
+                            io:format("failed"),
+                            {error, io_lib:format(
+                                      "Expected output does not match rendered output~n"
+                                      "==Expected==~n~s~n--Actual--~n~s~n==End==~n",
+                                      [RenderResult, Data])}
+                    end;
+               true ->
+                    io:format("missing error"),
+                    {missing_error, "rendering should have failed :" ++ File}
             end;
             end;
         {'EXIT', Reason} ->
         {'EXIT', Reason} ->
-            io:format("~n"),
-            {error, lists:flatten(io_lib:format("failed invoking render method of ~p ~p", [Module, Reason]))};
+            io:format("failed"),
+            {render_error, io_lib:format("failed invoking render method of ~p ~p", [Module, Reason])};
         Err ->
         Err ->
-            io:format("~n"),
-            case RenderStatus of
-                error ->  ok;
-                _ -> Err
+            if RenderStatus =:= error -> io:format("ok");
+               true -> io:format("failed"),
+                       {render_error, io_lib:format("~p", [Err])}
             end
             end
     end.   
     end.   
 
 
+get_expected_result(Name) ->
+    FileName = filename:join([templates_dir("expect"), Name]),
+    case filelib:is_regular(FileName) of
+        true -> {ok, Data} = file:read_file(FileName), Data;
+        false -> undefined
+    end.
 
 
-templates_docroot() ->
-    filename:join([erlydtl_deps:get_base_dir(), "tests", "input"]).
-
-templates_outdir() ->   
-    filename:join([erlydtl_deps:get_base_dir(), "tests", "output"]).
+templates_docroot() -> templates_dir("input").
+templates_dir(Name) -> filename:join([erlydtl_deps:get_base_dir(), "tests", Name]).