haml--haml/test/test_helper.rb

104 lines
2.8 KiB
Ruby
Raw Normal View History

2015-10-24 07:44:18 -04:00
require 'unindent'
2015-10-13 07:45:07 -04:00
require 'bundler/setup'
require 'minitest/autorun'
require 'action_pack'
require 'action_controller'
require 'action_view'
require 'rails'
2015-10-24 03:48:26 -04:00
require 'hamlit'
2015-11-20 12:55:12 -05:00
require 'haml'
2015-10-24 03:48:26 -04:00
2015-11-13 07:27:42 -05:00
# require 'minitest/reporters'
# Minitest::Reporters.use!
2015-10-13 07:45:07 -04:00
BASE_TEST_CLASS = if defined?(Minitest::Test)
Minitest::Test
else
MiniTest::Unit::TestCase
end
module Declarative
def test(name, &block)
2015-10-24 08:43:34 -04:00
define_method("test_ #{name}", &block)
2015-10-24 07:44:18 -04:00
end
end
module RenderHelper
def assert_render(expected, haml, options = {})
2015-11-21 08:17:30 -05:00
actual = render_hamlit(haml, options)
assert_equal expected, actual
2015-10-13 07:45:07 -04:00
end
2015-10-24 08:43:34 -04:00
2015-11-21 08:17:30 -05:00
def render_haml(haml, options = {})
options = options.dup
locals = options.delete(:locals) || {}
haml_options = { escape_html: true, escape_attrs: true, ugly: true }
Haml::Engine.new(haml, haml_options.merge(options)).render(Object.new, locals)
end
def render_hamlit(haml, options = {})
options = options.dup
locals = options.delete(:locals) || {}
Hamlit::Template.new(options) { haml }.render(Object.new, locals)
2015-11-07 03:51:17 -05:00
end
def assert_haml(haml, options = {})
2015-11-21 08:17:30 -05:00
expected = render_haml(haml, options)
actual = render_hamlit(haml, options)
assert_equal expected, actual
2015-10-24 08:43:34 -04:00
end
2015-10-13 07:45:07 -04:00
end
class Haml::TestCase < BASE_TEST_CLASS
extend Declarative
def render(text, options = {}, base = nil, &block)
2015-10-25 03:14:55 -04:00
options = { escape_html: false }.merge(options) # incompatible default
2015-10-13 07:45:07 -04:00
scope = options.delete(:scope) || Object.new
locals = options.delete(:locals) || {}
options.delete(:ugly)
eval Hamlit::Engine.new(options).call(text)
end
2015-11-21 03:30:40 -05:00
def assert_haml_ugly(text, options = {}, base = nil)
haml_base = { ugly: true, escape_html: true, escape_attrs: true }
hamlit_base = { escape_html: true }
scope = options.delete(:scope) || Object.new
locals = options.delete(:locals) || {}
haml_result = Haml::Engine.new(text, haml_base.merge(options)).render(scope, locals)
hamlit_result = Hamlit::Template.new(hamlit_base.merge(options)) { text }.render(scope, locals)
assert_equal haml_result, hamlit_result
2015-10-13 07:45:07 -04:00
end
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)
2015-11-21 03:14:33 -05:00
Hamlit::HamlUtil.silence_warnings(&block)
2015-10-13 07:45:07 -04:00
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 self.error(*args)
2015-11-21 03:14:33 -05:00
Hamlit::HamlError.message(*args)
2015-10-13 07:45:07 -04:00
end
end