amrita2 1.9.6 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. data/README +112 -0
  2. data/init.rb +6 -0
  3. data/lib/amrita2/gettext.rb +116 -0
  4. data/lib/amrita2/macro.rb +153 -0
  5. data/lib/amrita2/rails_bridge.rb +172 -26
  6. data/lib/amrita2/template.rb +2634 -234
  7. data/lib/amrita2/testsupport.rb +171 -0
  8. data/lib/amrita2/version.rb +3 -3
  9. data/lib/amrita2.rb +1 -0
  10. data/sample/depot/app/controllers/admin_controller.rb +59 -0
  11. data/sample/depot/app/controllers/application.rb +20 -0
  12. data/sample/depot/app/controllers/info_controller.rb +19 -0
  13. data/sample/depot/app/controllers/login_controller.rb +85 -0
  14. data/sample/depot/app/controllers/store_controller.rb +68 -0
  15. data/sample/depot/app/helpers/admin_helper.rb +7 -0
  16. data/sample/depot/app/helpers/application_helper.rb +10 -0
  17. data/sample/depot/app/helpers/ar_form.rb +169 -0
  18. data/sample/depot/app/helpers/form_tag.rb +24 -0
  19. data/sample/depot/app/helpers/info_helper.rb +7 -0
  20. data/sample/depot/app/helpers/standard_form.rb +73 -0
  21. data/sample/depot/app/helpers/store_helper.rb +14 -0
  22. data/sample/depot/app/models/cart.rb +36 -0
  23. data/sample/depot/app/models/cart_item.rb +26 -0
  24. data/sample/depot/app/models/line_item.rb +34 -0
  25. data/sample/depot/app/models/order.rb +57 -0
  26. data/sample/depot/app/models/product.rb +41 -0
  27. data/sample/depot/app/models/user.rb +83 -0
  28. data/sample/depot/config/boot.rb +49 -0
  29. data/sample/depot/config/environment.rb +83 -0
  30. data/sample/depot/config/environments/development.rb +24 -0
  31. data/sample/depot/config/environments/production.rb +24 -0
  32. data/sample/depot/config/environments/test.rb +24 -0
  33. data/sample/depot/config/routes.rb +10 -0
  34. data/sample/depot/db/migrate/001_create_products.rb +18 -0
  35. data/sample/depot/db/migrate/002_add_price.rb +14 -0
  36. data/sample/depot/db/migrate/003_add_test_data.rb +68 -0
  37. data/sample/depot/db/migrate/004_add_sessions.rb +20 -0
  38. data/sample/depot/db/migrate/005_create_orders.rb +21 -0
  39. data/sample/depot/db/migrate/006_create_line_items.rb +27 -0
  40. data/sample/depot/db/migrate/007_create_users.rb +18 -0
  41. data/sample/depot/db/schema.rb +45 -0
  42. data/sample/depot/public/dispatch.rb +15 -0
  43. data/sample/depot/test/functional/admin_controller_test.rb +54 -0
  44. data/sample/depot/test/functional/info_controller_test.rb +23 -0
  45. data/sample/depot/test/functional/login_controller_test.rb +74 -0
  46. data/sample/depot/test/functional/store_controller_test.rb +57 -0
  47. data/sample/depot/test/integration/dsl_user_stories_test.rb +126 -0
  48. data/sample/depot/test/integration/user_stories_test.rb +70 -0
  49. data/sample/depot/test/performance/order_speed_test.rb +58 -0
  50. data/sample/depot/test/test_helper.rb +16 -0
  51. data/sample/depot/test/unit/cart_test.rb +39 -0
  52. data/sample/depot/test/unit/cart_test1.rb +31 -0
  53. data/sample/depot/test/unit/line_item_test.rb +15 -0
  54. data/sample/depot/test/unit/order_test.rb +15 -0
  55. data/sample/depot/test/unit/product_test.rb +98 -0
  56. data/sample/depot/vendor/plugins/amrita2/init.rb +6 -0
  57. data/sample/hello/hello.rb +22 -0
  58. data/sample/login_engine/app/controllers/application.rb +16 -0
  59. data/sample/login_engine/app/controllers/user_controller.rb +265 -0
  60. data/sample/login_engine/app/helpers/application_helper.rb +3 -0
  61. data/sample/login_engine/app/helpers/form_tag.rb +16 -0
  62. data/sample/login_engine/app/helpers/two_columns.rb +24 -0
  63. data/sample/login_engine/app/helpers/two_columns_form.rb +47 -0
  64. data/sample/login_engine/app/helpers/user_helper.rb +88 -0
  65. data/sample/login_engine/app/models/user.rb +7 -0
  66. data/sample/login_engine/app/models/user_notify.rb +75 -0
  67. data/sample/login_engine/config/boot.rb +45 -0
  68. data/sample/login_engine/config/environment.rb +140 -0
  69. data/sample/login_engine/config/environments/development.rb +21 -0
  70. data/sample/login_engine/config/environments/production.rb +18 -0
  71. data/sample/login_engine/config/environments/test.rb +19 -0
  72. data/sample/login_engine/config/routes.rb +23 -0
  73. data/sample/login_engine/db/migrate/001_create_users.rb +25 -0
  74. data/sample/login_engine/db/schema.rb +25 -0
  75. data/sample/login_engine/lib/config.rb +113 -0
  76. data/sample/login_engine/lib/hpricot_test_extension.rb +80 -0
  77. data/sample/login_engine/lib/login_engine/authenticated_system.rb +113 -0
  78. data/sample/login_engine/lib/login_engine/authenticated_user.rb +155 -0
  79. data/sample/login_engine/lib/login_engine.rb +62 -0
  80. data/sample/login_engine/public/dispatch.rb +10 -0
  81. data/sample/login_engine/test/functional/amrita2_test.rb +267 -0
  82. data/sample/login_engine/test/functional/user_controller_test.rb +544 -0
  83. data/sample/login_engine/test/mocks/mail.rb +14 -0
  84. data/sample/login_engine/test/mocks/time.rb +19 -0
  85. data/sample/login_engine/test/test_helper.rb +31 -0
  86. data/sample/login_engine/test/unit/user_test.rb +116 -0
  87. data/sample/login_engine/vendor/plugins/amrita2/init.rb +6 -0
  88. data/specs/attribute.rb +201 -0
  89. data/specs/datatypes.rb +231 -0
  90. data/specs/dictionary.rb +68 -0
  91. data/specs/erb_cdata.rb +187 -0
  92. data/specs/filters.rb +513 -0
  93. data/specs/gettext/erb_gettext.rb +42 -0
  94. data/specs/gettext/gettext_util.rb +65 -0
  95. data/specs/gettext/static_text.rb +103 -0
  96. data/specs/impl/code_generator.rb +87 -0
  97. data/specs/impl/dynamic_element.rb +92 -0
  98. data/specs/impl/hash_delegator.rb +57 -0
  99. data/specs/impl/parse_opt.rb +34 -0
  100. data/specs/impl/preprocess.rb +823 -0
  101. data/specs/impl/testsupport.rb +89 -0
  102. data/specs/inlineruby.rb +429 -0
  103. data/specs/intro.rb +654 -0
  104. data/specs/loop.rb +203 -0
  105. data/specs/macro.rb +532 -0
  106. data/specs/sample.rb +34 -0
  107. data/specs/sanitize.rb +110 -0
  108. data/specs/template.rb +189 -0
  109. data/specs/trace.rb +97 -0
  110. metadata +138 -19
  111. data/lib/amrita2/core.rb +0 -1897
  112. data/lib/amrita2/rd.rb +0 -314
