$:.unshift File.expand_path('../../lib', __FILE__) require 'json' require 'unindent' require 'open-uri' def generate_spec(mode) spec = <<-SPEC.unindent require "haml" # This is a spec converted by haml-spec. # See: https://github.com/haml/haml-spec describe "haml #{mode} mode with escape_html" do DEFAULT_OPTIONS = { ugly: true, escape_html: true }.freeze def assert_haml(haml, locals, options) engine = Haml::Engine.new(haml, DEFAULT_OPTIONS.merge(options)) hamlit = Hamlit::Template.new(suppress_warnings(options)) { haml } expect(hamlit.render(Object.new, locals)).to eq(engine.render(Object.new, locals)) end # NOTE: Hamlit only supports escape_html mode. # Thus the option is not supporeted and prints noisy warnings. def suppress_warnings(options) options = options.dup options.delete(:escape_html) options end SPEC url = 'https://raw.githubusercontent.com/haml/haml-spec/master/tests.json' contexts = JSON.parse(open(url).read) 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| tests << { name: name, html: test['html'], haml: test['haml'], locals: test['locals'], config: test['config'], } end spec += tests.map { |test| locals = Hash[(test[:locals] || {}).map {|x, y| [x.to_sym, y]}] options = Hash[(test[:config] || {}).map {|x, y| [x.to_sym, y]}] options[:format] = options[:format].to_sym if options[:format] generate_specify(test, locals, options, mode) }.join("\n") spec += " end\n" end spec += "end\n" File.write("hamlit/haml_spec.rb", spec) 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} assert_haml(haml, locals, options) end SPEC end desc 'Convert tests.json into ugly rspec tests' task :ugly do generate_spec(:ugly) end task default: :ugly