require 'amrita2' require 'amrita2/testsupport' context "amxml parser" do include Amrita2 it "tag only" do amxml = Amrita2::Core::PreProcessor::AmXml a = amxml.parse(%[a]) a.tag.should == "a" a.attr.should == "" a.should_not be_has_amxml_attrs end specify "tag with namespace only" do amxml = Amrita2::Core::PreProcessor::AmXml a = amxml.parse(%[xx:a/]) a.tag.should == "xx:a" a.attr.should == "" a.should_not be_has_amxml_attrs a = amxml.parse(%[xx:a]) a.tag.should == "xx" a.attr.should == "" a.should be_has_amxml_attrs a.src_name.should == "a" a = amxml.parse(%[xx:a b="c"]) a.tag.should == "xx:a" a.attr.should == ' b="c"' a.should_not be_has_amxml_attrs end specify "tag and attrs" do amxml = Amrita2::Core::PreProcessor::AmXml a = amxml.parse(%[a href="http://amrita2.rubyforge.org/"]) a.tag.should == "a" a.attr.should == %[ href="http://amrita2.rubyforge.org/"] a.should_not be_has_amxml_attrs a = amxml.parse(%[a href='http://amrita2.rubyforge.org/']) a.tag.should == "a" a.attr.should == %[ href='http://amrita2.rubyforge.org/'] a.should_not be_has_amxml_attrs a = amxml.parse(%[a href='http://amrita2.rubyforge.org/' class="xxx"]) a.tag.should == "a" a.attr.should == %[ href='http://amrita2.rubyforge.org/' class="xxx"] a.should_not be_has_amxml_attrs a = amxml.parse(%[a href = 'http://amrita2.rubyforge.org/' class = "xxx"]) a.tag.should == "a" a.attr.should == %[ href = 'http://amrita2.rubyforge.org/' class = "xxx"] a.should_not be_has_amxml_attrs a = amxml.parse(%[x:a y:href='http://amrita2.rubyforge.org/' zz:class="xxx"]) a.tag.should == "x:a" a.attr.should == %[ y:href='http://amrita2.rubyforge.org/' zz:class="xxx"] a.should_not be_has_amxml_attrs end specify "src" do amxml = Amrita2::Core::PreProcessor::AmXml a = amxml.parse(%[:xxx]) a.tag.should == "_" a.attr.should == "" a.should be_has_amxml_attrs a.src_name.should == "xxx" a = amxml.parse(%[a:xxx]) a.tag.should == "a" a.attr.should == "" a.should be_has_amxml_attrs a.src_name.should == "xxx" end specify "src and filters" do amxml = Amrita2::Core::PreProcessor::AmXml a = amxml.parse(%[a:xxx|Attr]) a.tag.should == "a" a.attr.should == "" a.should be_has_amxml_attrs a.src_name.should == "xxx" a.filters.should == "Attr" a = amxml.parse(%[a:xxx| \n Attr]) a.tag.should == "a" a.attr.should == "" a.should be_has_amxml_attrs a.src_name.should == "xxx" a.filters.should == "Attr" end specify "loop" do amxml = Amrita2::Core::PreProcessor::AmXml a = amxml.parse(%[ [1..5] ]) a.tag.should == "_" a.attr.should == "" a.should be_has_amxml_attrs a.src_name.should == nil a.filters.should == nil a.for.should == %[1..5] a = amxml.parse(%[a [1..5] ]) a.tag.should == "a" a.attr.should == "" a.should be_has_amxml_attrs a.src_name.should == nil a.filters.should == nil a.for.should == %[1..5] end specify "cond" do amxml = Amrita2::Core::PreProcessor::AmXml '?![a == 1]'.should match(%r[#{ amxml::S_AMVARS}]) a = amxml.parse(%[a ?![a == 1] ]) a.tag.should == "a" a.attr.should == "" a.should be_has_amxml_attrs a.src_name.should == nil a.filters.should == nil a.skipif.should == "a == 1" a = amxml.parse(%[a ?[ b == 1 ] ]) a.tag.should == "a" a.attr.should == "" a.should be_has_amxml_attrs a.src_name.should == nil a.filters.should == nil a.skipif.should == nil a.skipunless.should == "b == 1" end specify "value" do amxml = Amrita2::Core::PreProcessor::AmXml a = amxml.parse(%[a { :xxx=>1 }]) a.tag.should == "a" a.attr.should == "" a.should be_has_amxml_attrs a.src_name.should == nil a.filters.should == nil a.value.should == ":xxx=>1" end specify "result" do amxml = Amrita2::Core::PreProcessor::AmXml amxml.parse(%[a { :xxx=>1 }]).result(true).should == %[] amxml.parse(%[a href="xxx" { :xxx=>1 }]).result(true).should == %[] amxml.parse(%[a ?[a == 1]]).result(true).should == %[] amxml.parse(%[a:xxx|Attr]).result(true).should == %[] amxml.parse(%[tr:clients|NVar[:surname, :firstname, :email]]).result(false).should == %[] amxml.parse(%[ [1..5] ]).result(false).should == %[<_ am:for='1..5' >] amxml.parse("tr:clients \n |NVar[:surname, :firstname, :email]").result(false).should == "" end end context "preprocessor" do include Amrita2 specify "check CDATA regexp" do t = "aaa 2}<".should match(r) "<<:title<".should match(r) "<<:title [1..5]<".should match(r) "<<:title ?[$_ == 1]<".should match(r) "<<:title { :val => 2}<".should match(r) "<< [1..5]<".should match(r) "<< ?[$_ == 1]<".should match(%r[(<< #{src_start}(.*?)?<$)]) "<< ?[$_ == 1]<".should match(r) "<<{ :val => 2}<".should match(r) " <ccc") ret.should == "aaaccc" ret = p.process("aaacccAAACCC") ret.should == "aaacccAAACCC" end specify "replace <% %>" do p = Amrita2::Core::PreProcessor.new ret = p.process("<%aaa%>") ret.should == "]]>" ret = p.process("a<%b%>c<%d%>e") ret.should == "a]]>c]]>e" end specify "replace <%= %>" do p = Amrita2::Core::PreProcessor.new ret = p.process("<%=aaa%>") ret.should == "]]>" ret = p.process("a<%b%>c<%=d%>e") ret.should == "a]]>c]]>e" end specify "don't replace <%= %> in cdata" do p = Amrita2::Core::PreProcessor.new ret = p.process("]]>") ret.should == "]]>" ret = p.process("a<%=b%>c]]>e") ret.should == "a]]>c]]>e" s = <<-END 1 + 2 = <%= a + 2 %> ]]> END p.process(s).should == s end specify "not replace <%= %> in cdata in real" do text = <<-'END' END ret = Amrita2::Core::PreProcessor.new.process(text) text.should == ret end specify "section" do pp = Amrita2::Core::PreProcessor.new ret = pp.process("aaa<%(Section1)xxx%>bbb") ret.should == "aaabbb" pp.sections[:Section1].should == "xxx" end specify "section and cdata" do pp = Amrita2::Core::PreProcessor.new ret = pp.process("aaa<%(Section1)xxx%>bbb<%ccc%>ddd") ret.should == "aaabbb]]>ddd" pp.sections[:Section1].should == "xxx" end specify "two sections and cdata" do pp = Amrita2::Core::PreProcessor.new ret = pp.process("aaa<%(Section1)xxx%>bbb<%ccc%>ddd<%(Section2)yyy%>eee") ret.should == "aaabbb]]>dddeee" pp.sections[:Section1].should == "xxx" pp.sections[:Section2].should == "yyy" end specify "two sections and cdata with nest" do pp = Amrita2::Core::PreProcessor.new ret = pp.process("aaa<%(Section1)<%=1%>xxx%>bbb<%%<%=2%>ccc%>ddd<%(Section2)<%=3%>yyy%>eee") ret.should == "aaabbbccc%>]]>dddeee" pp.sections[:Section1].should == "<%=1%>xxx" pp.sections[:Section2].should == "<%=3%>yyy" end specify "nbsp" do pp = Amrita2::Core::PreProcessor.new ret = pp.process("  &") ret.should == "  &" end end context "amxml inline" do include Amrita2 specify "inline" do p = Amrita2::Core::PreProcessor.new ret = p.process("<>") #" ret.should == "" ret = p.process("<>") ret.should == "" ret = p.process("<

>") ret.should == "

" ret = p.process("<>") ret.should == "" ret = p.process(%[<>]) ret.should == %[] p.process("<>").should == "

" p.process("<

>").should == "

" end specify "inline skipif" do p = Amrita2::Core::PreProcessor.new ret = p.process("<>") ret.should == "" ret = p.process("<>") ret.should == "" end specify "inline anonymous tag" do p = Amrita2::Core::PreProcessor.new ret = p.process("<<:xxx>>") ret.should == "<_ am:src='xxx' />" end specify "inline filter" do r = %r[#{Amrita2::Core::PreProcessor::AmXml::S_SRC_AND_FILTERS }] ":a|A|B".should match(r) ":a|A[aaa]|B[abb]".should match(r) p = Amrita2::Core::PreProcessor.new ret = p.process(%[<:url, :body=>:description]>>]) ret.should == %[] ret = p.process(%[<:url, :body=>:description]>>]) ret.should == %[] ret = p.process(%[<:url, :body=>:description]>>]) ret.should == %[] ret = p.process(%[<<:|Each[:class=>[:odd, :even]]|Attr[:class,:body=>:no]>>]) ret.should == %[<_ am:filter='Each[:class=>[:odd, :even]]|Attr[:class,:body=>:no]' />] ret = p.process(%[<<:|Each[:class=>[:odd, :even]]|\nAttr[:class,:body=>:no]>>]) ret.should == %[<_ am:filter='Each[:class=>[:odd, :even]]|\nAttr[:class,:body=>:no]' />] ret = p.process(%[<>]) ret.should == %[] end specify "css selecter like" do p = Amrita2::Core::PreProcessor.new ret = p.process("<><>") ret.should == '' ret = p.process("<>") ret.should == '' ret = p.process("<>") ret.should == '' ret = p.process("<>") ret.should == '' ret = p.process("<>") ret.should == '
' end specify "css selecter like" do p = Amrita2::Core::PreProcessor.new ret = p.process("<

>") ret.should == '

]]>

