Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mustache Spec compliance #99

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "ext/spec"]
path = ext/spec
url = git://github.com/mustache/spec.git
1 change: 1 addition & 0 deletions ext/spec
Submodule spec added at bf6288
53 changes: 53 additions & 0 deletions test/compliance_spec.rb
@@ -0,0 +1,53 @@
require 'rubygems'
require 'yaml'
require 'json'

YAML::add_builtin_type('code') do |_, value|
value['js'].tap do |func|
def func.to_json(_)
"function() { return #{self}; }"
end
end
end

__DIR__ = File.dirname(__FILE__)

testnames = Dir.glob(__DIR__ + '/../ext/spec/specs/[^~]*.yml').map do |name|
File.basename name, '.yml'
end

def load_tests(dir, name)
file = File.join(dir, '..', 'ext', 'spec', 'specs', "#{name}.yml")
return YAML.load_file(file)['tests']
end

describe "Mustache Spec compliance" do
before(:all) do
@mustache = File.read(__DIR__ + "/../mustache.js")
end

testnames.each do |testname|
load_tests(__DIR__, testname).each do |test|
describe test['name'] do
it test['desc'] do
runner = <<-JS
#{@mustache}
print(Mustache.to_html(
#{test['template'].to_json},
#{test['data'].to_json},
#{test['partials'].to_json}
));
JS

# #print appends a newline
run_js(runner).should == test['expected'] + "\n"
end
end
end
end

def run_js(js)
File.open("runner.js", 'w') {|f| f << js}
`js runner.js`
end
end