require 'test/unit' require 'kashmir' require 'elusion' # 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 tags; %w{ }; end def wahr; true; end def falsch; false; end def nix; nil; end def xml %{text'alright'} end end # And time stood still... def Time.now Time.at 1095096544 end class KashmirTest < Test::Unit::TestCase # Test verbatim copying def test_verbatim template = Kashmir.new("") assert_equal "", template.expand(nil) template = Kashmir.new("foo") assert_equal "foo", template.expand(nil) end def test_simple_replacement template = Kashmir.new("^bar ^quux") assert_equal "BAR QUUUX", template.expand(TestHelper.new) template = Kashmir.new("foo ^bar ^quux quux") assert_equal "foo BAR QUUUX quux", template.expand(TestHelper.new) template = Kashmir.new("and;now'a,hard$stuff% : ^hard") assert_equal "and;now'a,hard$stuff% : Th'is ha;rd!", template.expand(TestHelper.new) end def test_eval_replacement template = Kashmir.new( "foo ^(bar.downcase) ^(quux.reverse.tr('U','M')) quux") assert_equal "foo bar XMMMQ quux", template.expand(TestHelper.new) template = Kashmir.new("1+1 = ^(1+1)") assert_equal "1+1 = 2", template.expand(TestHelper.new) end def test_multiline template = Kashmir.new < } ^numbers.reverse.each{<^to_s> } last EOF assert_equal < <2> <3> <4> <5> <5> <4> <3> <2> <1> last EOF template = Kashmir.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 = Kashmir.new <^title

^tagline

^(Time.now)

^# overview
    ^entries.each{ [
  • ^title.upcase
  • ] }
^entries.each{

^title

^recent?.true?{[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

#{Time.now}

    [
  • HEAD
  • ] [
  • ANOTHER
  • ]

head

[New]

some


another

again


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

EOF end def test_errors template = Kashmir.new('^foo') assert_raises(NoMethodError, "During template expansion: undefined method `foo' for nil:NilClass") { template.expand(nil) } end def test_output s = "" template = Kashmir.new('^bar') template.expand(TestHelper.new, s) assert_equal "BAR", s a = [] template = Kashmir.new('^bar ^quux') template.expand(TestHelper.new, a) assert_equal ["BAR", " ", "QUUUX"], a end def test_multiple_send template = Kashmir.new("^quux.reverse.to_s") assert_equal template.expand(TestHelper.new), "XUUUQ" template = Kashmir.new("^(quux).reverse") assert_equal template.expand(TestHelper.new), "QUUUX.reverse" end def test_xml_escape template = Kashmir.for_XML <text\'alright\'
<foo><bar><baz> last EOF end def test_ignore template = Kashmir.for_XML < "bar", "quux" => "pling"}.each){ ^0 -> ^1 -> ^1.reverse } last EOF assert_equal < pling -> gnilp foo -> bar -> rab last EOF end class MyTaglib def [](tag) "#{tag}=^#{tag}" end end def test_taglib template = Kashmir.new("^quux ^(call quux) ^bar ^(call bar)", MyTaglib.new) assert_equal "QUUUX quux=QUUUX BAR bar=BAR", template.expand(TestHelper.new) template = Kashmir.new(" ^(call multiline) ^(call bar)", MyTaglib.new) assert_equal " multiline=hack\nme! bar=BAR", template.expand(TestHelper.new) assert_raises(RuntimeError, 'no taglib defined') { template = Kashmir.new("^(call notaglib)") } end def test_blog_with_elusion template = Kashmir.new <^title

^tagline

^(Time.now)

^# overview
    ^entries.each{ [
  • ^title.upcase
  • ] }
^entries.each{

^title

^recent?.true?{[New]}

^body


}

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

EOF elusion = Elusion.new { |e| e.title "chris blogs" e.tagline "a blog by christian neukirchen" e.entries { |f| f.title "head" f.body "some " f.recent? true } e.entries { |f| f.title "another" f.body "again" f.recent? false } } assert_equal template.expand(elusion), <chris blogs

a blog by christian neukirchen

#{Time.now}

    [
  • HEAD
  • ] [
  • ANOTHER
  • ]

head

[New]

some


another

again


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

EOF end def test_blog_with_elusion_dwim template = Kashmir.new <^title

^tagline

^(Time.now)

^# overview
    ^entries{ [
  • ^title.upcase
  • ] }
^entries{

^title

^recent?{[New]}

^body


}

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

EOF expansion = template.expand { |e| e.title "chris blogs" e.tagline "a blog by christian neukirchen" e.entries { |f| f.title "head" f.body "some " f.recent? true } e.entries { |f| f.title "another" f.body "again" f.recent? false } } assert_equal expansion, <chris blogs

a blog by christian neukirchen

#{Time.now}

    [
  • HEAD
  • ] [
  • ANOTHER
  • ]

head

[New]

some


another

again


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

EOF end end