Commits

Kazunori Ninomiya committed 502eb66

Add test.

Comments (0)

Files changed (31)

 .DS_Store
-node_modules/*
+node_modules
+npm-debug.log
+tmp
 !.gitkeep
+{
+  "curly": true,
+  "eqeqeq": true,
+  "immed": true,
+  "latedef": true,
+  "newcap": true,
+  "noarg": true,
+  "sub": true,
+  "undef": true,
+  "boss": true,
+  "eqnull": true,
+  "node": true,
+  "es5": true,
+  "laxcomma": true
+}
 module.exports = function(grunt)
 {
   grunt.initConfig({
-    ect: {
-      top: {
-        options: {
-          root: 'test/ect',
-        },
-        src:  'page',
-        dest: 'test/page.html',
-        variables: {
-          title : 'Hello, world!',
-          id : 'main',
-          links: [
-            { name : 'Google', url : 'http://google.com/' },
-            { name : 'Facebook', url : 'http://facebook.com/' },
-            { name : 'Twitter', url : 'http://twitter.com/' }
-          ],
-          upperHelper : function (string) {
-            return string.toUpperCase();
+      jshint: {
+          all: [
+              'Gruntfile.js'
+            , 'tasks/*.js'
+            , '<%= nodeunit.tests %>'
+          ]
+        , options: {
+            jshintrc: '.jshintrc'
           }
-        }
-      },
-    },
+      }
+
+      // Before generating any new files, remove any previously-created files.
+    , clean: {
+        tests: ['tmp']
+      }
+
+    , ect: {
+          renderCompatibility: {
+              options: {
+                root: 'test/fixtures/ect'
+              }
+            , src:  'page'
+            , dest: 'tmp/compatibility/page.html'
+            , variables: {
+                  title: 'Hello, world!'
+                , id: 'main'
+                , links: [
+                      { name: 'Google',   url: 'http://google.com/'   }
+                    , { name: 'Facebook', url: 'http://facebook.com/' }
+                    , { name: 'Twitter',  url: 'http://twitter.com/'  }
+                  ]
+                , upperHelper: function (string) {
+                    return string.toUpperCase();
+                  }
+              }
+          }
+        , renderDefault: {
+            files: {
+                // 1:1 render
+                'tmp/default/ect.txt': 'test/fixtures/ect1.ect'
+                // concat then render into single file
+              , 'tmp/default/concat.txt': [
+                    'test/fixtures/ect1.ect'
+                  , 'test/fixtures/ect2.ect'
+                ]
+            }
+          }
+        , renderCustomOptions1: {
+              options: {
+                root: 'test/fixtures'
+              }
+            , files: {
+                'tmp/customOptions1/concat.txt': '*.ect'
+              }
+          }
+        , renderCustomOptions2: {
+              options: {
+                variables: {
+                    title: 'Hello, world!'
+                  , id: 'main'
+                  , links: [
+                        { name: 'Google',   url: 'http://google.com/'   }
+                      , { name: 'Facebook', url: 'http://facebook.com/' }
+                      , { name: 'Twitter',  url: 'http://twitter.com/'  }
+                    ]
+                  , upperHelper: function (string) {
+                      return string.toUpperCase();
+                    }
+                }
+              }
+            , files: {
+                'tmp/customOptions2/page.html': 'test/fixtures/ect/page'
+              }
+          }
+          // use JavaScript object as root.
+        , renderCustomOptions3: {
+              options: {
+                  root: {
+                      layout: '<html><head><title>[%- @title %]</title></head><body>[% content %]</body></html>'
+                    , page: '[% extend "layout" %]<p>Page content</p>',
+                  }
+                , open: '[%'
+                , close: '%]'
+                , variables: {
+                    title: 'Hello, world!'
+                  }
+              }
+            , src:  'page'
+            , dest: 'tmp/customOptions3/page.html'
+          }
+        , renderEach: {
+              options: {
+                  variables: { title: 'Hello, world!' }
+                , ext: '.html'
+              }
+            , expand: true
+            , flatten: true
+            , cwd: 'test/fixtures/view'
+            , src: '*.html'
+            , dest: 'tmp/each'
+            , ext: '.html'
+          }
+      }
+
+      // Unit tests.
+    , nodeunit: {
+        tests: ['test/*_test.js']
+      }
+
   });
 
+  // Actually load this plugin's task(s).
   grunt.loadTasks('tasks');
+
+  // These plugins provide necessary tasks.
+  grunt.loadNpmTasks('grunt-contrib-jshint');
+  grunt.loadNpmTasks('grunt-contrib-clean');
+  grunt.loadNpmTasks('grunt-contrib-nodeunit');
+
+  // Whenever the "test" task is run, first clean the "tmp" dir, then run this
+  // plugin's task(s), then test the result.
+  grunt.registerTask('test', ['clean', 'ect', 'nodeunit']);
+
+  // By default, lint and run all tests.
+  grunt.registerTask('default', ['jshint', 'test']);
+
 };

test/ect/footer

-<div id="footer">
-	<div class="left">Generated by ECT</div>
-	<% content 'footer-info' %>
-</div>

test/ect/layout

-<!DOCTYPE html>
-<html>
-	<head>
-		<title><%- @title %></title>
-	</head>
-	<body>
-		<% content %>
-		<% include 'footer' %>
-	</body>
-</html>

test/ect/list

-<% linkHelper = (link) -> %>
-	<li><a href="<%- link.url %>"><%- link.name %></a></li>
-<% end %>
-
-<% if @links?.length : %>
-	<ul>
-		<% for link in @links : %>
-			<%- linkHelper link %>
-		<% end %>
-	</ul>
-<% else : %>
-	<p>List is empty</p>
-<% end %>

test/ect/page

-<% extend 'layout' %>
-
-<div id="<%- @id %>">
-	<h1><%- @upperHelper @title %></h1>
-	<% include 'list' %>
-</div>
-
-<% block 'footer-info' : %>
-	<div class="right">page: main</div>
-<% end %>
+'use strict';
+
+var grunt = require('grunt');
+
+function readFile(file)
+{
+  var contents = grunt.file.read(file);
+
+  if (process.platform === 'win32') {
+    contents = contents.replace(/\r\n/g, '\n');
+  }
+
+  return contents;
+}
+
+function assertFileEquality(test, pathToActual, pathToExpected, message)
+{
+  test.equal(readFile(pathToExpected), readFile(pathToActual), message);
+}
+
+exports.ect = {
+    renderCompatibility: function(test)
+    {
+      test.expect(1);
+
+      assertFileEquality(test,
+        'tmp/compatibility/page.html',
+        'test/expected/compatibility/page.html',
+        '');
+
+      test.done();
+    }
+  , renderDefault: function(test)
+    {
+      test.expect(2);
+      
+      assertFileEquality(test,
+        'tmp/default/ect.txt',
+        'test/expected/default/ect.txt',
+        '');
+
+      assertFileEquality(test,
+        'tmp/default/concat.txt',
+        'test/expected/default/concat.txt',
+        '');
+
+      test.done();
+    }
+  , renderCustomOptions1: function(test)
+    {
+      test.expect(1);
+
+      assertFileEquality(test,
+        'tmp/customOptions1/concat.txt',
+        'test/expected/customOptions1/concat.txt',
+        '');
+
+      test.done();
+    }
+  , renderCustomOptions2: function(test)
+    {
+      test.expect(1);
+
+      assertFileEquality(test,
+        'tmp/customOptions2/page.html',
+        'test/expected/customOptions2/page.html',
+        '');
+
+      test.done();
+    }
+  , renderCustomOptions3: function(test)
+    {
+      test.expect(1);
+
+      assertFileEquality(test,
+        'tmp/customOptions3/page.html',
+        'test/expected/customOptions3/page.html',
+        '');
+
+      test.done();
+    }
+  , renderEach: function(test)
+    {
+      test.expect(5);
+
+      assertFileEquality(test,
+        'tmp/each/footer.html',
+        'test/expected/each/footer.html',
+        '');
+
+      assertFileEquality(test,
+        'tmp/each/header.html',
+        'test/expected/each/header.html',
+        '');
+
+      assertFileEquality(test,
+        'tmp/each/layout.html',
+        'test/expected/each/layout.html',
+        '');
+
+      assertFileEquality(test,
+        'tmp/each/page.html',
+        'test/expected/each/page.html',
+        '');
+
+      assertFileEquality(test,
+        'tmp/each/sublayout.html',
+        'test/expected/each/sublayout.html',
+        '');
+
+      test.done();
+    }
+};

test/expected/compatibility/page.html

+<!DOCTYPE html>
+<html>
+	<head>
+		<title>Hello, world!</title>
+	</head>
+	<body>
+		
+<div id="main">
+	<h1>HELLO, WORLD!</h1>
+	
+	<ul>
+						<li><a href="http://google.com/">Google</a></li>
+						<li><a href="http://facebook.com/">Facebook</a></li>
+						<li><a href="http://twitter.com/">Twitter</a></li>
+			</ul>
+</div>
+
+		<div id="footer">
+	<div class="left">Generated by ECT</div>
+		<div class="right">page: main</div>
+</div>
+	</body>
+</html>

test/expected/customOptions1/concat.txt

+Test 0 1 2 3 4 5 6 7 8 9 10
+Test 10 9 8 7 6 5 4 3 2 1 0

test/expected/customOptions2/page.html

+<!DOCTYPE html>
+<html>
+	<head>
+		<title>Hello, world!</title>
+	</head>
+	<body>
+		
+<div id="main">
+	<h1>HELLO, WORLD!</h1>
+	
+	<ul>
+						<li><a href="http://google.com/">Google</a></li>
+						<li><a href="http://facebook.com/">Facebook</a></li>
+						<li><a href="http://twitter.com/">Twitter</a></li>
+			</ul>
+</div>
+
+		<div id="footer">
+	<div class="left">Generated by ECT</div>
+		<div class="right">page: main</div>
+</div>
+	</body>
+</html>

test/expected/customOptions3/page.html

+<html><head><title>Hello, world!</title></head><body><p>Page content</p></body></html>

test/expected/default/concat.txt

+Test 0 1 2 3 4 5 6 7 8 9 10
+Test 10 9 8 7 6 5 4 3 2 1 0

test/expected/default/ect.txt

+Test 0 1 2 3 4 5 6 7 8 9 10

test/expected/each/footer.html

+<p>Footer content</p>

test/expected/each/header.html

+<h1>Page title</h1>

test/expected/each/layout.html

+<!DOCTYPE html>
+<html>
+	<head>
+		<title>Hello, world!</title>
+	</head>
+	<body>
+		<header><h1>Page title</h1>
+</header>
+		<section></section>
+		<section></section>
+		<section></section>
+		<footer><p>Footer content</p>
+</footer>
+	</body>
+</html>

test/expected/each/page.html

+<!DOCTYPE html>
+<html>
+	<head>
+		<title>Hello, world!</title>
+	</head>
+	<body>
+		<header><h1>Page title</h1>
+</header>
+		<section><div>
+<p>
+Page content
+</p>
+</div>
+</section>
+		<section>redefined side1 content
+</section>
+		<section>side2 content
+</section>
+		<footer><p>Footer content</p>
+</footer>
+	</body>
+</html>

test/expected/each/sublayout.html

+<!DOCTYPE html>
+<html>
+	<head>
+		<title>Hello, world!</title>
+	</head>
+	<body>
+		<header><h1>Page title</h1>
+</header>
+		<section><div>
+</div>
+</section>
+		<section>side1 content
+</section>
+		<section></section>
+		<footer><p>Footer content</p>
+</footer>
+	</body>
+</html>

test/fixtures/ect/footer

+<div id="footer">
+	<div class="left">Generated by ECT</div>
+	<% content 'footer-info' %>
+</div>

test/fixtures/ect/layout

+<!DOCTYPE html>
+<html>
+	<head>
+		<title><%- @title %></title>
+	</head>
+	<body>
+		<% content %>
+		<% include 'footer' %>
+	</body>
+</html>

test/fixtures/ect/list

+<% linkHelper = (link) -> %>
+	<li><a href="<%- link.url %>"><%- link.name %></a></li>
+<% end %>
+
+<% if @links?.length : %>
+	<ul>
+		<% for link in @links : %>
+			<%- linkHelper link %>
+		<% end %>
+	</ul>
+<% else : %>
+	<p>List is empty</p>
+<% end %>

test/fixtures/ect/page

+<% extend 'layout' %>
+
+<div id="<%- @id %>">
+	<h1><%- @upperHelper @title %></h1>
+	<% include 'list' %>
+</div>
+
+<% block 'footer-info' : %>
+	<div class="right">page: main</div>
+<% end %>

test/fixtures/ect1.ect

+Test<% for num in [0..10] : %> <%- num %><% end %>

test/fixtures/ect2.ect

+Test<% for num in [10..0] : %> <%- num %><% end %>

test/fixtures/view/footer.html

+<p>Footer content</p>

test/fixtures/view/header.html

+<h1>Page title</h1>

test/fixtures/view/layout.html

+<!DOCTYPE html>
+<html>
+	<head>
+		<title><%- @title %></title>
+	</head>
+	<body>
+		<header><% include 'header' %></header>
+		<section><% content %></section>
+		<section><% content 'side1' %></section>
+		<section><% content 'side2' %></section>
+		<footer><% include 'footer' %></footer>
+	</body>
+</html>

test/fixtures/view/page.html

+<% extend 'sublayout' %>
+<p>
+Page content
+</p>
+<% block 'side1' : %>
+redefined side1 content
+<% end %>
+<% block 'side2' : %>
+side2 content
+<% end %>

test/fixtures/view/sublayout.html

+<% extend 'layout' %>
+<div>
+<% content() %>
+</div>
+<% block 'side1' : %>
+side1 content
+<% end %>

test/page.html

-<!DOCTYPE html>
-<html>
-	<head>
-		<title>Hello, world!</title>
-	</head>
-	<body>
-		
-<div id="main">
-	<h1>HELLO, WORLD!</h1>
-	
-	<ul>
-						<li><a href="http://google.com/">Google</a></li>
-						<li><a href="http://facebook.com/">Facebook</a></li>
-						<li><a href="http://twitter.com/">Twitter</a></li>
-			</ul>
-</div>
-
-		<div id="footer">
-	<div class="left">Generated by ECT</div>
-		<div class="right">page: main</div>
-</div>
-	</body>
-</html>