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
data/specs/macro.rb ADDED
@@ -0,0 +1,532 @@
1
+ require 'rexml/document'
2
+ require 'amrita2'
3
+ require 'amrita2/macro'
4
+ require 'amrita2/testsupport'
5
+
6
+ include Amrita2::Runtime
7
+
8
+ context "Amrita2 has Macro" do
9
+ class Hello < Amrita2::Macro::Base
10
+ include Amrita2
11
+ #TemplateText = <<-END
12
+ # <span macro:src="greeting" />, macro world!
13
+ #END
14
+ TemplateText = <<-END
15
+ <<:greeting>>, macro world!
16
+ END
17
+ end
18
+
19
+ specify " " do
20
+ text = <<-END
21
+ <span><hello greeting="Hello" /> <span am:src="msg" /></span>
22
+ END
23
+ t = Amrita2::Template.new(text)
24
+ t.add_macro(Hello.new)
25
+ t.test_with(:msg => "Enjoy it!") do |result|
26
+ result.should_be_samexml_as "Hello, macro world! Enjoy it!"
27
+ end
28
+ end
29
+ end
30
+
31
+
32
+ context "Macro can contain dynamic element" do
33
+ class Hello2 < Amrita2::Macro::Base
34
+ include Amrita2
35
+
36
+ TemplateText = <<-END.gsub(/\s+/m, ' ')
37
+ <span macro:src="greeting" />, macro world!
38
+ <span target_src="msg" />
39
+ END
40
+ end
41
+
42
+ specify " " do
43
+ text = <<-END
44
+ <%(BeforeCompile) use_macro(Hello2) %>
45
+ <span><hello2 greeting='Hello'/> </span>
46
+ END
47
+ t = Amrita2::Template.new(text)
48
+ t.add_macro(Hello2.new)
49
+
50
+ t.test_with(:msg => "Enjoy it!") do |result|
51
+ result.should_be_samexml_as "Hello, macro world! Enjoy it! \n"
52
+ end
53
+ end
54
+ end
55
+
56
+ context "two macros in one template" do
57
+ class Hello3 < Amrita2::Macro::Base
58
+ include Amrita2
59
+
60
+ TemplateText = <<-END
61
+ <span macro:src="hello">
62
+ <span macro:src="greeting" />, macro world!
63
+ <span macro:filter="Attr[:target_src=>:amrita_id]" />
64
+ </span>
65
+ END
66
+ Option = {
67
+ :tag => :hello,
68
+ :use_tag => true
69
+ }
70
+ end
71
+
72
+ specify " " do
73
+ text = <<-END
74
+ <ul>
75
+ <li><hello greeting="Good morning" amrita_id="msg1"/></li>
76
+ <li><hello greeting="Hello" amrita_id="msg2"/></li>
77
+ </ul>
78
+ END
79
+ t = Amrita2::Template.new(text)
80
+ t.add_macro(Hello3.new)
81
+ t.test_with(:msg1 => "Enjoy it!", :msg2=> "It's great, isn't it?") do |result|
82
+ result.should_be_samexml_as <<-END
83
+ <ul>
84
+ <li>Good morning, macro world! Enjoy it!</li>
85
+ <li>Hello, macro world! It's great, isn't it?</li>
86
+ </ul>
87
+ END
88
+ end #"
89
+ end
90
+ end
91
+
92
+ context "Macro can expand child elements" do
93
+ class Hello4 < Amrita2::Macro::Base
94
+ include Amrita2
95
+ TemplateText = <<-END
96
+ <p macro:src="hello">
97
+ <span macro:src="msg" />, macro world!
98
+ <span macro:src="contents" />
99
+ </p>
100
+ END
101
+
102
+ Option = {
103
+ :tag=>:hello,
104
+ :use_tag => true,
105
+ :use_contents=>:contents
106
+ }
107
+
108
+ end
109
+
110
+ specify " " do
111
+ text = <<-END
112
+ <ul>
113
+ <li><hello msg="Good morning"><span am:src="msg1" /><em am:src="msg2" /></hello></li>
114
+ <li><hello msg="Hello" ><strong am:src="msg3" /></hello></li>
115
+ </ul>
116
+ END
117
+ t = Amrita2::Template.new(text)
118
+ t.add_macro(Hello4.new)
119
+ t.test_with(:msg1 => "Try it.", :msg2=>"Enjoy it!", :msg3=> "It's great, isn't it?") do |result|
120
+ result.should_be_samexml_as <<-END
121
+ <ul>
122
+ <li><p>Good morning, macro world! Try it.<em>Enjoy it!</em> </p></li>
123
+ <li><p>Hello, macro world! <strong>It's great, isn't it?</strong> </p></li>
124
+ </ul>
125
+ END
126
+ end # "
127
+ end
128
+ end
129
+
130
+ context "Amazon Link Macro" do
131
+ class Amazon < Amrita2::Macro::Base
132
+ include Amrita2
133
+ ElementName = "book"
134
+ TemplateText = <<-END
135
+ <span>
136
+ <a macro:src = "book|NVar[:isbn, :content]"
137
+ target_src = "book|NVar[:$1, :$2]"
138
+ href="http://www.amazon.co.jp/exec/obidos/ASIN/$$1/">
139
+ $$2
140
+ </a>
141
+ </span>
142
+ END
143
+ Option = {
144
+ :tag=>:book,
145
+ :use_tag => true,
146
+ :use_contents=>true
147
+ }
148
+ end
149
+
150
+ specify " " do
151
+ text = <<-END
152
+ <div>
153
+ <book isbn="isbn" content="title" />
154
+ </div>
155
+ END
156
+ #
157
+ # after macro expansion
158
+ # <div><a href='http://www.amazon.co.jp/exec/obidos/ASIN/$1/'
159
+ # am:src='book|NVar[:isbn_no, :title]'>$2</a></div>
160
+ #
161
+ t = Amrita2::Template.new(text) do |e, name, filters|
162
+ filters << Amrita2::Filters::MacroFilter[Amazon]
163
+ end
164
+
165
+ data = {
166
+ :book=>{
167
+ :isbn=>'4798013951',
168
+ :title=>'Ruby on Rails for dummies'
169
+ }
170
+ }
171
+ #t.set_trace(STDOUT)
172
+ t.test_with(data) do |result|
173
+ result.should_be_samexml_as <<-END
174
+ <div>
175
+ <a href="http://www.amazon.co.jp/exec/obidos/ASIN/4798013951/">Ruby on Rails for dummies</a>
176
+ </div>
177
+ END
178
+ end
179
+ end
180
+ end
181
+
182
+ context "Macro with erb" do
183
+ class Formula < Amrita2::Macro::Base
184
+ include Amrita2
185
+ TemplateText = <<-END
186
+ <span macro:src = "formula">
187
+ <span macro:skipif="$_[:op] != '+'">
188
+ <span macro:filter="NVar[:name, :term1, :term2]"
189
+ target_src="$1">
190
+ <%%= $_[:$2] %%> + <%%= $_[:$3] %%> = <%%= $_[:$2].to_i + $_[:$3].to_i %%>
191
+ </span>
192
+ </span>
193
+ <span macro:skipif="$_[:op] != '-'">
194
+ <span macro:filter="NVar[:name, :term1, :term2]"
195
+ target_src="$1">
196
+ <%%= $_[:$2] %%> - <%%= $_[:$3] %%> = <%%= $_[:$2].to_i - $_[:$3].to_i %%>
197
+ </span>
198
+ </span>
199
+ </span>
200
+ END
201
+
202
+ Option = {
203
+ :tag => :formula,
204
+ :use_tag => true
205
+ }
206
+ end
207
+
208
+ specify " " do
209
+ text = <<-END
210
+ <formula name="plus" op="+" term1="term1" term2="term2"/>/
211
+ <formula name="minus" op="-" term1="num1" term2="num2"/>
212
+ END
213
+
214
+ t = Amrita2::Template.new(text,:inline_ruby) do |e, name, filters|
215
+ filters << Amrita2::Filters::MacroFilter[Formula]
216
+ end
217
+
218
+ #
219
+ # after macro expansion
220
+ # <span target_src='plus'>
221
+ # <%%= $_[:term1] %> + <%%= $_[:term2] %> = <%%= $_[:term1].to_i + $_[:term2].to_i %>
222
+ # </span>
223
+ # <span target_src='minus'>
224
+ # <%%= $_[:num1] %> - <%%= $_[:num2] %> = <%%= $_[:num1].to_i - $_[:num2].to_i %>
225
+ # </span>
226
+ #
227
+
228
+ data = {
229
+ :plus => {
230
+ :term1 => 10,
231
+ :term2 => 20,
232
+ },
233
+ :minus => {
234
+ :num1 => 30,
235
+ :num2 => 5
236
+ }
237
+ }
238
+ #t.set_trace(STDOUT)
239
+ t.test_with(data) do |result|
240
+ r = result.gsub(/[\s\n]*/, "")
241
+ r.should == '10+20=30/30-5=25'
242
+ end
243
+ end
244
+ end
245
+
246
+ context "Macro producing erb" do
247
+ class ErbTest < Amrita2::Macro::Base
248
+ include Amrita2
249
+ TemplateText = <<-'END'
250
+ <div>
251
+ <% var_defined_in_erb_of_macro = value_of_macro_ref.to_i * 10 %>
252
+ <img macro:filter="NVar[:host, :var_defined_in_erb_of_macro]" src="http://$1/img$2.png" />
253
+
254
+ <%% var_defined_in_erb_of_template = value_of_model.to_i * 10 %>
255
+ <img macro:filter="NVar[:host]"
256
+ target_filter="NVar[:var_defined_in_erb_of_template]"
257
+ src="http://$1.com/img$$1.png" />
258
+
259
+ <%% var_generated_with_macro_and_model_value = "#{ value_of_model + <%= value_of_macro_ref %>}" %>
260
+ <img macro:filter="NVar[:host]"
261
+ target_filter="NVar[:var_generated_with_macro_and_model_value]"
262
+ src="http://$1/img$$1.png" />
263
+ </div>
264
+ END
265
+
266
+ Option = {
267
+ :tag => :erbtest
268
+ }
269
+
270
+ def preprocess_element(mt, element)
271
+ host = element.attributes["host"]
272
+ value_of_macro_ref = element.attributes["value_of_macro_ref"]
273
+ mt.render_with(binding)
274
+ end
275
+
276
+ end
277
+
278
+ specify " " do
279
+ text = <<-END
280
+ <erbtest host="some.host.com" value_of_macro_ref="10" />
281
+ END
282
+
283
+ #<div>
284
+ # <img src='http://some.host.com/img100.png'/>
285
+ #
286
+ # <![CDATA[<% var_defined_in_erb_of_template = value_of_model.to_i * 10 %>]]>
287
+ # <img am:filter='NVar[:var_defined_in_erb_of_template]' src='http://some.host.com.com/img$1.png'/>
288
+ #
289
+ # <![CDATA[<% var_generated_with_macro_and_model_value = "#{ value_of_model + 10}" %>]]>
290
+ # <img am:filter='NVar[:var_generated_with_macro_and_model_value]' src='http://some.host.com/img$1.png'/>
291
+ #
292
+ #</div>
293
+
294
+ t = Amrita2::Template.new(text,:inline_ruby) do |e, name, filters|
295
+ filters << Amrita2::Filters::MacroFilter[ErbTest]
296
+ end
297
+
298
+ value_of_model = 5
299
+ #t.set_trace(STDOUT)
300
+ t.test_with(binding) do |result|
301
+ result.should_be_samexml_as <<-END
302
+ <div>
303
+ <img src = "http://some.host.com/img100.png" />
304
+ <img src='http://some.host.com.com/img50.png'/>
305
+ <img src='http://some.host.com/img15.png'/>
306
+ </div>
307
+ END
308
+ end
309
+ end
310
+ end
311
+
312
+ context "Table" do
313
+ class Table < Amrita2::Macro::Base
314
+ include Amrita2
315
+ TemplateText = <<-END
316
+ <table>
317
+ <tr><th macro:src='header'/></tr>
318
+ <tr target_src='detail'>
319
+ <td macro:src="column|Attr[:target_src=>'column_id']">
320
+ <span macro:src="contents" />
321
+ </td>
322
+ </tr>
323
+ </table>
324
+ END
325
+
326
+ Option = {
327
+ :tag => "m:table"
328
+ }
329
+
330
+ def preprocess_element(mt, element)
331
+ cols = element.search("column").collect do |e|
332
+ e.as_amrita_dictionary(:use_contents=>:contents)
333
+ end
334
+
335
+ headers = cols.collect do |d|
336
+ d[:header]
337
+ end
338
+
339
+ data = {
340
+ :header => headers,
341
+ :column => cols,
342
+ }
343
+
344
+ #p data
345
+ #mt.set_trace(STDOUT)
346
+ mt.render_with(data)
347
+ end
348
+ end
349
+
350
+ specify " " do
351
+ text = <<-END
352
+ <m:table>
353
+ <column header='AAA' column_id='aaa' />
354
+ <column header='BBB' column_id='bbb'>
355
+ <a am:filter="NVar[:url, :title]" href="$1">$2</a>
356
+ </column>
357
+ </m:table>
358
+ END
359
+ t = Amrita2::Template.new(text)
360
+ t.add_macro(Table.new)
361
+
362
+ #<table>
363
+ # <tr><th>AAA</th><th>BBB</th></tr>
364
+ # <tr am:src='detail'>
365
+ # <td am:src='aaa'>
366
+ # </td>
367
+ # <td am:src='bbb'>
368
+ # <a am:filter='NVar[:url, :title]' href='$1'>$2</a>
369
+ # </td>
370
+ # </tr>
371
+ #</table>
372
+
373
+ data = {
374
+ :detail => [
375
+ { :aaa=>'a1', :bbb=>{ :url=>'url1', :title=>'b1'} },
376
+ { :aaa=>'a2', :bbb=>{ :url=>'url2', :title=>'b2'} },
377
+ { :aaa=>'a3', :bbb=>{ :url=>'url3', :title=>'b3'} },
378
+ ]
379
+ }
380
+ t.test_with(data) do |result|
381
+ result.should_be_samexml_as <<-END
382
+ <table>
383
+ <tr><th>AAA</th><th>BBB</th></tr>
384
+ <tr>
385
+ <td>a1</td><td>
386
+ <a href='url1'>b1</a>
387
+ </td>
388
+ </tr><tr>
389
+ <td>a2</td><td>
390
+ <a href='url2'>b2</a>
391
+ </td>
392
+ </tr><tr>
393
+ <td>a3</td><td>
394
+ <a href='url3'>b3</a>
395
+ </td>
396
+ </tr>
397
+ </table>
398
+ END
399
+ end
400
+ end
401
+ end
402
+
403
+
404
+ context "Table and Amazon" do
405
+ specify " " do
406
+ text = <<-END
407
+ <m:table>
408
+ <column header='No' column_id='no' />
409
+ <column header='Title'>
410
+ <book isbn="isbn" content="title" />
411
+ </column>
412
+ </m:table>
413
+ END
414
+ t = Amrita2::Template.new(text)
415
+ t.add_macro(Table.new)
416
+ t.add_macro(Amazon.new)
417
+
418
+ #<table>
419
+ # <tr><th>No</th><th>Title</th></tr>
420
+ # <tr am:src='detail'>
421
+ # <td am:src='no'/>
422
+ # <td>
423
+ # <a href='http://www.amazon.co.jp/exec/obidos/ASIN/$1/' am:src='book|NVar[:isbn, :title]'>$2</a>
424
+ # </td>
425
+ # </tr>
426
+ #</table>
427
+
428
+ data = {
429
+ :detail => [
430
+ { :no=>'1', :book=>{ :isbn=>'isbn1', :title=>'b1'} },
431
+ { :no=>'2', :book=>{ :isbn=>'isbn2', :title=>'b2'} },
432
+ { :no=>'3', :book=>{ :isbn=>'isbn3', :title=>'b3'} },
433
+ ]
434
+ }
435
+ t.test_with(data) do |result|
436
+ #IO.popen("w3m -T text/html", "w") { |f| f.puts result}
437
+ result.should_be_samexml_as <<-END
438
+ <table>
439
+ <tr><th>No</th><th>Title</th></tr>
440
+ <tr>
441
+ <td>1</td>
442
+ <td>
443
+ <a href='http://www.amazon.co.jp/exec/obidos/ASIN/isbn1/'>b1</a>
444
+ </td>
445
+ </tr>
446
+ <tr>
447
+ <td>2</td>
448
+ <td>
449
+ <a href='http://www.amazon.co.jp/exec/obidos/ASIN/isbn2/'>b2</a>
450
+ </td>
451
+ </tr>
452
+ <tr>
453
+ <td>3</td>
454
+ <td>
455
+ <a href='http://www.amazon.co.jp/exec/obidos/ASIN/isbn3/'>b3</a>
456
+ </td>
457
+ </tr>
458
+ </table>
459
+ END
460
+ end
461
+ end
462
+ end
463
+
464
+ context "Two columns" do
465
+ # taken from LoginEngine
466
+ class TwoColumn < Amrita2::Macro::Base
467
+ include Amrita2
468
+ ElementName = "twocolumn"
469
+ TemplateText = <<-END
470
+ <table target_src="table|AcceptData[true]">
471
+ <span macro:src="rows" >
472
+ <tr macro:filter="Attr[:class=>:tr_class]">
473
+ <td macro:filter="Attr[:class=>:prompt_class]"><label macro:src="title" /></td>
474
+ <td macro:filter="Attr[:class=>:value_class, :body=>:contents]" />
475
+ </tr><br />
476
+ </span>
477
+ </table>
478
+ END
479
+
480
+ Option = {
481
+ :tag => :twocolumn
482
+ }
483
+
484
+ def preprocess_element(mt, element)
485
+ table = element.as_amrita_dictionary
486
+ rows = element.search("row").collect do |c|
487
+ h = c.as_amrita_dictionary(:use_contents=>:contents)
488
+ h[:tr_class] = table[:tr_class] || "two_columns"
489
+ h[:prompt_class] = table[:prompt_class] || "prompt"
490
+ h[:value_class] = table[:value_class] || "value"
491
+ h
492
+ end
493
+
494
+ mt.render_with(binding)
495
+ end
496
+ end
497
+
498
+ specify " " do
499
+ text = <<-END
500
+ <twocolumn tr_class="two_columns" prompt_class="prompt" value_class="value">
501
+ <row title="Login ID:"><input name="user[login]" size="30" type="text" /></row>
502
+ <row title="Password:"><input name="user[password]" size="30" type="password" value="" /></row>
503
+ </twocolumn>
504
+ END
505
+
506
+ text = <<-END
507
+ <twocolumn>
508
+ <row title="Login ID:"><input name="user[login]" size="30" type="text" /></row>
509
+ <row title="Password:"><input name="user[password]" size="30" type="password" value="" /></row>
510
+ </twocolumn>
511
+ END
512
+
513
+ t = Amrita2::Template.new(text)
514
+ t.add_macro(TwoColumn)
515
+ t.test_with(:table=>true) do |result|
516
+ result.should_be_samexml_as <<-END
517
+ <table>
518
+ <tr class="two_columns">
519
+ <td class="prompt"><label>Login ID:</label></td>
520
+ <td class="value"><input name="user[login]" size="30" type="text" /></td>
521
+ </tr>
522
+ <br />
523
+ <tr class="two_columns">
524
+ <td class="prompt"><label>Password:</label></td>
525
+ <td class="value"><input name="user[password]" size="30" type="password" value="" /></td>
526
+ </tr>
527
+ <br />
528
+ </table>
529
+ END
530
+ end
531
+ end
532
+ end
data/specs/sample.rb ADDED
@@ -0,0 +1,34 @@
1
+
2
+ require 'amrita2'
3
+ require 'amrita2/testsupport'
4
+
5
+ module SampleTestSupport
6
+ def get_result(dir, fname)
7
+ Dir::chdir("sample/#{dir}") do
8
+ IO.popen("ruby -I../../lib #{fname}") do |f|
9
+ f.read
10
+ end
11
+ end
12
+ end
13
+
14
+ def get_expected(dir, fname)
15
+ File::open("sample/#{dir}/#{fname}") do |f|
16
+ f.read.split(/__END__/m).last
17
+ end
18
+ end
19
+
20
+ def compare(dir, fname)
21
+ result = get_result(dir, fname)
22
+ expected = get_expected(dir, fname)
23
+ result.should_be_samexml_as expected
24
+ end
25
+ end
26
+
27
+
28
+ context "hello" do
29
+ include SampleTestSupport
30
+
31
+ specify "hello.rb" do
32
+ compare('hello', 'hello.rb')
33
+ end
34
+ end
data/specs/sanitize.rb ADDED
@@ -0,0 +1,110 @@
1
+
2
+ require 'rexml/document'
3
+ require 'amrita2'
4
+ require 'amrita2/testsupport'
5
+
6
+
7
+ context "Sanitize" do
8
+ include Amrita2
9
+ include Amrita2::Filters
10
+
11
+ setup do
12
+ @t = Amrita2::Template.new('<span><span am:src="aaa" /></span>')
13
+ end
14
+
15
+ specify "no sanitize" do
16
+ @t.render_with(:aaa=>'Amrita2').should_be_samexml_as("Amrita2")
17
+ end
18
+
19
+ specify "do sanitize" do
20
+ @t.render_with(:aaa=>'&<>').should_be_samexml_as("&amp;&lt;&gt;")
21
+ @t.render_with(:aaa=>'<strong>Amrita2</strong>').should_be_samexml_as("&lt;strong&gt;Amrita2&lt;/strong&gt;")
22
+ end
23
+
24
+ specify "sanitized string" do
25
+ @t.render_with(:aaa=>Amrita2::SanitizedString['<strong>Amrita2</strong>']).should_be_samexml_as('<strong>Amrita2</strong>')
26
+ end
27
+
28
+ specify "NoSanitize filter" do
29
+ t = Amrita2::Template.new('<span><span am:src="aaa|NoSanitize" /></span>')
30
+ t.render_with(:aaa=>'<strong>Amrita2</strong>').should_be_samexml_as('<strong>Amrita2</strong>')
31
+ end
32
+ end
33
+
34
+ context "Sanitize attributes" do
35
+ include Amrita2
36
+ setup do
37
+ @t = Amrita2::Template.new('<span><a am:src="aaa|Attr[:href, :title, :body]" /></span>')
38
+ end
39
+
40
+ specify "no sanitize" do
41
+ data = {
42
+ :aaa=> {
43
+ :href=>'http://www.ruby-lang.org/',
44
+ :body=>'Ruby'
45
+ }
46
+ }
47
+ expected = "<a href='http://www.ruby-lang.org/'>Ruby</a>"
48
+ @t.render_with(data).should_be_samexml_as(expected)
49
+ end
50
+
51
+ specify "do sanitize" do
52
+ data = {
53
+ :aaa=> {
54
+ :href=>'http://www.ruby-lang.org/',
55
+ :title=>"Ruby's home page",
56
+ :body=>'<Ruby>'
57
+ }
58
+ }
59
+ expected = %[<a href="http://www.ruby-lang.org/" title="Ruby's home page">&lt;Ruby&gt;</a>]
60
+ @t.test_with(data) do |result|
61
+ result.should_be_samexml_as(expected)
62
+ end
63
+ end
64
+
65
+ specify "stop sanitize" do
66
+ data = {
67
+ :aaa=> {
68
+ :href=>'http://www.ruby-lang.org/',
69
+ :title=>"Ruby's home page",
70
+ :body=>Amrita2::SanitizedString['<strong>Ruby</strong>']
71
+ }
72
+ }
73
+ expected = %[<a href='http://www.ruby-lang.org/' title="Ruby's home page"><strong>Ruby</strong></a>]
74
+ @t.test_with(data) do |result|
75
+ result.should_be_samexml_as(expected)
76
+ end
77
+ end
78
+ end
79
+
80
+ context "Sanitize attributes with NVar" do
81
+ include Amrita2
82
+ specify "do sanitize " do
83
+ t = Amrita2::Template.new('<span><a am:src="aaa|NVar[:url, :body]" href="$1">$2</a></span>')
84
+ t.test_with(:aaa =>{:url=>'a"b"c', :body=>'efg'}) do |result|
85
+ result.should_be_samexml_as('<a href="a&quot;b&quot;c">efg</a>')
86
+ end
87
+
88
+ t = Amrita2::Template.new('<span><a am:src="aaa|NVarForAttr[:url]" href="$1">efg</a></span>')
89
+ t.test_with(:aaa =>{:url=>'a"b"c', :body=>'efg'}) do |result|
90
+ result.should_be_samexml_as('<a href="a&quot;b&quot;c">efg</a>')
91
+ end
92
+ end
93
+ end
94
+
95
+ context "Skip Sanitize to result of Template" do
96
+ include Amrita2
97
+
98
+ setup do
99
+ @t1 = Amrita2::Template.new('<p><span am:src="aaa" /></p>')
100
+ @t2 = Amrita2::Template.new('<body><span am:src="aaa" /></body>')
101
+ end
102
+
103
+ specify "no sanitize " do
104
+ a = @t1.render_with(:aaa=>'Amrita2')
105
+ a.should_be_samexml_as("<p>Amrita2</p>")
106
+ @t2.test_with(:aaa=>a) do |result|
107
+ result.should_be_samexml_as("<body><p>Amrita2</p></body>")
108
+ end
109
+ end
110
+ end