specs/impl/preprocess.rb in amrita2-2.0.1 vs specs/impl/preprocess.rb in amrita2-2.0.2
- old
+ new
@@ -1,40 +1,38 @@
-
-require 'rexml/element'
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/"]
@@ -57,43 +55,43 @@
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 == ""
@@ -108,32 +106,32 @@
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 == ""
@@ -174,11 +172,11 @@
$1.should == "bbbaaa"
$2.should == "<![CDATA["
$3.should == "bbb"
end
- specify "check regexp"do
+ specify "check regexp"do
tag = %r[\w*]
attr = %r[(?:\s*\w+=(?:".*?"|'.*?'))*] #"
src_start = %r[(?::| \[|\?\[|\{)]
r = %r[^\s*<<\s*(#{tag})(#{attr})(#{src_start}(.*?))?<\s*$]
"<<<".should match(r)
@@ -206,43 +204,43 @@
ret = p.process("aaa<![CDATA[bbb]]>ccc")
ret.should == "aaa<![CDATA[bbb]]>ccc"
ret = p.process("aaa<![CDATA[bbb]]>cccAAA<![CDATA[BBB]]>CCC")
ret.should == "aaa<![CDATA[bbb]]>cccAAA<![CDATA[BBB]]>CCC"
end
-
+
specify "replace <% %>" do
p = Amrita2::Core::PreProcessor.new
ret = p.process("<%aaa%>")
ret.should == "<![CDATA[<%aaa%>]]>"
ret = p.process("a<%b%>c<%d%>e")
ret.should == "a<![CDATA[<%b%>]]>c<![CDATA[<%d%>]]>e"
end
-
+
specify "replace <%= %>" do
p = Amrita2::Core::PreProcessor.new
ret = p.process("<%=aaa%>")
ret.should == "<![CDATA[<%=aaa%>]]>"
ret = p.process("a<%b%>c<%=d%>e")
ret.should == "a<![CDATA[<%b%>]]>c<![CDATA[<%=d%>]]>e"
end
-
+
specify "don't replace <%= %> in cdata" do
p = Amrita2::Core::PreProcessor.new
ret = p.process("<![CDATA[<%=aaa%>]]>")
ret.should == "<![CDATA[<%=aaa%>]]>"
ret = p.process("a<%=b%>c<![CDATA[<%d%>]]>e")
ret.should == "a<![CDATA[<%=b%>]]>c<![CDATA[<%d%>]]>e"
-
+
s = <<-END
<![CDATA[
<% a = 1 %>
1 + 2 = <%= a + 2 %>
]]>
END
p.process(s).should == s
end
-
+
specify "not replace <%= %> in cdata in real" do
text = <<-'END'
<ul>
<li am:foreach="data" am:if='$_.name =~ /amrita/'>
<![CDATA[
@@ -257,56 +255,56 @@
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<![CDATA[<%ccc%>]]>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<![CDATA[<%ccc%>]]>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 == "aaabbb<![CDATA[<%%<%=2%>ccc%>]]>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("<<a>>") #"
- ret.should == "<a />"
+ ret.should == "<a />"
ret = p.process("<<a:xxx>>")
ret.should == "<a am:src='xxx' />"
ret = p.process("<<p class='text':aaa>>")
ret.should == "<p am:src='aaa' class='text' />"
ret = p.process("<<a href='http://amrita2.rubyforge.org/':xxx>>")
@@ -315,25 +313,25 @@
ret.should == %[<a am:src='xxx' href="http://amrita2.rubyforge.org/" />]
p.process("<<h3:title>>").should == "<h3 am:src='title' />"
p.process("<<h3 class='title':title>>").should == "<h3 am:src='title' class='title' />"
end
-
+
specify "inline skipif" do
p = Amrita2::Core::PreProcessor.new
- ret = p.process("<<a ?[a == 1]>>")
+ ret = p.process("<<a ?[a == 1]>>")
ret.should == "<a am:skipif='not(a == 1)' />"
- ret = p.process("<<a ?![a == 1]>>")
+ ret = p.process("<<a ?![a == 1]>>")
ret.should == "<a am:skipif='a == 1' />"
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)
@@ -345,19 +343,19 @@
ret = p.process(%[<<a:xxx|Attr[:href=>:url, :body=>:description]>>])
ret.should == %[<a am:src='xxx|Attr[:href=>:url, :body=>:description]' />]
ret = p.process(%[<<a:|Attr[:href=>:url, :body=>:description]>>])
ret.should == %[<a am:filter='Attr[:href=>:url, :body=>:description]' />]
-
+
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(%[<<a:website\|Attr>>])
ret.should == %[<a am:src='website|Attr' />]
- end
-
+ end
+
specify "css selecter like" do
p = Amrita2::Core::PreProcessor.new
ret = p.process("<<aaa#bbb>><<ccc.ddd>>")
ret.should == '<aaa id="bbb" /><ccc class="ddd" />'
@@ -370,28 +368,34 @@
ret.should == '<a href="http://www.brain-tokyo.jp/index.html#a" id="bbb" class="ccc" />'
ret = p.process("<<div.cart-title>>")
ret.should == '<div class="cart-title" />'
end
+ specify "css selecter like" do
+ p = Amrita2::Core::PreProcessor.new
+
+ ret = p.process("<<p %= 1+2 >>")
+ ret.should == '<p><![CDATA[<%= 1+2 %>]]></p>'
+ 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("<html>test</html>")
<<html<
test
END
-
+
p = Amrita2::Core::PreProcessor.new
p.process(<<-END).should_be_samexml_as('<div class="cart-title">test</div>')
<<div.cart-title<
test
END
@@ -495,11 +499,11 @@
<<div#cart
{
:style => @cart.items.empty? ? "display: none" : nil
} <
%= render(:partial => "cart", :object => @cart)
-
+
END
<div am:v='HashDelegator.new($_) { {:style => @cart.items.empty? ? "display: none" : nil} }' id="cart">
<![CDATA[<%
_erbout.concat(eval(%{ render(:partial => "cart", :object => @cart)}).to_s)
%>]]>
@@ -510,11 +514,11 @@
specify "multi line" do
p = Amrita2::Core::PreProcessor.new
p.process(<<-END).should_be_samexml_as(<<-END)
<<table<
- <<tr:clients
+ <<tr:clients
|NVar[:surname, :firstname, :email]<
<td>$1, $2</td>
<td><a href="mailto:$3">$3</a></td>
END
<table>
@@ -556,20 +560,20 @@
</_>
</_>
END
a = p.process(<<-END)
- <<
- [1..5] <
+ <<
+ [1..5] <
<<?[ ($_%2) == 0
-
+
] <
<< {
:no => $_,
:square=> $_*$_
} <
-
+
<<li<
<<:no>> * <<:no>> = <<:square>>
END
a.should_be_samexml_as(expected)
a = p.process(<<-END)
@@ -583,50 +587,50 @@
end
end
context "amxml erb" do
include Amrita2
-
+
specify "simple" do
p = Amrita2::Core::PreProcessor.new
- p.process(<<-END).should == (<<-END).strip
+ p.process(<<-END).gsub("% ", '%').should == (<<-END).strip
<<div<
% a = 1
END
-<div ><![CDATA[<%
+<div ><![CDATA[<%
a = 1
%>]]>
</div>
END
- p.process(<<-END).should == (<<-END).strip
+ p.process(<<-END).gsub("% ", '%').gsub(" \n", "\n").should == (<<-END).strip
<<div<
%= a + 1
END
-<div ><![CDATA[<%
- _erbout.concat(eval(%{ a + 1}).to_s)
+<div ><![CDATA[<%
+ _erbout.concat(eval(%{ a + 1}).to_s)
%>]]>
</div>
END
p.process(<<-END).should_be_samexml_as(<<-END)
<<div<
% form_remote_tag :url => { :action => :add_to_cart, :id => $_ } do
- %= submit_tag "Add to Cart"
+ %= submit_tag "Add to Cart"
% end
END
<div>
<% form_remote_tag :url => { :action => :add_to_cart, :id => $_ } do
-_erbout.concat(eval(%{ submit_tag "Add to Cart" }).to_s)
+_erbout.concat(eval(%{ submit_tag "Add to Cart"}).to_s)
end %>
</div>
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