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:
parent
e1906e5a61
commit
a25c31bf6b
4 changed files with 48 additions and 2 deletions
14
Rakefile
14
Rakefile
|
@ -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
|
||||
|
|
|
@ -21,4 +21,15 @@ module Hamlit
|
|||
filter :StaticMerger
|
||||
use :Generator, -> { options[:generator] }
|
||||
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
|
||||
|
|
|
@ -7,7 +7,7 @@ module Hamlit
|
|||
end
|
||||
|
||||
def call(template)
|
||||
Haml::Parser.new(template, {}).parse
|
||||
Haml::Parser.new(template, Haml::Options.defaults).parse
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
23
test/haml-spec/hamlit_test.rb
Normal file
23
test/haml-spec/hamlit_test.rb
Normal 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
|
Loading…
Reference in a new issue