$:.unshift File.expand_path('../../../lib', __FILE__) require 'json' desc 'Convert tests.json into rspec tests' task :convert do spec = "describe 'haml-spec' do\n" contexts = JSON.parse(File.read(File.expand_path('../../../haml-spec/tests.json', __FILE__))) 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| options = Hash[(test[:config] || {}).map {|x, y| [x.to_sym, y]}] spec = " specify \"#{test[:name]}\" do\n" spec += " html = %q{#{test[:html]}}\n" spec += " haml = %q{#{test[:haml]}}\n" spec += " locals = Hash[#{test[:locals] || {}}.map {|x, y| [x.to_sym, y]}]\n" spec += " options = #{options}\n" spec += " options[:format] = :#{options[:format]}\n" if options.key?(:format) spec += " engine = Hamlit::Template.new(options) { haml }\n" spec += " result = engine.render(Object.new, locals)\n" spec += " expect(result.strip).to eq(html)\n" spec += " end\n" }.join("\n") spec += " end\n" end spec += "end\n" File.write('ruby_haml_spec.rb', spec) end task default: :convert