$:.unshift File.expand_path('../../lib', __FILE__) require 'json' require 'unindent' require 'open-uri' def generate_spec(test, locals, options) <<-SPEC specify \"#{test[:name]}\" do haml = %q{#{test[:haml]}} html = %q{#{test[:html]}} locals = #{locals} options = #{options} assert_ugly(haml, locals, options) end SPEC end desc 'Convert tests.json into rspec tests' task :convert do spec = <<-SPEC.unindent require "haml" describe "haml" do def assert_pretty(haml, locals, options) engine = Haml::Engine.new(haml, options) hamlit = Hamlit::Template.new(options) { haml } expect(hamlit.render(Object.new, locals)).to eq(engine.render(Object.new, locals)) end def assert_ugly(haml, locals, options) assert_pretty(haml, locals, { ugly: true }.merge(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_spec(test, locals, options) }.join("\n") spec += " end\n" end spec += "end\n" File.write('haml_spec.rb', spec) end task default: :convert