2015-03-23 00:32:40 +09:00
|
|
|
$:.unshift File.expand_path('../../../lib', __FILE__)
|
|
|
|
|
|
|
|
require 'json'
|
|
|
|
|
|
|
|
desc 'Convert tests.json into rspec tests'
|
|
|
|
task :convert do
|
2015-03-23 00:42:05 +09:00
|
|
|
spec = "describe 'haml-spec' do\n"
|
2015-03-23 00:32:40 +09:00
|
|
|
|
|
|
|
contexts = JSON.parse(File.read(File.expand_path('../../../haml-spec/tests.json', __FILE__)))
|
2015-03-23 00:42:05 +09:00
|
|
|
contexts.each_with_index do |context, index|
|
|
|
|
spec += "\n" if index != 0
|
|
|
|
spec += " context \"#{context[0]}\" do\n"
|
|
|
|
|
|
|
|
tests = []
|
|
|
|
context[1].each do |name, test|
|
2015-03-23 00:32:40 +09:00
|
|
|
tests << {
|
2015-03-23 00:42:05 +09:00
|
|
|
name: name,
|
2015-03-23 00:32:40 +09:00
|
|
|
html: test['html'],
|
|
|
|
haml: test['haml'],
|
|
|
|
locals: test['locals'],
|
|
|
|
config: test['config'],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-03-23 00:42:05 +09:00
|
|
|
spec += tests.map { |test|
|
|
|
|
options = Hash[(test[:config] || {}).map {|x, y| [x.to_sym, y]}]
|
|
|
|
|
|
|
|
spec = " specify \"#{test[:name]}\" do\n"
|
|
|
|
spec += " html = %q{#{test[:html]}}\n"
|
|
|
|
spec += " haml = %q{#{test[:haml]}}\n"
|
|
|
|
spec += " locals = Hash[#{test[:locals] || {}}.map {|x, y| [x.to_sym, y]}]\n"
|
|
|
|
spec += " options = #{options}\n"
|
2015-03-23 00:47:34 +09:00
|
|
|
spec += " options[:format] = :#{options[:format]}\n" if options.key?(:format)
|
2015-03-23 00:42:05 +09:00
|
|
|
spec += " engine = Hamlit::Template.new(options) { haml }\n"
|
|
|
|
spec += " result = engine.render(Object.new, locals)\n"
|
|
|
|
spec += " expect(result.strip).to eq(html)\n"
|
|
|
|
spec += " end\n"
|
|
|
|
}.join("\n")
|
2015-03-23 00:32:40 +09:00
|
|
|
spec += " end\n"
|
2015-03-23 00:42:05 +09:00
|
|
|
end
|
|
|
|
|
2015-03-23 00:32:40 +09:00
|
|
|
spec += "end\n"
|
|
|
|
File.write('ruby_haml_spec.rb', spec)
|
|
|
|
end
|
|
|
|
|
|
|
|
task default: :convert
|