require 'test_helper' require 'pry' class HandlerTest < ActiveSupport::TestCase GARAHandler = Gara::Template::Handler.new DEFAULT_DOCTYPE = "" A_LINK = %Q(link) class LookupContext def disable_cache yield end def find_template(*args) end attr_accessor :formats end class Context def initialize @output_buffer = "original" @virtual_path = nil end def lookup_context @lookup_context ||= LookupContext.new end def link_helper A_LINK end end def new_template(body = " h1 'Hello' ", details = { format: :html }) ActionView::Template.new(body, "hello template", details.fetch(:handler) { GARAHandler }, {:virtual_path => "hello"}.merge!(details)) end def render(locals = {}) output = @template.render(@context, locals) output end def with_doctype(body, alt_doctype = nil) "#{alt_doctype||DEFAULT_DOCTYPE}\n#{body}" end def setup @context = Context.new end test "our handler is registered" do handler = ActionView::Template.registered_template_handler("gara") assert_equal Gara::Template::Handler, handler end test "html generates

Hello

by default" do @template = new_template assert_equal "

Hello

", render end test "nesting elements with ruby block structure" do @template = new_template("ul { li 'one' ; li 'two' ; li 'three' }") assert_equal "", render end test "class names" do @template = new_template("p.whatever.another 'Lorum Ipsum' ") assert_equal "

Lorum Ipsum

", render end test "other attributes" do @template = new_template("p('Lorum Ipsum', style: 'align: right;')") assert_equal "

Lorum Ipsum

", render end test "real document has doctype and newline" do @template = new_template("html { body { h1 \"hello\" } }") assert_equal with_doctype("\n \n

hello

\n \n\n"), render end test "locals work" do @template = new_template "h1 title" @template.locals = [:title] assert_equal "

Foo

", render(title: 'Foo') end test "string in block works" do @template = new_template "h1 { 'foo' } " assert_equal "

foo

", render end test "helpers returning html when alone in a block" do @template = new_template("li { link_helper } ") assert_equal "
  • #{A_LINK}
  • ", render end test "helpers returning html work in sequence within a block" do @template = new_template("li { link_helper ; link_helper } ") assert_equal "
  • \n#{A_LINK}#{A_LINK}\n
  • ", render end end