require "assert"
require "stringio"
require 'undies/node_stack'
require "undies/template"
class Undies::Template
class BasicTests < Assert::Context
desc 'a template'
before do
@src = Undies::Source.new(Proc.new {})
@output = Undies::Output.new(@outstream = StringIO.new(@out = ""))
@t = Undies::Template.new(@src, {}, @output)
end
subject { @t }
should have_class_method :source_stack, :node_stack, :flush, :escape_html
should have_instance_methods :to_s, :element, :tag
should have_instance_methods :_, :__
should have_instance_methods :__yield, :__partial
should have_instance_methods :__push, :__pop, :__flush
should have_instance_methods :__attrs
should "know it's node stack" do
assert_kind_of Undies::NodeStack, subject.class.node_stack(subject)
end
should "complain if creating a template with no Output obj" do
assert_raises ArgumentError do
Undies::Template.new(@src, {})
end
end
should "default the data to an empty hash if none provided" do
assert_nothing_raised do
Undies::Template.new(@src, @output)
end
end
should "default the source to an empty Proc source if none provided" do
assert_nothing_raised do
Undies::Template.new(@output)
end
assert_equal "", @out
end
end
class NodeTests < BasicTests
desc "with text data"
before do
@data = "stuff & more stuff"
end
should "add the text escaped using the '_' method" do
subject._ @data
subject.__flush
assert_equal subject.class.escape_html(@data), @out
end
should "add the text un-escaped using the '__' method" do
subject.__ @data
subject.__flush
assert_equal @data, @out
end
should "add empty string nodes using '__' and '_' methods with no args" do
subject._
subject.__
subject.__flush
assert_equal "", @out
end
end
class ElementTests < BasicTests
desc "using the 'element' helper"
should "stream element output" do
subject.element(:br)
subject.__flush
assert_equal "
", @out
end
should "alias it with 'tag'" do
subject.tag(:br)
subject.__flush
assert_equal "
", @out
end
should "respond to underscore-prefix methods" do
assert subject.respond_to?(:_br)
end
should "respond to underscore-prefix methods as element methods" do
subject._br
subject.__flush
assert_equal "
", @out
end
should "not respond to element methods without an underscore-prefix" do
assert !subject.respond_to?(:div)
assert_raises NoMethodError do
subject.div
end
end
end
class BuildAttrsTests < BasicTests
should "modify attributes during a build using the __attrs method" do
subject.element(:div)
subject.__push
subject.__attrs :class => 'test'
subject.__pop
subject.__flush
assert_equal "
hi there
friend
hi
" assert_equal @expected_output, @output @template._p { @template._ "action" } @template.__pop @template.__flush @expected_output = "hi
action