require 'rexml/document' require 'amrita2' require 'amrita2/testsupport' require 'time' include Amrita2 include Amrita2::Runtime include Amrita2::Filters context "Repeat Filter" do include Amrita2::Runtime include Amrita2::Filters specify "with string" do #t = Amrita2::Template.new('
') t = Amrita2::Template.new('
<<:aaa | Repeat[5] >>
') t.render_with(:aaa => "A").should_be_samexml_as '
AAAAA
' end end context "Default Filter" do include Amrita2 include Amrita2::Runtime include Amrita2::Filters specify "with not nil data" do begin #t = Amrita2::Template.new('
aaa
') t = Amrita2::Template.new('
<<:aaa | Default[123] >>
') t.test_with(:aaa => "xyz") do |result| result.should_be_samexml_as '
xyz
' end end end specify "with nil " do t = Amrita2::Template.new('aaa') t.test_with(:aaa => nil) do |result| result.should_be_samexml_as '123' end end specify "append filter in am:filter" do text = 'aaa' t = Amrita2::Template.new(text) t.test_with(:aaa => nil) do |result| result.should_be_samexml_as '789' end end specify "append filter in ruby code" do text = 'aaa' t = Amrita2::Template.new(text) do |e, src, filters| if src == "aaa" filters << Amrita2::Filters::Default[456] end end t.test_with(:aaa => nil) do |result| result.should_be_samexml_as '456' end end specify "with not nil data" do begin t = Amrita2::Template.new(%[
aaa
]) t.test_with(:aaa => Time.parse('2007-10-01')) do |result| result.should_be_samexml_as '
07-10-01
' end t.test_with(:aaa => nil) do |result| result.should_be_samexml_as '
' end end end end context "Format Filter" do include Amrita2::Runtime include Amrita2::Filters specify "Integer" do #t = Amrita2::Template.new(%q[aaa]) t = Amrita2::Template.new(%q[<<:aaa | Format['(%s)']>>]) t.test_with(:aaa => 123) do |result| result.should_be_samexml_as '(123)' end end specify "with Default" do t = Amrita2::Template.new(%q[aaa]) t.render_with(:aaa => nil).should_be_samexml_as '456' end end context "Other ways to setup filters" do include Amrita2::Runtime include Amrita2::Filters def test(t) t.test_with(:aaa => "A") do |result| result.should_be_samexml_as '
(A)(A)(A)(A)(A)
' end t.test_with({}) do |result| result.should_be_samexml_as '
a
' end end specify "am:src" do t = Amrita2::Template.new <<-END
END test(t) end specify "am:filter" do t = Amrita2::Template.new <<-END
END test(t) end specify "am:src and am:filter" do t = Amrita2::Template.new <<-END
END test(t) end specify "new + block" do text = <<-END
END t = Amrita2::Template.new(text) do |e, src, filters| if e.name == 'span' and src == "aaa" filters << Default['a'] << Format['(%s)'] << Repeat[5] end end test(t) end specify "inline filter proc" do t = Amrita2::Template.new <<-END <%(BeforeCompile) filter_setup do |e, src, filters| if e.name == 'span' and src == "aaa" filters << Default['a'] << Format['(%s)'] << Repeat[5] end end %>
END test(t) end end context "with am:v" do include Amrita2::Runtime include Amrita2::Filters specify "Integer" do text = %q[aaa] t = Amrita2::Template.new(text) aaa = 123 t.test_with(binding) do |result| result.should_be_samexml_as '(123)' end end end context "with Amrita2:Tuppl" do include Amrita2::Runtime include Amrita2::Filters specify "simple" do text = <<-END [$2] END t = Amrita2::Template.new(text) t.test_with(:aaa=>Amrita2::Util::Tuple['amrita', 'Amrita template library']) do |result| result.should_be_samexml_as '[Amrita template library]' end end specify "with a hash" do text = <<-END [$2] END data = { :name => 'amrita', :description => 'Amrita template library' } t = Amrita2::Template.new(text) t.test_with(:aaa=>data) do |result| result.should_be_samexml_as '[Amrita template library]' end end specify "with an array of hash" do text = <<-END END data = [ { :name => 'amrita', :description => 'Amrita template library' }, { :name => 'amrita2', :description => 'Amrita2 template library' } ] t = Amrita2::Template.new(text) t.test_with(:aaa=>data) do |result| result.should_be_samexml_as <<-END END end end specify "with an array of struct" do text = <<-END END s = Struct.new(:name, :description) data = [ s.new('amrita', 'Amrita template library'), s.new('amrita2', 'Amrita2 template library'), ] t = Amrita2::Template.new(text) t.test_with(:aaa=>data) do |result| result.should_be_samexml_as <<-END END end end end context "Method Filter" do include Amrita2::Runtime include Amrita2::Filters specify "with string" do t = Amrita2::Template.new('
') t.render_with(:aaa => "AbC").should_be_samexml_as '
abc
' end specify "with parameter" do t = Amrita2::Template.new(%[
]) t.render_with(:aaa => Time.parse("2007/08/31")).should_be_samexml_as '
07-08-31
' end specify "with parameter short" do t = Amrita2::Template.new(%[
]) t.render_with(:aaa => Time.parse("2007/08/31")).should_be_samexml_as '
07-08-31
' end specify "with parameter short" do t = Amrita2::Template.new <<-END <
> END task = [ { :memo => 'Default and filter' }, { :memo => nil }, { :memo => 'xyz' } ] t.render_with(binding).should_be_samexml_as '
DEFAULT AND FILTER
(no text)
XYZ
' end end context "CommandFilter" do include Amrita2::Runtime include Amrita2::Filters specify "cat -n" do t = Amrita2::Template.new(%[
]) t.test_with(:aaa => "abc") do|r| r.should_be_samexml_as "1
abc
" end end specify "tidy" do t = Amrita2::Template.new <<-END <
  • 1
  • 2
  • 3
  • END t.test_with(binding) do|r| r.should == <
    • 1
    • 2
    • 3
    END end end specify "tidy for only debug" do text = <<-END <%(BeforeCompile) self.class.module_eval { remove_const(:Tidy) } if self.class.const_defined?(:Tidy) if ENV['RAILS_ENV'] == "development" Tidy = CommandFilter['tidy -q -xml -indent'] else Tidy = nil end %> <
  • 1
  • 2
  • 3 END ENV['RAILS_ENV'] = "development" t = Amrita2::Template.new text t.test_with(binding) do|r| r.should == " \n
    \n
      \n
    • 1
    • \n
    • 2
    • \n
    • 3
    • \n
    \n
    \n" end ENV['RAILS_ENV'] = "production" t = Amrita2::Template.new text t.test_with(binding) do |r| r.should == " \n
    • 1
    • 2
    • 3
    \n
    " end end end context "Join Filter" do include Amrita2::Runtime include Amrita2::Filters specify "Join elements" do t = Amrita2::Template.new <<-END <
    aaa bbbb END t.test_with(binding) do|r| r.should_be_samexml_as '
    aaaa aaa bbbb
    ' end end specify "Join with
    " do t = Amrita2::Template.new <<-END <
    '] < aaaa bbbb END t.test_with(binding) do|r| r.should_be_samexml_as '
    aaaa
    bbbb
    ' end end specify "Join with nbsp" do t = Amrita2::Template.new <<-END <
    aaaa bbbb
    ' end end specify "Join erb with nbsp" do t = Amrita2::Template.new <<-END <
    aaaa 1 bbbb 2 cccc
    ' end end end context "Stream Filter" do include Amrita2::Runtime include Amrita2::Filters specify "Change Stream" do t = Amrita2::Template.new <<-END <> END t.test_with(binding) do|r| r.should_be_samexml_as 'bbbb aaaa' end end specify "Change Stream with erb" do t = Amrita2::Template.new <<-END <> END t.test_with(binding) do|r| r.should_be_samexml_as 'bbbb aaaa' end end specify "multi streams" do t = Amrita2::Template.new <<-END <> <<=efg>> END t.test_with(binding) do|r| r.should_be_samexml_as 'ddd aaa ccc bbb' end end end context "Module Extend Filter" do include Amrita2::Runtime include Amrita2::Filters module ModuleExtendFilterTest def bbb "(#{super})" end end specify "module extend" do t = Amrita2::Template.new <<-END <
    > END aaa = Struct.new(:bbb).new('bbb') t.test_with(binding) do |r| r.should_be_samexml_as '
    (bbb)
    ' end end end context "ToHash Filter" do include Amrita2::Runtime include Amrita2::Filters specify "with string" do t = Amrita2::Template.new <<-END <<:a|ToHash[:xxx]< <<:xxx>> END t.render_with(:a => "A").should_be_samexml_as 'A' end specify "many keys" do t = Amrita2::Template.new <<-END <<:time | ToHash[:h=>:hour, :m=>:min, :s=>:sec] < <<:h>>:<<:m>>:<<:s>> END tm = Time.local(2008,1,10,9,10,30) tm.extend(Amrita2::DictionaryData) t.render_with(:time=>tm).should_be_samexml_as '9:10:30' end end