@@ -0,0 +1,187 @@
1
+
2
+ require 'rexml/document'
3
+ require 'amrita2'
4
+ require 'amrita2/testsupport'
5
+
6
+ include Amrita2
7
+ include Amrita2::Filters
8
+ include Amrita2::Runtime
9
+
10
+
11
+ context "text inside CDATA is evaluated as ERb" do
12
+ include Amrita2::Runtime
13
+ setup do
14
+ @t = Amrita2::Template.new('<span><![CDATA[ <%= 1 + 1 %> ]]></span>')
15
+ end
16
+
17
+ specify "TOPLEVEL Binding" do
18
+ @t.test_with({}) do |result|
19
+ result.strip.should == "2"
20
+ end
21
+ end
22
+ end
23
+
24
+ context "you can omit CDATA in most case because AMXML preprocessor inserts it." do
25
+ include Amrita2::Runtime
26
+ setup do
27
+ @t = Amrita2::Template.new('<span><%= 1 + 1 %></span>')
28
+ end
29
+
30
+ specify "TOPLEVEL Binding" do
31
+ @t.test_with({}) do |result|
32
+ result.strip.should == "2"
33
+ end
34
+ end
35
+ end
36
+
37
+ context "Minimum mixed Template" do
38
+ include Amrita2::Runtime
39
+ setup do
40
+ @t = Amrita2::Template.new('<span><span am:src="aaa" /><![CDATA[ <%= 1 + 1 %> ]]></span>')
41
+ end
42
+
43
+ specify "TOPLEVEL Binding" do
44
+ @t.test_with(:aaa=>' 1 + 1 =') do |result|
45
+ result.strip.should == "1 + 1 = 2"
46
+ end
47
+ end
48
+ end
49
+
50
+ context "Minimum ERB Template with variable" do
51
+ include Amrita2::Runtime
52
+ setup do
53
+ @t = Amrita2::Template.new('<span><![CDATA[ <%= a + 1 %> ]]></span>')
54
+ end
55
+
56
+ specify "TOPLEVEL Binding" do
57
+ proc { @t.render_with({}) }.should raise_error(NameError)
58
+ end
59
+
60
+ specify "set Binding" do
61
+ a = 100
62
+ @t.test_with(binding) do |result|
63
+ result.strip.should == "101"
64
+ end
65
+ end
66
+ end
67
+
68
+ context "<%% ... %%>" do
69
+ include Amrita2::Runtime
70
+ specify " " do
71
+ t = Amrita2::Template.new('<%%= 1 + 1 %>')
72
+ t.test_with({}) do |result|
73
+ result.strip.should == "<%= 1 + 1 %>"
74
+ t = Amrita2::Template.new(result.dup)
75
+ t.test_with({}) do |result|
76
+ result.strip.should == "2"
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+
83
+ context '#{..} in erb' do
84
+ include Amrita2::Runtime
85
+ specify " " do
86
+ t = Amrita2::Template.new('<%= %[#{1+1}] %>')
87
+ t.test_with({}) do |result|
88
+ result.strip.should == "2"
89
+ end
90
+ end
91
+ end
92
+
93
+ context "<%% ... %%>" do
94
+ include Amrita2::Runtime
95
+ specify " " do
96
+ text = <<-END
97
+ <span>
98
+ <%= $_[:term1] %> + <%= $_[:term2] %> = <%= $_[:term1].to_i + $_[:term2].to_i %>
99
+ <%= $_[:term1] %> - <%= $_[:term2] %> = <%= $_[:term1].to_i - $_[:term2].to_i %>
100
+ </span>
101
+ END
102
+
103
+ t = Amrita2::Template.new(text, :inline_ruby)
104
+ t.test_with(:term1=>20, :term2=>30) do |result|
105
+ result.should_be_samexml_as "20 + 30 = 50 20 - 30 = -10"
106
+ end
107
+ end
108
+ end
109
+
110
+ context '<%= %> in erb' do
111
+ include Amrita2::Runtime
112
+ specify " " do
113
+ t = Amrita2::Template.new('<%= %[<%= 1+1 %%>] %>')
114
+ t.set_trace(STDOUT)
115
+ t.test_with({}) do |result|
116
+ result.strip.should == "<%= 1+1 %>"
117
+ t = Amrita2::Template.new(result.dup)
118
+ t.test_with({}) do |result|
119
+ result.strip.should == "2"
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ context "amxml block erb" do
126
+ include Amrita2::Runtime
127
+ specify " " do
128
+ text = <<-END
129
+ <<div<
130
+ % abc = 100 + 20 + 3
131
+ <<:abc>>
132
+ END
133
+ abc = nil
134
+ t = Amrita2::Template.new(text, :inline_ruby)
135
+ t.test_with(binding) do |result|
136
+ result.should_be_samexml_as "<div>123</div>"
137
+ end
138
+ end
139
+
140
+ specify "block " do
141
+ text = <<-END
142
+ <<div<
143
+ % sum = 0
144
+ % 10.times do |i|
145
+ % sum += i
146
+ % end
147
+ <<span class='sum':sum>>
148
+ END
149
+
150
+ t = Amrita2::Template.new(text, :inline_ruby)
151
+ t.test_with(binding) do |result|
152
+ result.should_be_samexml_as "<div><span class='sum'>45</span></div>"
153
+ end
154
+ end
155
+ end
156
+
157
+ context "amxml cdata_erb block('<<%<')" do
158
+ include Amrita2::Runtime
159
+ specify " " do
160
+ text = <<-END
161
+ <<ul<
162
+ <<%<
163
+ <% 3.times do |n| %>
164
+ <li><%= n + 1 %></li>
165
+ <% end %>
166
+ END
167
+ t = Amrita2::Template.new(text, :inline_ruby)
168
+ t.test_with(binding) do |result|
169
+ result.should_be_samexml_as "<ul><li>1</li><li>2</li><li>3</li></ul>"
170
+ end
171
+ end
172
+ end
173
+
174
+ context "amxml cdata_erb block(keep cdata)" do
175
+ include Amrita2::Runtime
176
+ specify " " do
177
+ text = <<-END
178
+ <<div<
179
+ <<%<
180
+ <![CDATA[aaa]]>
181
+ END
182
+ t = Amrita2::Template.new(text, :inline_ruby)
183
+ t.test_with(binding) do |result|
184
+ result.should == "<div> <![CDATA[aaa\n]]></div>"
185
+ end
186
+ end
187
+ end
data/specs/filters.rb ADDED
@@ -0,0 +1,513 @@
1
+
2
+ require 'rexml/document'
3
+ require 'amrita2'
4
+ require 'amrita2/testsupport'
5
+
6
+ include Amrita2
7
+ include Amrita2::Runtime
8
+ include Amrita2::Filters
9
+
10
+ context "Repeat Filter" do
11
+ include Amrita2::Runtime
12
+ include Amrita2::Filters
13
+
14
+ specify "with string" do
15
+ #t = Amrita2::Template.new('<div><span am:src="aaa|Repeat[5]" /></div>')
16
+
17
+ t = Amrita2::Template.new('<div><<:aaa | Repeat[5] >></div>')
18
+ t.render_with(:aaa => "A").should_be_samexml_as '<div>AAAAA</div>'
19
+ end
20
+ end
21
+
22
+ context "Default Filter" do
23
+ include Amrita2
24
+ include Amrita2::Runtime
25
+ include Amrita2::Filters
26
+
27
+ specify "with not nil data" do
28
+ begin
29
+ #t = Amrita2::Template.new('<div><span am:src="aaa|Default[123]">aaa</span></div>')
30
+
31
+ t = Amrita2::Template.new('<div><<:aaa | Default[123] >></div>')
32
+ t.test_with(:aaa => "xyz") do |result|
33
+ result.should_be_samexml_as '<div>xyz</div>'
34
+ end
35
+ end
36
+ end
37
+
38
+ specify "with nil " do
39
+ t = Amrita2::Template.new('<span><span am:src="aaa|Default[123]">aaa</span></span>')
40
+ t.test_with(:aaa => nil) do |result|
41
+ result.should_be_samexml_as '123'
42
+ end
43
+ end
44
+
45
+ specify "append filter in am:filter" do
46
+ text = '<span><span am:src="aaa" am:filter="Default[789]">aaa</span></span>'
47
+ t = Amrita2::Template.new(text)
48
+ t.test_with(:aaa => nil) do |result|
49
+ result.should_be_samexml_as '789'
50
+ end
51
+ end
52
+
53
+ specify "append filter in ruby code" do
54
+ text = '<span><span am:src="aaa">aaa</span></span>'
55
+ t = Amrita2::Template.new(text) do |e, src, filters|
56
+ if src == "aaa"
57
+ filters << Amrita2::Filters::Default[456]
58
+ end
59
+ end
60
+ t.test_with(:aaa => nil) do |result|
61
+ result.should_be_samexml_as '456'
62
+ end
63
+ end
64
+
65
+ specify "with not nil data" do
66
+ begin
67
+ t = Amrita2::Template.new(%[<div><span am:src='aaa|Default[""]|[:strftime, "%y-%m-%d"]'>aaa</span></div>])
68
+ t.test_with(:aaa => Time.parse('2007-10-01')) do |result|
69
+ result.should_be_samexml_as '<div>07-10-01</div>'
70
+ end
71
+ t.test_with(:aaa => nil) do |result|
72
+ result.should_be_samexml_as '<div />'
73
+ end
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ context "Format Filter" do
80
+ include Amrita2::Runtime
81
+ include Amrita2::Filters
82
+
83
+ specify "Integer" do
84
+ #t = Amrita2::Template.new(%q[<span><span am:src="aaa|Format['(%s)']">aaa</span></span>])
85
+
86
+ t = Amrita2::Template.new(%q[<span><<:aaa | Format['(%s)']>></span>])
87
+ t.test_with(:aaa => 123) do |result|
88
+ result.should_be_samexml_as '(123)'
89
+ end
90
+ end
91
+
92
+ specify "with Default" do
93
+ t = Amrita2::Template.new(%q[<span><span am:src="aaa|Default[456] | Format['(%s)']">aaa</span></span>])
94
+ t.render_with(:aaa => nil).should_be_samexml_as '456'
95
+ end
96
+ end
97
+
98
+ context "Other ways to setup filters" do
99
+ include Amrita2::Runtime
100
+ include Amrita2::Filters
101
+
102
+ def test(t)
103
+ t.test_with(:aaa => "A") do |result|
104
+ result.should_be_samexml_as '<div>(A)(A)(A)(A)(A)</div>'
105
+ end
106
+ t.test_with({}) do |result|
107
+ result.should_be_samexml_as '<div>a</div>'
108
+ end
109
+ end
110
+
111
+ specify "am:src" do
112
+ t = Amrita2::Template.new <<-END
113
+ <div><span am:src="aaa | Default['a'] | Format['(%s)'] | Repeat[5]" /></div>
114
+ END
115
+ test(t)
116
+ end
117
+
118
+ specify "am:filter" do
119
+ t = Amrita2::Template.new <<-END
120
+ <div><span am:src="aaa"
121
+ am:filter="Default['a'] | Format['(%s)'] | Repeat[5]" /></div>
122
+ END
123
+ test(t)
124
+ end
125
+
126
+ specify "am:src and am:filter" do
127
+ t = Amrita2::Template.new <<-END
128
+ <div><span am:src="aaa | Default['a']"
129
+ am:filter=" Format['(%s)'] | Repeat[5]" /></div>
130
+ END
131
+ test(t)
132
+ end
133
+
134
+ specify "new + block" do
135
+ text = <<-END
136
+ <div><span am:src="aaa"/></div>
137
+ END
138
+
139
+ t = Amrita2::Template.new(text) do |e, src, filters|
140
+ if e.name == 'span' and src == "aaa"
141
+ filters << Default['a'] << Format['(%s)'] << Repeat[5]
142
+ end
143
+ end
144
+
145
+ test(t)
146
+ end
147
+
148
+ specify "inline filter proc" do
149
+ t = Amrita2::Template.new <<-END
150
+ <%(BeforeCompile)
151
+ filter_setup do |e, src, filters|
152
+ if e.name == 'span' and src == "aaa"
153
+ filters << Default['a'] << Format['(%s)'] << Repeat[5]
154
+ end
155
+ end
156
+ %>
157
+ <div><span am:src="aaa" /></div>
158
+ END
159
+
160
+ test(t)
161
+ end
162
+ end
163
+
164
+ context "with am:v" do
165
+ include Amrita2::Runtime
166
+ include Amrita2::Filters
167
+
168
+ specify "Integer" do
169
+ text = %q[<span><span am:v="aaa" am:filter="Format['(%s)']">aaa</span></span>]
170
+ t = Amrita2::Template.new(text)
171
+ aaa = 123
172
+ t.test_with(binding) do |result|
173
+ result.should_be_samexml_as '(123)'
174
+ end
175
+ end
176
+ end
177
+
178
+ context "with Amrita2:Tuppl" do
179
+ include Amrita2::Runtime
180
+ include Amrita2::Filters
181
+
182
+ specify "simple" do
183
+ text = <<-END
184
+ <span>
185
+ <a am:src="aaa|NVar"
186
+ href="http://raa.ruby-lang.org/list.rhtml?name=$1">[$2]</a>
187
+ </span>
188
+ END
189
+
190
+ t = Amrita2::Template.new(text)
191
+ t.test_with(:aaa=>Amrita2::Util::Tuple['amrita', 'Amrita template library']) do |result|
192
+ result.should_be_samexml_as '<a href="http://raa.ruby-lang.org/list.rhtml?name=amrita">[Amrita template library]</a>'
193
+ end
194
+ end
195
+
196
+ specify "with a hash" do
197
+ text = <<-END
198
+ <span>
199
+ <a am:src="aaa|NVar[:name, :description]"
200
+ href="http://raa.ruby-lang.org/list.rhtml?name=$1">[$2]</a>
201
+ </span>
202
+ END
203
+ data = {
204
+ :name => 'amrita',
205
+ :description => 'Amrita template library'
206
+ }
207
+ t = Amrita2::Template.new(text)
208
+ t.test_with(:aaa=>data) do |result|
209
+ result.should_be_samexml_as '<a href="http://raa.ruby-lang.org/list.rhtml?name=amrita">[Amrita template library]</a>'
210
+ end
211
+ end
212
+
213
+ specify "with an array of hash" do
214
+ text = <<-END
215
+ <ul>
216
+ <li am:src="aaa|NVar[:name, :description]">
217
+ <a href="http://raa.ruby-lang.org/list.rhtml?name=$1">[$2]</a>
218
+ </li>
219
+ </ul>
220
+ END
221
+ data = [
222
+ {
223
+ :name => 'amrita',
224
+ :description => 'Amrita template library'
225
+ },
226
+ {
227
+ :name => 'amrita2',
228
+ :description => 'Amrita2 template library'
229
+ }
230
+ ]
231
+ t = Amrita2::Template.new(text)
232
+ t.test_with(:aaa=>data) do |result|
233
+ result.should_be_samexml_as <<-END
234
+ <ul>
235
+ <li>
236
+ <a href='http://raa.ruby-lang.org/list.rhtml?name=amrita'>[Amrita template library]</a>
237
+ </li>
238
+ <li>
239
+ <a href='http://raa.ruby-lang.org/list.rhtml?name=amrita2'>[Amrita2 template library]</a>
240
+ </li>
241
+ </ul>
242
+ END
243
+ end
244
+ end
245
+
246
+ specify "with an array of struct" do
247
+ text = <<-END
248
+ <ul>
249
+ <li am:src="aaa|NVar[:name, :description]">
250
+ <a href="http://raa.ruby-lang.org/list.rhtml?name=$1">[$2]</a>
251
+ </li>
252
+ </ul>
253
+ END
254
+ s = Struct.new(:name, :description)
255
+ data = [
256
+ s.new('amrita', 'Amrita template library'),
257
+ s.new('amrita2', 'Amrita2 template library'),
258
+ ]
259
+ t = Amrita2::Template.new(text)
260
+ t.test_with(:aaa=>data) do |result|
261
+ result.should_be_samexml_as <<-END
262
+ <ul>
263
+ <li>
264
+ <a href='http://raa.ruby-lang.org/list.rhtml?name=amrita'>[Amrita template library]</a>
265
+ </li>
266
+ <li>
267
+ <a href='http://raa.ruby-lang.org/list.rhtml?name=amrita2'>[Amrita2 template library]</a>
268
+ </li>
269
+ </ul>
270
+ END
271
+ end
272
+ end
273
+ end
274
+
275
+ context "Method Filter" do
276
+ include Amrita2::Runtime
277
+ include Amrita2::Filters
278
+
279
+ specify "with string" do
280
+ t = Amrita2::Template.new('<div><span am:src="aaa|:downcase" /></div>')
281
+ t.render_with(:aaa => "AbC").should_be_samexml_as '<div>abc</div>'
282
+ end
283
+
284
+ specify "with parameter" do
285
+ t = Amrita2::Template.new(%[<div><span am:src="aaa | FunctionFilter[:strftime, '%y-%m-%d']" /></div>])
286
+ t.render_with(:aaa => Time.parse("2007/08/31")).should_be_samexml_as '<div>07-08-31</div>'
287
+ end
288
+
289
+ specify "with parameter short" do
290
+ t = Amrita2::Template.new(%[<div><span am:src="aaa | [:strftime, '%y-%m-%d']" /></div>])
291
+ t.render_with(:aaa => Time.parse("2007/08/31")).should_be_samexml_as '<div>07-08-31</div>'
292
+ end
293
+
294
+ specify "with parameter short" do
295
+ t = Amrita2::Template.new <<-END
296
+ <<div :task<
297
+ <<:memo | Default['(no text)'] | :upcase >>
298
+ END
299
+ task =
300
+ [
301
+ { :memo => 'Default and filter' },
302
+ { :memo => nil },
303
+ { :memo => 'xyz' }
304
+ ]
305
+
306
+ t.render_with(binding).should_be_samexml_as '<div>DEFAULT AND FILTER</div><div>(no text)</div><div>XYZ</div>'
307
+ end
308
+ end
309
+
310
+ context "CommandFilter" do
311
+ include Amrita2::Runtime
312
+ include Amrita2::Filters
313
+
314
+ specify "cat -n" do
315
+ t = Amrita2::Template.new(%[<div am:src="aaa|CommandFilter['cat -n']" />])
316
+ t.test_with(:aaa => "abc") do|r|
317
+ r.should_be_samexml_as "1<div>abc</div>"
318
+ end
319
+ end
320
+
321
+ specify "tidy" do
322
+ t = Amrita2::Template.new <<-END
323
+ <<div : | CommandFilter['tidy -q -xml -indent '] <
324
+ <ul><li>1</li><li>2</li><li>3</li></ul>
325
+ END
326
+ t.test_with(binding) do|r|
327
+ r.should == <<END
328
+ <div>
329
+ <ul>
330
+ <li>1</li>
331
+ <li>2</li>
332
+ <li>3</li>
333
+ </ul>
334
+ </div>
335
+ END
336
+ end
337
+ end
338
+
339
+ specify "tidy for only debug" do
340
+ text = <<-END
341
+ <%(BeforeCompile)
342
+ self.class.module_eval { remove_const(:Tidy) } if self.class.const_defined?(:Tidy)
343
+ if ENV['RAILS_ENV'] == "development"
344
+ Tidy = CommandFilter['tidy -q -xml -indent']
345
+ else
346
+ Tidy = nil
347
+ end
348
+ %>
349
+ <<div : | Tidy <
350
+ <ul><li>1<li>2<li>3</ul>
351
+ END
352
+
353
+ ENV['RAILS_ENV'] = "development"
354
+ t = Amrita2::Template.new text
355
+ t.test_with(binding) do|r|
356
+ r.should == " \n<div>\n <ul>\n <li>1</li>\n <li>2</li>\n <li>3</li>\n </ul>\n</div>\n"
357
+ end
358
+
359
+ ENV['RAILS_ENV'] = "production"
360
+ t = Amrita2::Template.new text
361
+ t.test_with(binding) do |r|
362
+ r.should == " \n<div> <ul><li>1</li><li>2</li><li>3</li></ul>\n</div>"
363
+ end
364
+ end
365
+ end
366
+
367
+ context "Join Filter" do
368
+ include Amrita2::Runtime
369
+ include Amrita2::Filters
370
+
371
+ specify "Join elements" do
372
+ t = Amrita2::Template.new <<-END
373
+ <<div : | Join[:nbsp] <
374
+ aaaa
375
+ <a href="aaa">aaa</a>
376
+ bbbb
377
+ END
378
+ t.test_with(binding) do|r|
379
+ r.should_be_samexml_as '<div>aaaa&#160;<a href="aaa">aaa</a>&#160;bbbb</div>'
380
+ end
381
+ end
382
+
383
+ specify "Join with <br />" do
384
+ t = Amrita2::Template.new <<-END
385
+ <<div : | Join['<br />'] <
386
+ aaaa
387
+ bbbb
388
+ END
389
+ t.test_with(binding) do|r|
390
+ r.should_be_samexml_as '<div>aaaa<br />bbbb</div>'
391
+ end
392
+ end
393
+
394
+ specify "Join with nbsp" do
395
+ t = Amrita2::Template.new <<-END
396
+ <<div : | Join['&nbsp;'] <
397
+ aaaa
398
+ bbbb
399
+ END
400
+ t.test_with(binding) do|r|
401
+ r.should_be_samexml_as '<div>aaaa&nbsp;bbbb</div>'
402
+ end
403
+ end
404
+
405
+ specify "Join erb with nbsp" do
406
+ t = Amrita2::Template.new <<-END
407
+ <<div : | Join['&nbsp;'] <
408
+ aaaa
409
+ %=1
410
+ bbbb
411
+ %=1+1
412
+ cccc
413
+ END
414
+ t.test_with(binding) do|r|
415
+ r.should_be_samexml_as '<div>aaaa&nbsp;1&nbsp;bbbb&nbsp;2&nbsp;cccc</div>'
416
+ end
417
+ end
418
+
419
+ end
420
+
421
+ context "Stream Filter" do
422
+ include Amrita2::Runtime
423
+ include Amrita2::Filters
424
+
425
+ specify "Change Stream" do
426
+ t = Amrita2::Template.new <<-END
427
+ <<abc=<
428
+ aaaa
429
+ bbbb
430
+ <<=abc>>
431
+ END
432
+ t.test_with(binding) do|r|
433
+ r.should_be_samexml_as 'bbbb aaaa'
434
+ end
435
+ end
436
+
437
+ specify "Change Stream with erb" do
438
+ t = Amrita2::Template.new <<-END
439
+ <<abc=<
440
+ %= "aaaa"
441
+
442
+ %= "bbbb"
443
+ <<=abc>>
444
+ END
445
+ t.test_with(binding) do|r|
446
+ r.should_be_samexml_as 'bbbb aaaa'
447
+ end
448
+ end
449
+
450
+ specify "multi streams" do
451
+ t = Amrita2::Template.new <<-END
452
+ <<abc=<
453
+ aaa
454
+ <<efg=<
455
+ bbb
456
+ ccc
457
+ ddd
458
+ <<=abc>>
459
+ <<=efg>>
460
+ END
461
+ t.test_with(binding) do|r|
462
+ r.should_be_samexml_as 'ddd aaa ccc bbb'
463
+ end
464
+ end
465
+ end
466
+
467
+ context "Module Extend Filter" do
468
+ include Amrita2::Runtime
469
+ include Amrita2::Filters
470
+
471
+ module ModuleExtendFilterTest
472
+ def bbb
473
+ "(#{super})"
474
+ end
475
+ end
476
+
477
+ specify "module extend" do
478
+ t = Amrita2::Template.new <<-END
479
+ <<div :aaa | ModuleExtendFilterTest <
480
+ <<:bbb>>
481
+ END
482
+ aaa = Struct.new(:bbb).new('bbb')
483
+ t.test_with(binding) do |r|
484
+ r.should_be_samexml_as '<div>(bbb)</div>'
485
+ end
486
+ end
487
+ end
488
+
489
+
490
+ context "ToHash Filter" do
491
+ include Amrita2::Runtime
492
+ include Amrita2::Filters
493
+
494
+ specify "with string" do
495
+ t = Amrita2::Template.new <<-END
496
+ <<:a|ToHash[:xxx]<
497
+ <<:xxx>>
498
+ END
499
+
500
+ t.render_with(:a => "A").should_be_samexml_as 'A'
501
+ end
502
+
503
+ specify "many keys" do
504
+ t = Amrita2::Template.new <<-END
505
+ <<:time | ToHash[:h=>:hour, :m=>:min, :s=>:sec] <
506
+ <<:h>>:<<:m>>:<<:s>>
507
+ END
508
+
509
+ tm = Time.local(2008,1,10,9,10,30)
510
+ tm.extend(Amrita2::DictionaryData)
511
+ t.render_with(:time=>tm).should_be_samexml_as '9:10:30'
512
+ end
513
+ end