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
|
|
|
|
2015-03-29 22:13:43 +09:00
|
|
|
def generate_spec(mode)
|
2015-03-27 16:40:15 +09:00
|
|
|
spec = <<-SPEC.unindent
|
|
|
|
require "haml"
|
|
|
|
|
2015-03-29 22:27:11 +09:00
|
|
|
# This is a spec converted by haml-spec.
|
2015-03-29 22:08:23 +09:00
|
|
|
# See: https://github.com/haml/haml-spec
|
2015-05-16 12:23:03 +09:00
|
|
|
describe "haml #{mode} mode with ecape_html" do
|
2015-04-12 18:53:59 +09:00
|
|
|
DEFAULT_OPTIONS = { ugly: true, escape_html: true }.freeze
|
2015-03-27 16:40:15 +09:00
|
|
|
|
2015-04-12 18:53:59 +09:00
|
|
|
def assert_haml(haml, locals, options)
|
|
|
|
engine = Haml::Engine.new(haml, DEFAULT_OPTIONS.merge(options))
|
2015-05-16 12:23:03 +09:00
|
|
|
hamlit = Hamlit::Template.new(options) { haml }
|
2015-04-12 18:53:59 +09:00
|
|
|
expect(hamlit.render(Object.new, locals)).to eq(engine.render(Object.new, locals))
|
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-29 22:13:43 +09:00
|
|
|
generate_specify(test, locals, options, mode)
|
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-04-12 18:25:55 +09:00
|
|
|
File.write("hamlit/haml_spec.rb", spec)
|
2015-03-29 22:13:43 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_specify(test, locals, options, mode)
|
|
|
|
<<-SPEC
|
|
|
|
specify \"#{test[:name]}\" do
|
|
|
|
haml = %q{#{test[:haml]}}
|
|
|
|
html = %q{#{test[:html]}}
|
|
|
|
locals = #{locals}
|
|
|
|
options = #{options}
|
2015-04-12 18:53:59 +09:00
|
|
|
assert_haml(haml, locals, options)
|
2015-03-29 22:13:43 +09:00
|
|
|
end
|
|
|
|
SPEC
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Convert tests.json into ugly rspec tests'
|
|
|
|
task :ugly do
|
|
|
|
generate_spec(:ugly)
|
|
|
|
end
|
|
|
|
|
2015-04-12 18:25:55 +09:00
|
|
|
task default: :ugly
|