File: test_helper.rb

package info (click to toggle)
ruby-haml 6.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,004 kB
  • sloc: ruby: 9,908; sh: 23; makefile: 11
file content (106 lines) | stat: -rw-r--r-- 2,777 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require 'unindent'
require 'minitest/autorun'
require 'action_pack'
require 'action_controller'
require 'action_view'
require 'rails'

require 'haml'

# Protect Minitest from Rails' Minitest plugin: https://github.com/rails/rails/pull/19571
ENV['BACKTRACE'] ||= '1'

# require 'minitest/reporters'
# Minitest::Reporters.use!

BASE_TEST_CLASS = if defined?(Minitest::Test)
                    Minitest::Test
                  else
                    Minitest::Unit::TestCase
                  end

module Declarative
  def test(name, &block)
    define_method("test_ #{name}", &block)
  end
end

module RenderHelper
  def assert_render(expected, haml, options = {})
    actual = render_haml(haml, options)
    assert_equal expected, actual
  end

  def render_haml(haml, options = {})
    options = options.dup
    locals  = options.delete(:locals) || {}
    Haml::Template.new(options) { haml }.render(Object.new, locals)
  end

  def assert_haml(haml, options = {})
    if RUBY_ENGINE == 'truffleruby'
      skip 'truffleruby cannot run Haml'
    end
    expected = render_haml(haml, options)
    actual = render_haml(haml, options)
    assert_equal expected, actual
  end
end

class Haml::TestCase < BASE_TEST_CLASS
  extend Declarative

  def render(text, options = {}, base = nil, &block)
    options = { escape_html: false }.merge(options) # incompatible default
    %i[attr_wrapper cdata suppress_eval ugly].each { |opt| options.delete(opt) }
    scope = (base ? base.instance_eval { binding } : nil)
    eval Haml::Engine.new(options).call(text), scope
  end

  def render_template(text, options = {})
    haml_base = { escape_html: true }
    scope  = options.delete(:scope) || Object.new
    locals = options.delete(:locals) || {}
    Haml::Template.new(haml_base.merge(options)) { text }.render(scope, locals)
  end
  # TODO: fix the usages of assert_haml_ugly
  alias :assert_haml_ugly :render_template

  def assert_warning(message)
    the_real_stderr, $stderr = $stderr, StringIO.new
    yield

    if message.is_a?(Regexp)
      assert_match message, $stderr.string.strip
    else
      assert_equal message.strip, $stderr.string.strip
    end
  ensure
    $stderr = the_real_stderr
  end

  def silence_warnings(&block)
    Haml::Util.silence_warnings(&block)
  end

  def assert_raises_message(klass, message)
    yield
  rescue Exception => e
    assert_instance_of(klass, e)
    assert_equal(message, e.message)
  else
    flunk "Expected exception #{klass}, none raised"
  end

  def action_view_instance
    Class.new(ActionView::Base) do
      def compiled_method_container
        self.class
      end
    end.new(ActionView::LookupContext.new(''), {}, ActionController::Base.new)
  end

  def self.error(*args)
    Haml::Error.message(*args)
  end
end