From a25c31bf6b4351e9c3dfcea8d70916f1e5ea04a4 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Wed, 7 Oct 2015 00:18:41 +0900 Subject: [PATCH] Create Haml::Engine compatible class --- Rakefile | 14 +++++++++++++- lib/hamlit/engine.rb | 11 +++++++++++ lib/hamlit/parser.rb | 2 +- test/haml-spec/hamlit_test.rb | 23 +++++++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/haml-spec/hamlit_test.rb diff --git a/Rakefile b/Rakefile index 29955274..2d083ec4 100644 --- a/Rakefile +++ b/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 diff --git a/lib/hamlit/engine.rb b/lib/hamlit/engine.rb index 4f769229..9e3795d5 100644 --- a/lib/hamlit/engine.rb +++ b/lib/hamlit/engine.rb @@ -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 diff --git a/lib/hamlit/parser.rb b/lib/hamlit/parser.rb index 3b6d98fc..19876281 100644 --- a/lib/hamlit/parser.rb +++ b/lib/hamlit/parser.rb @@ -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 diff --git a/test/haml-spec/hamlit_test.rb b/test/haml-spec/hamlit_test.rb new file mode 100644 index 00000000..5e0e7dc0 --- /dev/null +++ b/test/haml-spec/hamlit_test.rb @@ -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