1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/test/test_helper.rb

88 lines
2.1 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'
require 'hamlit/template'
2015-10-10 23:13:03 -04: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
2015-10-24 08:43:34 -04:00
module RenderAssertion
2015-10-24 07:52:15 -04:00
def assert_render(haml, html, options = {})
2015-10-24 08:48:01 -04:00
options = options.dup
options.delete(:compatible_only)
options.delete(:error_with)
2015-10-24 09:23:25 -04:00
options = { escape_html: true, ugly: true }.merge(options)
2015-10-25 10:33:36 -04:00
unless options.delete(:skip_unindent)
haml, html = haml.unindent, html.unindent
end
2015-10-24 09:18:12 -04:00
assert_equal html, render(haml, options)
2015-10-13 07:45:07 -04:00
end
2015-10-24 08:43:34 -04:00
2015-10-24 09:23:25 -04:00
def render(text, options = {}, &block)
2015-10-24 08:43:34 -04:00
scope = options.delete(:scope) || Object.new
locals = options.delete(:locals) || {}
2015-10-24 09:23:25 -04:00
eval Hamlit::HamlEngine.new(text, options).precompiled
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) || {}
engine = Hamlit::HamlEngine.new(text, options)
return engine.to_html(base) if base
engine.to_html(scope, locals, &block)
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)
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 self.error(*args)
Haml::Error.message(*args)
end
end