require 'rexml/document'
require 'amrita2'
require 'amrita2/testsupport'
include Amrita2
include Amrita2::Filters
include Amrita2::Runtime
context "Inline Ruby" do
include Amrita2::Runtime
specify "set context value" do
text = <<-END
<%
$_ = { :aaa => "abc" + a }
%>
abc
END
t = Amrita2::Template.new(text, :inline_ruby)
a = "xyz"
t.test_with(binding) do |result|
result.should_be_samexml_as("abcxyz
")
end
end
specify "with cdata" do
text = <<-'END'
<%
$_ = { :aaa => SanitizedString["#{a}"] }
%>
abc
END
t = Amrita2::Template.new(text, :inline_ruby)
a = "xyz"
t.test_with(binding) do |result|
result.should_be_samexml_as("xyz
")
end
end
specify "with an array of struct" do
text = <<-'END'
-
<%
$_ = {
:url => "http://raa.ruby-lang.org/list.rhtml?name=#{$_.name}",
:description => ($_.description or $_.name)
}
%>
END
s = Struct.new(:name, :description)
data = [
s.new('amrita', 'Amrita template library'),
s.new('amrita2'),
s.new('Ruby on Rails', 'this will be passed'),
]
t = Amrita2::Template.new(text)
t.inline_ruby = true
t.test_with(binding) do |result|
result.should_be_samexml_as <<-END
END
end
end
specify "with erb" do
text = <<-'END'
END
s = Struct.new(:name, :description)
data = [
s.new('amrita', 'Amrita template library'),
s.new('amrita2'),
s.new('Ruby on Rails', 'this will be passed'),
]
t = Amrita2::Template.new(text)
t.inline_ruby = true
t.test_with(binding) do |result|
result.should_be_samexml_as <<-END
END
end
end
end
context "Logic IF" do
include Amrita2::Runtime
setup do
@t = Amrita2::Template.new('abc', :inline_ruby)
end
specify "if true" do
a = 1
@t.test_with(binding) do |result|
result.should_be_samexml_as("")
end
end
specify "false" do
a = 0
@t.test_with(binding) do |result|
result.should_be_samexml_as("abc")
end
end
specify "with loop" do
text = <<-END
END
t = Amrita2::Template.new(text, :inline_ruby)
t.test_with(binding) do |result|
result.should_be_samexml_as("")
end
end
end
context "Logic IF amxml" do
include Amrita2::Runtime
specify "if" do
t = Amrita2::Template.new('<
>')
a = 1
t.test_with(binding) do |result|
result.should_be_samexml_as("
")
end
a = 0
t.test_with(binding) do |result|
result.should_be_samexml_as("")
end
end
specify "unless" do
t = Amrita2::Template.new('<
>')
a = 0
t.test_with(binding) do |result|
result.should_be_samexml_as("
")
end
a = 1
t.test_with(binding) do |result|
result.should_be_samexml_as("")
end
end
specify "if" do
t = Amrita2::Template.new('<>')
xxx = "123"
a = 1
t.test_with(binding) do |result|
result.should_be_samexml_as("123")
end
a = 0
t.test_with(binding) do |result|
result.should_be_samexml_as("")
end
end
specify "with loop" do
text = <<-END
END
t = Amrita2::Template.new(text, :inline_ruby)
t.test_with(:list=>1..5) do |result|
result.should_be_samexml_as("")
end
text = <<-END
END
t = Amrita2::Template.new(text, :inline_ruby)
t.test_with(:list=>1..5) do |result|
result.should_be_samexml_as("")
end
end
end
context "Logic for" do
include Amrita2::Runtime
setup do
@t = Amrita2::Template.new('
', :inline_ruby)
end
specify "0" do
a = []
@t.test_with(binding) do |result|
result.should_be_samexml_as("")
end
end
specify "1" do
a = [1]
@t.test_with(binding) do |result|
result.should_be_samexml_as("1
")
end
end
specify "3" do
a = [1, 2, 3]
@t.test_with(binding) do |result|
result.should_be_samexml_as("123
")
end
end
end
context "Logic value" do
include Amrita2::Runtime
specify "1" do
t = Amrita2::Template.new('
', :inline_ruby)
a = 1
t.test_with(binding) do |result|
result.should_be_samexml_as("2
")
end
end
specify "method call" do
t = Amrita2::Template.new('
', :inline_ruby)
a = 123
t.test_with(binding) do |result|
result.should_be_samexml_as("246
")
end
end
def aaa(x)
x*2
end
specify "filter" do
t = Amrita2::Template.new('
')
t.inline_ruby = true
a = 1
t.test_with(binding) do |result|
result.should_be_samexml_as("2
")
end
a = "abc"
t.test_with(binding) do |result|
result.should_be_samexml_as("abcabc
")
end
end
specify "quotation" do
t = Amrita2::Template.new(%[
], :inline_ruby)
a = 2
t.test_with(binding) do |result|
result.should_be_samexml_as("abcabc
")
end
t = Amrita2::Template.new(%[
], :inline_ruby)
a = 2
t.test_with(binding) do |result|
result.should_be_samexml_as("abcabc
")
end
end
specify '#{ ...} in am:v' do
t = Amrita2::Template.new(%q[
], :inline_ruby)
a = 2
t.test_with(binding) do |result|
result.should_be_samexml_as("4
")
end
end
specify 'loop with if' do
t = Amrita2::Template.new <<-END
<
END
t.test_with(binding) do |result|
result.should_be_samexml_as <<-END
END
end
end
specify 'all' do
t = Amrita2::Template.new <<-END
< $_, :no2 => $_ ,:square=> $_*$_ }<
<- > * <<:no2>> = <<:square>>
END
t.test_with(binding) do |result|
result.should_be_samexml_as <<-END
END
end
end
specify 'all various format' do
t = Amrita2::Template.new <<-END
<
$_,
:no2 => $_,
:square=> $_*$_
} <
<- > * <<:no2>> = <<:square>>
END
t.test_with(binding) do |result|
result.should_be_samexml_as <<-END
- 2 * 2 = 4
- 4 * 4 = 16
- 6 * 6 = 36
END
end
end
end
context "quotes in erb" do
include Amrita2::Runtime
specify "double quote" do
text = %q[<%= "#{1+2}" %>]
t = Amrita2::Template.new(text)
t.test_with(binding) do |result|
result.should_be_samexml_as("3")
end
end
specify "making quote in erb" do
text = %q[<%%= "#{1+<%= 1+2 %>}" %>]
t = Amrita2::Template.new(text)
t.test_with(binding) do |result|
result.should_be_samexml_as(%q[ <%= "#{1+3}" %> ])
t = Amrita2::Template.new(result)
t.test_with(binding) do |result|
result.should_be_samexml_as("4")
end
end
end
end
context "eval with " do
include Amrita2::Runtime
include Amrita2::Filters
specify "simple" do
text = <<-END
<<:people {
:name => "%s %s" % [$_.first, $_.last]
}<
Hello, <<:name>>
END
t = Amrita2::Template.new(text)
People = Struct.new(:first, :last)
data = {
:people => [
People.new('Yukihiro', 'Matsumoto'),
People.new('Taku', 'Nakajima'),
]
}
t.test_with(data, binding) do |result|
result.should_be_samexml_as <<-END
Hello, Yukihiro Matsumoto
Hello, Taku Nakajima
END
end
end
specify "simple with Hash" do
text = <<-END
<<:people {
:name => "%s %s" % [$_[:first], $_[:last]]
}<
Hello, <<:name>>
END
t = Amrita2::Template.new(text)
data = {
:people => {
:first => 'Taku',
:last => 'Nakajima',
}
}
t.test_with(data, binding) do |result|
result.should_be_samexml_as <<-END
Hello, Taku Nakajima
END
end
end
end