require 'runit/testcase' require 'amrita/node_expand' class TestNodeExpand < RUNIT::TestCase include Amrita def test_string e = e(:x, :id=>"aaa") { "xyz" } assert_equal('e(:x) { "abcdefg" }', e.expand1("abcdefg").to_ruby) assert_equal('e(:x) { "abcdefg" }', eval(e.expand1("abcdefg").to_ruby).to_ruby) end def test_nil e = e(:x, :id=>"aaa") { "xyz" } assert_equal('Amrita::Null', e.expand1(nil).to_ruby) assert_equal('Amrita::Null', e.expand1(false).to_ruby) end def test_array e = e(:x, :id=>"aaa") { "xyz" } a = [ 1, 2, 3] assert_equal('[ e(:x) { "1" }, e(:x) { "2" }, e(:x) { "3" } ]', e.expand1(a).to_ruby) assert_equal('[ e(:x) { "1" }, e(:x) { "2" }, e(:x) { "3" } ]', Amrita::Node::to_node(eval(e.expand1(a).to_ruby)).to_ruby) context = ExpandContext.new context.delete_id = false context.delete_id_on_copy = false assert_equal(Node::to_node([ e(:x,a(:id, "aaa")) { "1" }, e(:x,a(:id, "aaa")) { "2" }, e(:x,a(:id, "aaa")) { "3" } ]), e.expand1(a, context)) end def test_hash e = e(:x, :id=>"aaa") { "xyz" } d = { :aaa =>"abcd" } assert_equal('e(:x) { "abcd" }', e.expand(d).to_ruby) context = ExpandContext.new context.delete_id = false assert_equal(e(:x, :id=>"aaa") { "abcd" }, e.expand(d, context)) end def test_hasharray e = e(:x, :id=>"aaa") { e(:y, :id=>"zz") { "aa" } } d = { :aaa => { :zz=>[1,2,3] } } assert_equal('e(:x) { [ e(:y) { "1" }, e(:y) { "2" }, e(:y) { "3" } ] }', e.expand(d).to_ruby) context = ExpandContext.new context.delete_id = false context.delete_id_on_copy = false assert_equal(e(:x,a(:id, "aaa")) { [ e(:y,a(:id, "zz")) { "1" }, e(:y,a(:id, "zz")) { "2" }, e(:y,a(:id, "zz")) { "3" } ] }, e.expand(d,context)) end def test_expandbymember e = e(:x, :id=>"aaa") { "xyz" } d = Struct.new(:Struct1, :aaa).new d.aaa = "struct data" d.extend ExpandByMember assert_equal('e(:x) { "struct data" }', e.expand(d).to_ruby) end def test_attrarray e = e(:x, :id=>"aaa", :aaa=>"bbb") { "xyz" } d = { :aaa=>a(:yyy, 123) } assert_equal(e(:x,a(:aaa, "bbb"),a(:yyy, "123")) { "xyz" }, e.expand(d)) assert_equal('e(:x,a(:aaa, "bbb"),a(:yyy, "123")) { "xyz" }', eval(e.expand(d).to_ruby).to_ruby) end def test_attrarray2 # This test is from bug report from Brandt Kurowski require "amrita/format" require "amrita/parser" template1 = HtmlParser::parse_text '
' template2 = HtmlParser::parse_text 'one
two
' data = { :foo => 'day', :bar => a(:lang => 'en') { 'night' }, } assert_equal('day
night
', format_inline(data) { template1 }) assert_equal('day
night
', format_inline(data) { template2 }) end def test_nodelete e = e(:x, :id=>"aaa") { "xyz" } d = { :aaa =>"abcd" } assert_equal('e(:x) { "abcd" }', e.expand(d).to_ruby) context = Amrita::ExpandContext.new context.delete_id = false assert_equal('e(:x,a(:id, "aaa")) { "abcd" }', e.expand(d, context).to_ruby) e = e(:x, :id=>"aaa") { "xyz" } a = [ 1, 2, 3] assert_equal('[ e(:x) { "1" }, e(:x) { "2" }, e(:x) { "3" } ]', e.expand1(a, context).to_ruby) context.delete_id_on_copy = false assert_equal('[ e(:x,a(:id, "aaa")) { "1" }, e(:x,a(:id, "aaa")) { "2" }, e(:x,a(:id, "aaa")) { "3" } ]', e.expand1(a, context).to_ruby) end def test_attr_expand1 e = e(:a, :href=>"@url") { e(:span, :id=>"url") } d = { :url=>"http://www.ruby-lang.org/" } assert_equal('e(:a,a(:href, "@url")) { e(:span) { "http://www.ruby-lang.org/" } }', e.expand(d).to_ruby) context = Amrita::ExpandContext.new context.expand_attr = true assert_equal('e(:a,a(:href, "http://www.ruby-lang.org/")) { e(:span) { "http://www.ruby-lang.org/" } }', e.expand(d, context).to_ruby) end def test_attr_expand2 e = e(:a, :id=>"url") url = "http://www.ruby-lang.org/" d = { :url=>a(:href,url) { url } } assert_equal('e(:a,a(:href, "http://www.ruby-lang.org/")) { "http://www.ruby-lang.org/" }', e.expand(d).to_ruby) end def test_attr_expand3 require 'amrita/parser' require 'amrita/format' template = HtmlParser.parse_text <<-END| x1 | y1 |
| x2 | y2 |
| x1 | y1 |
| x2 | y2 |