Sha256: abeca147d8bec34f69ad0ddcf6197d0a0578fa624e0a856a1b3bde403ea88431

Contents?: true

Size: 1.89 KB

Versions: 30

Compression:

Stored size: 1.89 KB

Contents

$:.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 ecape_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(options) { haml }
        expect(hamlit.render(Object.new, locals)).to eq(engine.render(Object.new, locals))
      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

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
hamlit-1.7.2 spec/Rakefile
hamlit-1.7.1 spec/Rakefile
hamlit-1.7.0 spec/Rakefile
hamlit-1.6.7 spec/Rakefile
hamlit-1.6.6 spec/Rakefile
hamlit-1.6.5 spec/Rakefile
hamlit-1.6.4 spec/Rakefile
hamlit-1.6.3 spec/Rakefile
hamlit-1.6.2 spec/Rakefile
hamlit-1.6.1 spec/Rakefile
hamlit-1.6.0 spec/Rakefile
hamlit-1.5.9 spec/Rakefile
hamlit-1.5.8 spec/Rakefile
hamlit-1.5.7 spec/Rakefile
hamlit-1.5.6 spec/Rakefile
hamlit-1.5.5 spec/Rakefile
hamlit-1.5.4 spec/Rakefile
hamlit-1.5.3 spec/Rakefile
hamlit-1.5.2 spec/Rakefile
hamlit-1.5.1 spec/Rakefile