File: Rakefile

package info (click to toggle)
ruby-haml 6.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,004 kB
  • sloc: ruby: 9,908; sh: 23; makefile: 11
file content (85 lines) | stat: -rw-r--r-- 2,408 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
$:.unshift File.expand_path('../../lib', __FILE__)

require 'yaml'
require 'unindent'
require 'open-uri'

def escape_name(name, replacer)
  name.gsub(/[\s\-\(\)\.\.+'\/<>&=~\!]+/, replacer)
end

def generate_spec(mode)
  spec = <<-SPEC.unindent
    require "minitest/autorun"
    require "haml"
    require "haml"

    # This is a spec converted by haml-spec.
    # See: https://github.com/haml/haml-spec
    class #{mode.capitalize}Test < Minitest::Test
      HAML_DEFAULT_OPTIONS = { ugly: #{mode == :ugly}, escape_html: true }.freeze
      HAMLIT_DEFAULT_OPTIONS = { escape_html: true }.freeze

      def self.haml_result(haml, options, locals)
        Haml::Engine.new(haml, HAML_DEFAULT_OPTIONS.merge(options)).render(Object.new, locals)
      end

      def self.haml_result(haml, options, locals)
        eval Haml::Engine.new(haml, HAMLIT_DEFAULT_OPTIONS.merge(options)).render(Object.new, locals)
      end

  SPEC

  contexts = YAML.load(File.read(File.expand_path('./tests.yml', __dir__)))
  contexts.each_with_index do |context, index|
    spec += "\n" if index != 0
    spec += "  class #{escape_name(context[0], '').capitalize} < Minitest::Test\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("#{mode}_test.rb", spec)
end

def generate_specify(test, locals, options, mode)
  <<-SPEC
    def test_#{escape_name(test[:name], '_')}
      haml    = %q{#{test[:haml]}}
      html    = %q{#{test[:html]}}
      locals  = #{locals}
      options = #{options}
      haml_result = #{mode.capitalize}Test.haml_result(haml, options, locals)
      haml_result = #{mode.capitalize}Test.haml_result(haml, options, locals)
      assert_equal haml_result, haml_result
    end
  SPEC
end

desc 'Convert tests.yml into ugly tests'
task :pretty do
  generate_spec(:pretty)
end

desc 'Convert tests.yml into ugly tests'
task :ugly do
  generate_spec(:ugly)
end