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

Create Haml::Engine compatible class

This commit is contained in:
Takashi Kokubun 2015-10-07 00:18:41 +09:00
parent e1906e5a61
commit a25c31bf6b
4 changed files with 48 additions and 2 deletions

View file

@ -1 +1,13 @@
require "bundler/gem_tasks" require 'bundler/gem_tasks'
require 'rake/testtask'
task default: :test
Rake::TestTask.new do |t|
t.libs << 'lib' << 'test'
files = Dir['test/*_test.rb']
files << 'test/haml-spec/hamlit_test.rb'
t.test_files = files
t.warning = true
t.verbose = true
end

View file

@ -21,4 +21,15 @@ module Hamlit
filter :StaticMerger filter :StaticMerger
use :Generator, -> { options[:generator] } use :Generator, -> { options[:generator] }
end end
class HamlEngine
def initialize(template, options = {})
@template = template
@options = options
end
def render(scope = Object.new, locals = {}, &block)
eval Engine.new.call(@template)
end
end
end end

View file

@ -7,7 +7,7 @@ module Hamlit
end end
def call(template) def call(template)
Haml::Parser.new(template, {}).parse Haml::Parser.new(template, Haml::Options.defaults).parse
end end
end end
end end

View file

@ -0,0 +1,23 @@
require "rubygems"
require "minitest/autorun"
require "json"
require "hamlit"
class HamlTest < MiniTest::Unit::TestCase
contexts = JSON.parse(File.read(File.dirname(__FILE__) + "/tests.json"))
contexts.each do |context|
context[1].each do |name, test|
define_method("test_spec: #{name} (#{context[0]})") do
html = test["html"]
haml = test["haml"]
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.key?(:format)
engine = Hamlit::HamlEngine.new(haml, options)
result = engine.render(Object.new, locals)
assert_equal html, result.strip
end
end
end
end