fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-02-22 02:32 pm

[dw-free] Framework for JS unit tests

[commit: http://hg.dwscoalition.org/dw-free/rev/ca3225625315]

http://bugs.dwscoalition.org/show_bug.cgi?id=3524

Move fetching and parsing of the test templates from the .tt file into the
controller. Also tweak documentation and whitespace.

Patch by [personal profile] fu.

Files modified:
  • cgi-bin/DW/Controller/Dev.pm
  • views/dev/tests.tt
  • views/dev/tests/sample.js
--------------------------------------------------------------------------------
diff -r 8daecda810b9 -r ca3225625315 cgi-bin/DW/Controller/Dev.pm
--- a/cgi-bin/DW/Controller/Dev.pm	Tue Feb 22 22:29:33 2011 +0800
+++ b/cgi-bin/DW/Controller/Dev.pm	Tue Feb 22 22:32:10 2011 +0800
@@ -33,6 +33,29 @@ sub tests_handler {
 
     my $r = DW::Request->get;
 
+    my @includes;
+    my $testcontent = eval{ DW::Template->template_string( "dev/tests/${test}.js" ) } || "";
+    if ( $testcontent ) {
+        $testcontent =~ m#/\*\s*INCLUDE:\s*(.*?)\*/#s;
+        my $match = $1;
+        for my $res ( split( /\n+/, $match ) ) {
+
+            # skip things that don't look like names (could just be an empty line)
+            next unless $res =~ /\w+/;
+
+            # remove the library label
+            $res =~ s/(\w+)://;
+
+            # skip if we specify a library that's different from our current library
+            next if $1 && $1 ne $lib;
+
+            push @includes, LJ::trim( $res );
+        }
+    }
+
+    my $testhtml = eval{ DW::Template->template_string( "dev/tests/${test}.html" ) }
+            || "<!-- no html template -->";
+
     # force a site scheme which only shows the bare content
     # but still prints out resources included using need_res
     $r->note( bml_use_scheme => "global" );
@@ -41,6 +64,9 @@ sub tests_handler {
     return DW::Template->render_template( "dev/tests.tt", {
             testname => $test,
             testlib  => $lib,
+            testhtml => $testhtml,
+            tests    => $testcontent,
+            includes => \@includes,
          } );
 }
 1;
diff -r 8daecda810b9 -r ca3225625315 views/dev/tests.tt
--- a/views/dev/tests.tt	Tue Feb 22 22:29:33 2011 +0800
+++ b/views/dev/tests.tt	Tue Feb 22 22:32:10 2011 +0800
@@ -11,24 +11,7 @@ the same terms as Perl itself.  For a co
 %]
 
 [% testname = testname | html %]
-[% label = testname %]
-[%- includes = [ "js/tests/qunit.js", "stc/tests/qunit.css" ] -%]
-
-[%- TRY;
-        tests = BLOCK;
-            INSERT "dev/tests/${testname}.js";
-        END;
-
-        has_tests = 1;
-    CATCH file;
-        label = "No tests defined for $testname";
-    END
--%]
-
-[%- IF has_tests AND ( matches = tests.match('(?s)/\* INCLUDE:\n(.*?)\*/') );
-        includes = includes.merge( matches.first.split("\\s+").grep("\\w+") );
-    END
--%]
+[%- includes = includes.merge( [ "js/tests/qunit.js", "stc/tests/qunit.css" ] ) -%]
 
 [%- IF testlib == "jquery";
         dw.need_res( { group => "jquery" }, includes );
@@ -43,15 +26,7 @@ the same terms as Perl itself.  For a co
 
 
 [%- sections.head = BLOCK %]
-<!-- run all the tests -->
-<!-- include extra JS / CSS files by adding this comment to your testname.js:
-
-/* INCLUDE:
-js/blah.js
-stc/blah.css
-*/
-
--->
+<!-- test definition -->
 <script type="text/javascript">
 if ( ! location.search ) {
     var lib = "[% testlib %]";
@@ -64,24 +39,22 @@ if ( ! location.search ) {
 
 [% IF testlib == "jquery" %]
     $(document).ready(function(){
-        [% tests %]
+        [%- tests -%]
     });
 [% ELSIF testlib == "old" %]
     LiveJournal.register_hook("page_load", function(){
-        [% tests %]
+        [%- tests -%]
     });
 [% ELSE %]
     window.onload = function() {
-        [% tests %]
+        [%- tests -%]
     }
 [% END %]
 </script>
 [% END %]
 
-
-<h1 id="qunit-header">[% label %]</h1>
-
-[%- IF has_tests -%]
+[%- IF tests -%]
+    <h1 id="qunit-header">[% testname %]</h1>
     <h2 id="qunit-banner"></h2>
     <div id="qunit-testrunner-toolbar"></div>
     <h2 id="qunit-userAgent"></h2>
@@ -89,10 +62,8 @@ if ( ! location.search ) {
 
     <!-- for any html required by the test -->
     <div id="qunit-fixture">
-    [%- TRY -%]
-        [% INSERT "dev/tests/${testname}.html" %]
-    [% CATCH file %]
-            <!-- no html template -->
-    [% END %]
+        [% testhtml %]
     </div>
+[% ELSE %]
+    <h1 id="qunit-header">No tests for [% testname %]</h1>
 [%- END -%]
diff -r 8daecda810b9 -r ca3225625315 views/dev/tests/sample.js
--- a/views/dev/tests/sample.js	Tue Feb 22 22:29:33 2011 +0800
+++ b/views/dev/tests/sample.js	Tue Feb 22 22:32:10 2011 +0800
@@ -1,6 +1,8 @@
 /* INCLUDE:
 js/sample.js
 stc/sample.css
+jquery: stc/jquery-only-file.js
+old: stc/old-only-file.js
 */
 
 /*==============================================================================
--------------------------------------------------------------------------------