require "assert" require 'undies/io' require 'undies/element_node' require 'undies/element' class Undies::ElementNode class BasicTests < Assert::Context desc 'an element node' before do # io test with :pp 1 so we can test newline insertion # io test with level 1 so we can test element start tag writing @io = Undies::IO.new(@out = "", :pp => 1, :level => 2) @e = Undies::Element.open(:div, "hi") @en = Undies::ElementNode.new(@io, @e) end subject { @en } should have_readers :io, :element, :cached should have_instance_methods :attrs, :text, :element_node should have_instance_methods :partial, :flush, :push, :pop, :to_s should "know its IO" do assert_equal @io, subject.io end should "know its element" do assert_equal @e, subject.element end should "have nothing cached by default" do assert_nil subject.cached end should "complain if trying to add text in a build" do assert_raises Undies::ElementAPIError do subject.text 'blah' end end end class AddContentTests < BasicTests desc "adding content" before do @text1 = "some raw markup" @text2 = "more raw markup" @elem1 = Undies::Element::Open.new(:strong, "blah") @elem2 = Undies::Element::Closed.new(:br).meh @en1 = Undies::ElementNode.new(@io, @elem1) @en2 = Undies::ElementNode.new(@io, @elem2) end end class AddContentStartTagWrittenTests < AddContentTests desc "(the start tag has already been written)" before do subject.instance_variable_set("@start_tag_written", true) end end class ElementStartTagWrittenTests < AddContentStartTagWrittenTests desc "using the element_node meth" should "cache any element node given" do subject.element_node(@en1) assert_equal @en1, subject.cached end should "return the element node" do assert_equal @en1, subject.element_node(@en1) end should "write out any cached value when a new element is given" do subject.element_node(@en1) assert_empty @out subject.element_node(@en2) assert_equal "#{@io.line_indent}blah#{@io.newline}", @out end should "write out any cached value when flushed" do subject.flush assert_empty @out subject.element_node(@en1) subject.flush assert_equal "#{@io.line_indent}blah#{@io.newline}", @out end end class PartialStartTagWrittenTests < AddContentStartTagWrittenTests desc "using the partial meth" should "cache any partial markup given" do subject.partial("partial markup") assert_equal "partial markup", subject.cached end should "write out any cached partial values" do subject.partial("partial markup") assert_empty @out subject.element_node(@en2) assert_equal "partial markup", @out end should "write out any cached value when flushed" do subject.flush assert_empty @out subject.partial("partial markup") subject.flush assert_equal "partial markup", @out end end class AddContentStartTagNotWrittenTests < AddContentTests desc "(the start tag has not been written)" before do subject.instance_variable_set("@start_tag_written", false) end end class ElementStartTagNotWrittenTests < AddContentStartTagNotWrittenTests desc "using the element_node meth" should "cache any element_node given" do subject.element_node(@en1) assert_equal @en1, subject.cached end should "return the element node" do assert_equal @en1, subject.element_node(@en1) end should "write out the start tag with IO#newline when an element is given" do subject.element_node(@en1) assert_equal "#{@io.line_indent(-1)}
a
b