' end end context "amxml block" do include Amrita2 specify "simple" do p = Amrita2::Core::PreProcessor.new p.process(<<-END).should_be_samexml_as("<_ am:src='title' /> ") <<:title>> END p = Amrita2::Core::PreProcessor.new p.process(<<-END).should_be_samexml_as("test") <test
') <12345

END

12345

END p.process(<<-END).should_be_samexml_as(<<-END) <title1 <12345

END title1

12345

END p.process(<<-END).should_be_samexml_as("

") <
> END end specify "src" do p = Amrita2::Core::PreProcessor.new p.process(<<-END).should_be_samexml_as(<<-END) <$1, $2 $3 END
$1, $2 $3
END end specify "for" do p = Amrita2::Core::PreProcessor.new p.process(<<-END).should_be_samexml_as(<<-END) << [1..5] < abc END <_ am:for='1..5'>abc END end specify "for and cond" do p = Amrita2::Core::PreProcessor.new p.process(<<-END).should_be_samexml_as(<<-END) << [1..5] < << ?[ ($_%2) == 0] < abc END <_ am:for='1..5'>\n<_ am:skipif='not(($_%2) == 0)'>abc\n END end specify "for and cond and value" do p = Amrita2::Core::PreProcessor.new p.process(<<-END).should_be_samexml_as(<<-END) << [1..5] < << ?[ ($_%2) == 0] < << { :no => $_, :square=> $_*$_ }< <> * <<:no>> = <<:square>> END <_ am:for='1..5'> <_ am:skipif='not(($_%2) == 0)'> <_ am:v='HashDelegator.new($_) { {:no => $_, :square=> $_*$_} }'>
  • <_ am:src='no' />*<_ am:src='no' />=<_ am:src='square' />
  • END end specify "filter and value" do p = Amrita2::Core::PreProcessor.new p.process(<<-END).should_be_samexml_as(<<-END) < @cart.items.empty? ? "display: none" : nil } < %= render(:partial => "cart", :object => @cart) END
    "cart", :object => @cart)}).to_s) %>]]>
    END end specify "multi line" do p = Amrita2::Core::PreProcessor.new p.process(<<-END).should_be_samexml_as(<<-END) <$1, $2 $3 END
    $1, $2 $3
    END p.process(<<-END).should_be_samexml_as(<<-END) <:name, :author=>:author] |Each[:class=>["odd", "even"]] |Attr[:class] < END
    END end specify "inline ruby" do p = Amrita2::Core::PreProcessor.new expected = <<-END <_ am:for='1..5'> <_ am:skipif='not(($_%2) == 0)'> <_ am:v='HashDelegator.new($_) { {:no => $_,\n :square=> $_*$_} }'>
  • <_ am:src='no' />*<_ am:src='no' />=<_ am:src='square' />
  • END a = p.process(<<-END) << [1..5] < < $_, :square=> $_*$_ } < <> * <<:no>> = <<:square>> END a.should_be_samexml_as(expected) a = p.process(<<-END) << [1..5] < << ?[ ($_%2) == 0] < << { :no => $_, :square=> $_*$_ }< <> * <<:no>> = <<:square>> END a.gsub(/\s+/m, ' ').should_be_samexml_as(expected.gsub(/\s+/m, ' ')) end end context "amxml erb" do include Amrita2 specify "simple" do p = Amrita2::Core::PreProcessor.new p.process(<<-END).gsub("% ", '%').should == (<<-END).strip <]]>
    END p.process(<<-END).gsub("% ", '%').gsub(" \n", "\n").should == (<<-END).strip <]]> END p.process(<<-END).should_be_samexml_as(<<-END) < { :action => :add_to_cart, :id => $_ } do %= submit_tag "Add to Cart" % end END
    <% form_remote_tag :url => { :action => :add_to_cart, :id => $_ } do _erbout.concat(eval(%{ submit_tag "Add to Cart"}).to_s) end %>
    END end end context "cell processor" do include Amrita2 specify "not cell" do l = Amrita2::Core::PreProcessor::LineProcessor.new do |s| s end l.parse_line("||aaa").should == false l.cells.size.should == 0 end specify "1 cell" do l = Amrita2::Core::PreProcessor::LineProcessor.new do |s| s end l.parse_line("|||aaa|") l.cells.size.should == 1 c = l.cells.first c[:text].should == "aaa" c[:tag].should == :td l.get_result_xml.should == "aaa" l = Amrita2::Core::PreProcessor::LineProcessor.new do |s| s end l.parse_line("|||aaa") # omit the last | l.cells.size.should == 1 c = l.cells.first c[:text].should == "aaa" c[:tag].should == :td l.get_result_xml.should == "aaa" end specify "1 cell with spaces" do l = Amrita2::Core::PreProcessor::LineProcessor.new do |s| s end l.parse_line("| ||aaa|").should == true l.cells.size.should == 1 c = l.cells.first c[:text].should == "aaa" c[:tag].should == :td l.get_result_xml.should == "aaa" end specify "2cell" do l = Amrita2::Core::PreProcessor::LineProcessor.new do |s| s end l.parse_line("|||aaa|bbb") l.cells.size.should == 2 c = l.cells.first c[:text].should == "aaa" c[:tag].should == :td c = l.cells[1] c[:text].should == "bbb" c[:tag].should == :td l.get_result_xml.should == "aaabbb" end specify "th and 2cell" do l = Amrita2::Core::PreProcessor::LineProcessor.new do |s| s end l.parse_line("|||aaa||bbb|ccc|") l.cells.size.should == 3 c = l.cells.first c[:text].should == "aaa" c[:tag].should == :th c = l.cells[1] c[:text].should == "bbb" c[:tag].should == :td l.get_result_xml.should == "aaabbbccc" end specify "1cell and id" do l = Amrita2::Core::PreProcessor::LineProcessor.new do |s| s end l.parse_line("|||aaa|") l.parse_line("|id||xxx|") l.cells.size.should == 1 c = l.cells.first c[:text].should == "aaa" c[:tag].should == :td c["id"].should == "xxx" l.get_result_xml.should == "aaa" end specify "1 cell 2 lines" do l = Amrita2::Core::PreProcessor::LineProcessor.new do |s| s end l.parse_line("|||aaa|") l.parse_line("|||bbb|") l.cells.size.should == 1 c = l.cells.first c[:text].should == "aaa\nbbb" c[:tag].should == :td l.get_result_xml.should == "aaa\nbbb" end specify "proc" do l = Amrita2::Core::PreProcessor::LineProcessor.new do |s| "(#{s})" end l.parse_line("|||aaa|") l.cells.size.should == 1 c = l.cells.first c[:text].should == "aaa" c[:tag].should == :td l.get_result_xml.should == "(aaa)" end specify "real td cells" do l = Amrita2::Core::PreProcessor::LineProcessor.new do |s| s end l.parse_line(%[ |||$1, $2|$3|]) l.cells.size.should == 2 c = l.cells.first c[:text].should == "$1, $2" c[:tag].should == :td l.get_result_xml.should == "$1, $2$3" end specify "filter in cell" do l = Amrita2::Core::PreProcessor::LineProcessor.new do |s| s end l.parse_line(%q[ |||<:url]>>|]) l.cells.size.should == 1 c = l.cells.first c[:text].should == "<:url]>>" c[:tag].should == :td l.get_result_xml.should == "<:url]>>" end end context "cell line" do specify "1 cell" do p = Amrita2::Core::PreProcessor.new ret = p.process("|||aaa|") ret.should == "aaa" end specify "table" do p = Amrita2::Core::PreProcessor.new ret = p.process <<-END <$3| END ret.should_be_samexml_as <<-END
    $1, $2 $3
    END end specify "table with class" do p = Amrita2::Core::PreProcessor.new ret = p.process <<-END <$3| END ret.should_be_samexml_as <<-END
    $1, $2 $3
    END end specify "table with tr line" do p = Amrita2::Core::PreProcessor.new ret = p.process <<-END <$3| END ret.should_be_samexml_as <<-END
    $1, $2 $3
    END end end