#! /usr/bin/env ruby require 'test/unit' $VERBOSE = 2 STDOUT.sync = true $:.unshift 'lib' $:.unshift '../lib' $:.unshift '.' require 'tagz' class TagzTest < Test::Unit::TestCase include Tagz class ::String Equal = instance_method '==' remove_method '==' def == other Equal.bind(self.delete(' ')).call other.to_s.delete(' ') end end def test_000 expected = '' actual = tagz{ foo_ _foo } assert_equal expected, actual end def test_010 expected = '' actual = tagz{ foo_ bar_ _bar _foo } assert_equal expected, actual end def test_020 expected = '' actual = tagz{ foo_ bar_{} _foo } assert_equal expected, actual end def test_030 expected = '' actual = tagz{ foo_{ bar_{} } } assert_equal expected, actual end def test_040 expected = 'bar' actual = tagz{ foo_{ 'bar' } } assert_equal expected, actual end def test_050 expected = 'foobar' actual = tagz{ foo_{ bar_{ 'foobar' } } } assert_equal expected, actual end def test_060 expected = 'foobar' actual = tagz{ foo_('key' => 'value'){ bar_(:a => :b){ 'foobar' } } } assert_equal expected, actual end def test_070 expected = '' actual = tagz{ foo_{} + bar_{} } assert_equal expected, actual end =begin def test_080 assert_raises(Tagz::NotOpen) do foo_{ _bar } end end def test_090 assert_raises(Tagz::NotOpen) do _foo end end def test_100 assert_nothing_raised do foo_ _foo end end =end def test_110 expected = 'foobar' actual = tagz{ foo_{ bar_{ 'foobar' } 'this content is ignored because the block added content' } } assert_equal expected, actual end def test_120 expected = 'foobarbarfoo' actual = tagz{ foo_{ bar_{ 'foobar' } baz_{ 'barfoo' } } } assert_equal expected, actual end def test_121 expected = 'foobarbarfoo' actual = tagz{ foo_{ bar_{ 'foobar' } baz_{ 'barfoo' } } } assert_equal expected, actual end def test_130 expected = 'afoobarbbarfoo' actual = tagz{ foo_{ |t| t << 'a' bar_{ 'foobar' } t << 'b' baz_{ 'barfoo' } } } assert_equal expected, actual end def test_140 expected = 'baz' actual = tagz{ foo_{ bar_ << 'baz' _bar } } assert_equal expected, actual end def test_150 expected = 'barbaz' actual = tagz{ foo_{ bar_ << 'bar' tag = baz_ tag << 'baz' _baz _bar } } assert_equal expected, actual end def test_160 expected = 'ab' actual = tagz{ foo_{ |foo| foo << 'a' bar_{ |bar| bar << 'b' } } } assert_equal expected, actual end def test_170 expected = '' @list = %w( a b c ) actual = tagz{ html_{ body_{ ul_{ @list.each{|elem| li_{ elem } } } } } } assert_equal expected, actual end def test_180 expected = '42' actual = tagz{ html_{ b = body_ b << 42 _body } } assert_equal expected, actual end def test_190 expected = '42' actual = tagz{ html_{ body_ tagz << 42 ### tagz is always the current tag! _body } } assert_equal expected, actual end def test_200 expected = '42' actual = tagz{ html_{ body_{ tagz << 42 ### tagz is always the current tag! } } } assert_equal expected, actual end def test_210 expected = '42' actual = tagz{ html_{ body_{ |body| body << 42 } } } assert_equal expected, actual end =begin def test_220 expected = '42' actual = tagz{ 'html'.tag do 'body'.tag do 42 end end } assert_equal expected, actual end =end def test_230 expected = '
content
' actual = tagz{ html_{ body_{ div_(:k => :v){ "content" } } } } assert_equal expected, actual end def test_240 expected = '
content
' actual = tagz{ html_{ body_{ div_ "content", :k => :v } } } assert_equal expected, actual end def test_241 expected = '
content
' actual = tagz{ html_{ body_{ div_ "content", :k => :v _div } } } assert_equal expected, actual end def test_250 expected = '
content and more content
' actual = tagz{ html_{ body_{ div_("content", :k => :v){ ' and more content' } } } } assert_equal expected, actual end def test_260 expected = '
content
' actual = tagz{ html_{ body_{ div_ :k => :v tagz << "content" _div } } } assert_equal expected, actual end def test_270 expected = '
content
' actual = tagz{ html_{ body_{ div_ :k => :v tagz << "content" _div } } } assert_equal expected, actual end def test_280 expected = 'content' actual = tagz{ tagz << "content" } assert_equal expected, actual end def test_290 expected = 'foobar' actual = tagz{ tagz { tagz << 'foo' << 'bar' } } assert_equal expected, actual end =begin def test_300 expected = 'foobar' actual = tagz{ tagz{ tagz 'foo', 'bar' } } assert_equal expected, actual end =end def test_310 expected = '
foobar
' actual = tagz{ html_{ body_{ div_! "foo", "bar", :k => :v } } } assert_equal expected, actual end def test_320 expected = 'a|b|c' links = %w( a b c ) actual = tagz{ html_{ body_{ tagz.write links.map{|link| e(:a, :href => link){ link }}.join(e(:span){ '|' }) } } } assert_equal expected, actual end def test_330 expected = '' actual = tagz{ tagz { a_ b_ c_ } } assert_equal expected, actual end def test_340 expected = '' actual = tagz{ a_ { b_ c_ } } assert_equal expected, actual end def test_350 expected = 'content' actual = tagz{ a_ { b_ c_ "content" } } assert_equal expected, actual end def test_360 expected = 'contentmore content' actual = tagz{ a_ { b_ "content" c_ d_ "more content" } } assert_equal expected, actual end =begin def test_370 expected = 'ab' actual = tagz{ re = 'a' re << tagz{'b'} re } assert_equal expected, actual end =end def test_380 expected = 'ab' actual = tagz{ tagz{ 'a' } + tagz{ 'b' } } assert_equal expected, actual end def test_390 expected = '
foo&bar>
' actual = tagz{ div_(:class => 'bar&foo>'){ 'foo&bar>' } } assert_equal expected, actual expected = %|
#{ expected }
| actual = tagz{ div_(:class => 'bar&foo>'){ actual } } assert_equal expected, actual end def test_400 expected = '
foo&bar
' actual = tagz{ div_{ span_{ 'foo&bar' } } } assert_equal expected, actual end def test_410 expected = '
false
' actual = tagz{ div_{ false } } assert_equal expected, actual end def test_420 expected = "
\nfoobar\nfoobar\n
" actual = tagz{ div_{ __; span_{ :foobar }; ___('foobar'); } } assert_equal expected, actual end def test_430 c = Class.new{ include Tagz.globally def foobar() div_{ 'foobar' } end }.new actual=nil assert_nothing_raised{ actual=c.foobar } expected = '
foobar
' assert_equal expected, actual =begin e = nil assert_raises(NoMethodError){ begin; c.missing; ensure; e=$!; end } assert e messages = e.backtrace.map{|line| line.split(%r/:/, 3).last} assert messages.all?{|message| message !~ /tagz/} =end end def test_440 c = Class.new{ include Tagz def foobar() tagz{ div_{ 'foobar' } } end def barfoo() div_{ 'barfoo' } end }.new actual=nil assert_nothing_raised{ actual=c.foobar } expected = '
foobar
' assert_equal expected, actual assert_raises(NoMethodError){ c.barfoo } end def test_450 c = Class.new{ include Tagz.globally def a() tagz{ a_{ b(tagz); nil } } end def b(doc=nil) tagz(doc){ b_{ 'content' } } end }.new actual=nil assert_nothing_raised{ actual=c.a } expected = 'content' assert_equal expected, actual assert_nothing_raised{ c.b } end def test_460 c = Class.new{ include Tagz.globally def a div_( 'a>b' => 'a>b' ){ 'content' } end }.new actual = nil assert_nothing_raised{ actual=c.a} expected = %(
content
) assert_equal expected, actual original = Tagz.escape_attribute! false assert original actual = nil assert_nothing_raised{ actual=c.a} expected = %(
b="a>b">content
) assert_equal expected, actual Tagz.escape_attribute! original actual = nil assert_nothing_raised{ actual=c.a} expected = %(
content
) assert_equal expected, actual upcased = Tagz.escape_attribute! lambda{|value| original.call(value).upcase} assert upcased actual = nil assert_nothing_raised{ actual=c.a} expected = %(
content
) assert_equal expected, actual Tagz.escape_attributes! lambda{|value| upcased.call(value).downcase} actual = nil assert_nothing_raised{ actual=c.a} expected = %(
content
) assert_equal expected, actual ensure Tagz.escape_attributes!(original) end def test_470 c = Class.new{ include Tagz.globally def a div_( ){ 'a>b' } end }.new actual = nil assert_nothing_raised{ actual=c.a} expected = %(
a>b
) assert_equal expected, actual original = Tagz.escape_content!(true) assert original actual = nil assert_nothing_raised{ actual=c.a} expected = %(
a>b
) assert_equal expected, actual upcased = Tagz.escape_content!(lambda{|value| original.call(value).upcase}) assert upcased actual = nil assert_nothing_raised{ actual=c.a} expected = %(
A>B
) assert_equal expected, actual Tagz.escape_content!(original) actual = nil assert_nothing_raised{ actual=c.a} expected = %(
a>b
) assert_equal expected, actual ensure Tagz.escape_content!(original) end def test_480 c = Class.new{ include Tagz.globally def a div_( 'a>b' => '<>'){ 'a>b' } end }.new Tagz.i_know_what_the_hell_i_am_doing! actual = nil assert_nothing_raised{ actual=c.a} expected = %(
b="<>">a>b
) assert_equal expected, actual ensure Tagz.i_do_not_know_what_the_hell_i_am_doing! end def test_490 c = Class.new{ include Tagz.globally def a div_{ __ tagz.concat 'a>b' __ tagz.write 'c>d' __ tagz << 'e>f' __ tagz.push 'g>h' } end }.new actual = nil assert_nothing_raised{ actual=c.a} expected = "
\na>b\nc>d\ne>f\ng>h
" assert_equal expected, actual end def test_500 expected = actual = nil Module.new do before = constants include Tagz after = constants expected = %w[ TagzConstants ] actual = after - before end assert_equal expected, actual end def test_510 expected = actual = nil Module.new do before = constants include Tagz.globally after = constants expected = %w[ TagzConstants ] actual = after - before end assert_equal expected, actual end def test_520 expected = actual = nil Module.new do before = constants include Tagz.privately after = constants expected = %w[ TagzConstants ] actual = after - before end assert_equal expected, actual end end