1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
require 'mustache'
require 'tmpdir'
require 'yaml'
require 'minitest/autorun'
# Calls appropriate method on YAML. See: https://gist.github.com/tenderlove/958999ab4240b93bd3cd
YAML.add_domain_type(nil, 'code') { |_, val| eval(val['ruby']) }
# A simple base class for Mustache specs.
# Creates a partials directory, then points a (dynamic) subclass of Mustache at
# that directory before each test; the partials directory is destroyed after
# each test is run.
class MustacheSpec < Minitest::Test
def setup
@partials = File.join(File.dirname(__FILE__), 'partials')
Dir.mkdir(@partials)
@Mustache = Class.new(Mustache)
@Mustache.template_path = @partials
end
def teardown
Dir[File.join(@partials, '*')].each { |file| File.delete(file) }
Dir.rmdir(@partials)
end
# Extracts the partials from the test, and dumps them into the partials
# directory for inclusion.
def setup_partials(test)
(test['partials'] || {}).each do |name, content|
File.open(File.join(@partials, "#{name}.mustache"), 'w') do |f|
f.print(content)
end
end
end
# Asserts equality between the rendered template and the expected value,
# printing additional context data on failure.
def assert_mustache_spec(test)
actual = @Mustache.render(test['template'], test['data'])
assert_equal test['expected'], actual, "" <<
"#{ test['desc'] }\n" <<
"Data: #{ test['data'].inspect }\n" <<
"Template: #{ test['template'].inspect }\n" <<
"Partials: #{ (test['partials'] || {}).inspect }\n"
end
def test_noop; assert(true); end
end
spec_files = File.join(File.dirname(__FILE__), '..', 'ext', 'spec', 'specs', '*.yml')
Dir[spec_files].each do |file|
spec = YAML.load_file(file)
klass_name = "Test" + File.basename(file, ".yml").sub(/~/, '').capitalize
instance_eval "class ::#{klass_name} < MustacheSpec; end"
test_suite = Kernel.const_get(klass_name)
test_suite.class_eval do
spec['tests'].each do |test|
define_method :"test_spec - #{test['name']}" do
setup_partials(test)
assert_mustache_spec(test)
end
end
end
end
|