require 'test_helper' class HandlerTest < ActiveSupport::TestCase DEFAULT_DOCTYPE = "" A_LINK = %Q(link) class LookupContext def disable_cache yield end def find_template(*args) end attr_accessor :formats end class Context include ActionView::Context include ActionView::Helpers::TagHelper def initialize(*args) @output_buffer = "original" @virtual_path = nil end def lookup_context @lookup_context ||= LookupContext.new end def concat(string) @output_buffer << string end def capture(&block) block.call end def assigns {} end def link_helper A_LINK.html_safe end end def new_template(body = " h1 'Hello' ", details = { format: :html }) ActionView::Template.new(body, "hello template", details.fetch(:handler) { ExpressTemplates::Template::Handler.new }, {: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 controller = Object.new class << controller def view_context_class ; Context ; end end @context = Context.new(nil, {}, controller) end test "our handler is registered" do handler = ActionView::Template.registered_template_handler("et") assert_equal ExpressTemplates::Template::Handler, handler end test "html generates

Hello

by default" do @template = new_template result = render assert_equal "

Hello

\n", result end test "nesting elements with ruby block structure" do @template = new_template("ul { li 'one' ; li 'two' ; li 'three' }") assert_equal "\n", render end # TODO?: Does not work with arbre # test "class names" do # @template = new_template("p.whatever.another 'Lorum Ipsum' ") # assert_equal "

Lorum Ipsum

\n", render # end test "string in block works" do @template = new_template "h1 { 'foo' } " assert_equal "

foo

\n", 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 "other attributes" do @template = new_template("para('Lorum Ipsum', style: 'align: right;')") assert_equal "

Lorum Ipsum

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

Foo

\n", render(my_title: 'Foo') end test "helpers returning html when alone in a block" do @template = new_template("li { link_helper } ") assert_equal "
  • #{A_LINK}
  • \n", 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 test "raises warning if template has conditional logic" do temp = "dd(class: 'date') { if blog_post.publish_at blog_post.publish_at else link_to('Edit', '#'') end }" keywords = %w(if unless until case for do loop while) tokens = [] if Ripper.lex(temp).select do |element| element[1]==:on_kw end.each { |match| tokens.push(match) if keywords.include? match[2] } tokens.each do |first| out, @err = capture_io do warn 'foo' end end end assert_equal @err, "foo\n" end end