require 'test/unit' require './tangerine' # Fake object. class TestHelper def [](x) x * 2 end def bar "BAR" end def quux "QUUUX" end def hard "Th'is ha;rd!" end def multiline "hack\nme!" end def numbers [1, 2, 3, 4, 5] end def wahr true end def falsch false end def xml %{text'alright'} end end # And time stood still... def Time.now Time.at 1095096544 end class TangerineTest < Test::Unit::TestCase # Test verbatim copying def test_verbatim template = Tangerine.new("foo") assert_equal template.expand(nil), "foo" end def test_simple_replacement template = Tangerine.new("foo ,,bar ,,quux quux") assert_equal template.expand(TestHelper.new), "foo BAR QUUUX quux" template = Tangerine.new("and;now'a,hard$stuff% : ,,hard") assert_equal template.expand(TestHelper.new), "and;now'a,hard$stuff% : Th'is ha;rd!" end def test_eval_replacement template = Tangerine.new( "foo ,,(bar.downcase) ,,(quux.reverse.tr('U','M')) quux") assert_equal template.expand(TestHelper.new), "foo bar XMMMQ quux" template = Tangerine.new("1+1 = ,,(1+1)") assert_equal template.expand(TestHelper.new), "1+1 = 2" end def test_multiline template = Tangerine.new < } ,,(numbers.reverse){<,,to_s> } last EOF assert_equal template.expand(TestHelper.new), < <2> <3> <4> <5> <5> <4> <3> <2> <1> last EOF template = Tangerine.new <") \ << Entry.new("another", "again") end end Entry = Struct.new(:title, :body) class Entry # Only entries with a 'd' in the title are considered recent. :P def recent? title =~ /d/ end end def test_blog_example template = Tangerine.new <,,title

,,tagline

,,(Time.now)

,,# overview
    ,,entries{ [
  • ,,(title.upcase)
  • ] }
,,entries{

,,title

,,recent?{[New]}

,,body


}

,,title -- ,,tagline. ,,(Time.now.strftime("%d%b%Y").downcase)

EOF assert_equal template.expand(Blog.new), <chris blogs

a blog by christian neukirchen

Mon Sep 13 19:29:04 CEST 2004

    [
  • HEAD
  • ] [
  • ANOTHER
  • ]

head

[New]

some


another

again


chris blogs -- a blog by christian neukirchen. 13sep2004

EOF end def test_indent template = Tangerine.new <text\'alright\' last EOF end class MyTaglib def resolve(tag) "#{tag}=,,#{tag}" end end def test_taglib template = Tangerine.new(",,quux ,,@quux ,,bar ,,@bar", MyTaglib.new) assert_equal template.expand(TestHelper.new), "QUUUX quux=QUUUX BAR bar=BAR" template = Tangerine.new(",,@quux{,,reverse} ,,@bar", MyTaglib.new) assert_equal template.expand(TestHelper.new), "XUUUQ=xuuq bar=BAR" template = Tangerine.new(" ,,|@quux{,,reverse} ,,@bar", MyTaglib.new) assert_equal template.expand(TestHelper.new), " XUUUQ=xuuq bar=BAR" template = Tangerine.new(" ,,|@multiline{,,to_s} ,,@bar", MyTaglib.new) assert_equal template.expand(TestHelper.new), " multiline=hack\n me! bar=BAR" assert_raises(RuntimeError, 'Tag not resolvable: foo') { template = Tangerine.new(",,@notaglib") } end def test_filelib filelib = Tangerine::FileLib.new("test-data", "test-data/subdir") assert_raises(RuntimeError, 'Tag not resolvable: foo') { template = Tangerine.new(",,@foo", filelib) } template = Tangerine.new(",,quux ,,@quux ,,bar ,,@bar", filelib) assert_equal template.expand(TestHelper.new), "QUUUX Quux! BAR Bar!" end def test_block_newline template = Tangerine.new(",,quux{,,reverse} ,,quux") assert_equal template.expand(TestHelper.new), "XUUUQ QUUUX" template = Tangerine.new(",,quux{,,reverse}\n,,quux") assert_equal template.expand(TestHelper.new), "XUUUQ\nQUUUX" template = Tangerine.new(",,quux{,,reverse} ,,quux") assert_equal template.expand(TestHelper.new), "XUUUQ QUUUX" template = Tangerine.new(",,quux{,,reverse}\n\n,,quux") assert_equal template.expand(TestHelper.new), "XUUUQ\n\nQUUUX" end def test_comma_escaping template = Tangerine.new("quux") assert_equal template.expand(TestHelper.new), "quux" template = Tangerine.new(",quux") assert_equal template.expand(TestHelper.new), ",quux" template = Tangerine.new(",,quux") assert_equal template.expand(TestHelper.new), "QUUUX" template = Tangerine.new(",,,quux") assert_equal template.expand(TestHelper.new), ",,quux" template = Tangerine.new(",,,,quux") assert_equal template.expand(TestHelper.new), ",,,quux" template = Tangerine.new(",,,,,quux") assert_equal template.expand(TestHelper.new), ",,,,quux" end def test_rfc822 template = Tangerine::RFC822.new <