2015-03-28 10:38:05 +09:00
|
|
|
$:.unshift File.expand_path('../../lib', __FILE__)
|
2015-03-23 00:32:40 +09:00
|
|
|
|
|
|
|
require 'json'
|
2015-03-27 16:40:15 +09:00
|
|
|
require 'unindent'
|
2015-03-28 10:38:05 +09:00
|
|
|
require 'open-uri'
|
2015-03-27 16:40:15 +09:00
|
|
|
|
|
|
|
def generate_spec(test, locals, options)
|
|
|
|
<<-SPEC
|
|
|
|
specify \"#{test[:name]}\" do
|
|
|
|
haml = %q{#{test[:haml]}}
|
2015-03-28 10:53:56 +09:00
|
|
|
html = %q{#{test[:html]}}
|
2015-03-27 16:40:15 +09:00
|
|
|
locals = #{locals}
|
|
|
|
options = #{options}
|
|
|
|
assert_ugly(haml, locals, options)
|
|
|
|
end
|
|
|
|
SPEC
|
|
|
|
end
|
2015-03-23 00:32:40 +09:00
|
|
|
|
|
|
|
desc 'Convert tests.json into rspec tests'
|
|
|
|
task :convert do
|
2015-03-27 16:40:15 +09:00
|
|
|
spec = <<-SPEC.unindent
|
|
|
|
require "haml"
|
|
|
|
|
2015-03-27 21:40:44 +09:00
|
|
|
describe "haml" do
|
2015-03-27 16:40:15 +09:00
|
|
|
def assert_pretty(haml, locals, options)
|
|
|
|
engine = Haml::Engine.new(haml, options)
|
|
|
|
hamlit = Hamlit::Template.new(options) { haml }
|
2015-03-27 21:21:32 +09:00
|
|
|
expect(hamlit.render(Object.new, locals)).to eq(engine.render(Object.new, locals))
|
2015-03-27 16:40:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_ugly(haml, locals, options)
|
2015-03-27 17:13:08 +09:00
|
|
|
assert_pretty(haml, locals, { ugly: true }.merge(options))
|
2015-03-27 16:40:15 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
SPEC
|
2015-03-23 00:32:40 +09:00
|
|
|
|
2015-03-28 10:38:05 +09:00
|
|
|
url = 'https://raw.githubusercontent.com/haml/haml-spec/master/tests.json'
|
|
|
|
contexts = JSON.parse(open(url).read)
|
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|
|
2015-03-27 13:14:20 +09:00
|
|
|
locals = Hash[(test[:locals] || {}).map {|x, y| [x.to_sym, y]}]
|
2015-03-23 00:42:05 +09:00
|
|
|
options = Hash[(test[:config] || {}).map {|x, y| [x.to_sym, y]}]
|
2015-03-27 13:14:20 +09:00
|
|
|
options[:format] = options[:format].to_sym if options[:format]
|
2015-03-23 00:42:05 +09:00
|
|
|
|
2015-03-27 16:40:15 +09:00
|
|
|
generate_spec(test, locals, options)
|
2015-03-23 00:42:05 +09:00
|
|
|
}.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"
|
2015-03-28 10:38:05 +09:00
|
|
|
File.write('haml_spec.rb', spec)
|
2015-03-23 00:32:40 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
task default: :convert
|