Permalink
Browse files

Move the test building script into the Cakefile.

  • Loading branch information...
1 parent 7f5c020 commit 69ac70a4cbf9fab30577b62232b4094f057d2577 @pvande committed Dec 30, 2010
Showing with 51 additions and 50 deletions.
  1. +1 −1 .gitignore
  2. +50 −0 Cakefile
  3. +0 −49 build_spec_tests.rb
View
2 .gitignore
@@ -1 +1 @@
-test/*_spec.coffee
+test/*_spec.js
View
50 Cakefile
@@ -1,5 +1,6 @@
fs = require 'fs'
path = require 'path'
+{exec} = require 'child_process'
CoffeeScript = require 'coffee-script'
option '-o', '--output [DIR]', 'directory for compiled code'
@@ -11,3 +12,52 @@ task 'build:js', 'Builds Milk into ./pages (or --output)', (options) ->
fs.readFile path.join(__dirname, 'milk.coffee'), 'utf8', (err, data) ->
throw err if err
fs.writeFile path.join(out, 'milk.js'), CoffeeScript.compile(data)
+
+task 'spec:node', 'Creates compliance tests for the Mustache spec', ->
+ # Convert the YAML files to Javascript.
+ # Requires the YAML and JSON libraries.
+ ruby = 'ruby -rubygems -e "require \'yaml\'" -e "require \'json\'"'
+ ruby += " -e 'YAML::add_builtin_type(\"code\") do"
+ ruby += " |_,v| v[\"js\"].tap do |x|"
+ ruby += " def x.to_json(_)"
+ ruby += " \"function() { return \#{self}; }\""
+ ruby += " end"
+ ruby += " end"
+ ruby += " end'"
+ ruby += " -e 'print YAML.load_file(ARGV[0]).to_json()'"
+
+ dir = path.join(__dirname, 'ext', 'spec', 'specs')
+ files = fs.readdirSync dir
+ for file in files
+ do (file) ->
+ exec "#{ruby} -- #{path.join(dir, file)}", (err, json, _) ->
+ throw err if err
+
+ test = """
+ vows = require('vows');
+ equal = require('assert').equal;
+ Milk = require('milk');
+ suite = vows.describe('Mustache Specification - #{file}');
+
+ tests = #{json}['tests'];
+
+ var batch = {};
+ for (var i = 0; i < tests.length; i++) {
+ var test = tests[i];
+
+ var context = {};
+ context['topic'] = #{topic};
+ context[test.desc] = function(r) { equal(r, test.expected) };
+
+ batch[test.name] = context
+ }
+
+ suite.addBatch(batch);
+ suite.export(module);
+ """
+ testFile = file.replace(/^~/, '').replace(/\.yml$/, '_spec.js')
+ fs.writeFile path.join(__dirname, 'test', testFile), test
+
+topic = ->
+ try Milk.render(test.template, test.data, test.partials || {})
+ catch e then "ERROR: " + e
View
49 build_spec_tests.rb
@@ -1,49 +0,0 @@
-# Since Javascript's YAML libraries are still very young, we'll preprocess them
-# and build a test suite all at once.
-require 'yaml'
-require 'json'
-
-__DIR__ = File.dirname(__FILE__)
-SPECS = File.join(__DIR__, 'ext', 'spec', 'specs')
-
-YAML::add_builtin_type('code') do |_, val|
- func = val['js']
- def func.to_json(_); "(-> @f ||= `#{self}`)"; end
- func
-end
-
-Dir["#{SPECS}/*.yml"].each do |file|
- basename = File.basename(file)[/\w+/]
- tests = YAML.load_file(file)
- File.open(File.join(__DIR__, 'test', "#{basename}_spec.coffee"), 'w') do |f|
- basename[0] = basename[0, 1].upcase
- f.puts <<-COFFEE.gsub(/^ /, '')
- vows = require 'vows'
- assert = require 'assert'
-
- Milk = require 'milk'
-
- suite = vows.describe 'Mustache Specification - #{basename}'
- suite.addBatch
- COFFEE
-
- tests['tests'].each do |t|
- t.keys.each { |key| t[key] = t[key].to_json }
-
- f.puts <<-COFFEE.gsub(/^ /, '')
- #{t['name']}:
- topic: ->
- try
- Milk.render #{t['template']}, #{t['data']}, #{t['partials'] || '{}'}
- catch e
- return "ERROR: " + e
-
- #{t['desc'].inspect}: (result) ->
- assert.equal(result, #{t['expected']})
-
- COFFEE
- end
-
- f.puts("suite.export(module)")
- end
-end

0 comments on commit 69ac70a

Please sign in to comment.