$:.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" 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_specify(test, locals, options, mode) }.join("\n") spec += " end\n" end spec += "end\n" File.write("hamlit/#{mode}_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_#{mode}(haml, locals, options) end SPEC end desc 'Convert tests.json into ugly rspec tests' task :ugly do generate_spec(:ugly) end desc 'Convert tests.json into pretty rspec tests' task :pretty do generate_spec(:pretty) end task default: [:ugly, :pretty]