amrita2 1.9.6 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
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,116 @@
1
+
2
+ require File.dirname(__FILE__) + '/../test_helper'
3
+ class UserTest < Test::Unit::TestCase
4
+
5
+ # load the fixture into the developer-specified table using the custom
6
+ # 'fixture' method.
7
+ fixtures :users
8
+ #fixture :users, :table_name => LoginEngine.config(:user_table), :class_name => "User"
9
+
10
+ def setup
11
+ LoginEngine::CONFIG[:salt] = "test-salt"
12
+ end
13
+
14
+ def test_auth
15
+ assert_equal users(:bob), User.authenticate("bob", "atest")
16
+ assert_nil User.authenticate("nonbob", "atest")
17
+ end
18
+
19
+
20
+ def test_passwordchange
21
+
22
+ users(:longbob).change_password("nonbobpasswd")
23
+ users(:longbob).save
24
+ assert_equal users(:longbob), User.authenticate("longbob", "nonbobpasswd")
25
+ assert_nil User.authenticate("longbob", "alongtest")
26
+ users(:longbob).change_password("alongtest")
27
+ users(:longbob).save
28
+ assert_equal users(:longbob), User.authenticate("longbob", "alongtest")
29
+ assert_nil User.authenticate("longbob", "nonbobpasswd")
30
+
31
+ end
32
+
33
+ def test_disallowed_passwords
34
+
35
+ u = User.new
36
+ u.login = "nonbob"
37
+ u.email = "bobs@email.com"
38
+
39
+ u.change_password("tiny")
40
+ assert !u.save
41
+ assert u.errors.invalid?('password')
42
+
43
+ u.change_password("hugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehuge")
44
+ assert !u.save
45
+ assert u.errors.invalid?('password')
46
+
47
+ u.change_password("")
48
+ assert !u.save
49
+ assert u.errors.invalid?('password')
50
+
51
+ u.change_password("bobs_secure_password")
52
+ assert u.save
53
+ assert u.errors.empty?
54
+
55
+ end
56
+
57
+ def test_bad_logins
58
+
59
+ u = User.new
60
+ u.change_password("bobs_secure_password")
61
+ u.email = "bobs@email.com"
62
+
63
+ u.login = "x"
64
+ assert !u.save
65
+ assert u.errors.invalid?('login')
66
+
67
+ u.login = "hugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhugebobhug"
68
+ assert !u.save
69
+ assert u.errors.invalid?('login')
70
+
71
+ u.login = ""
72
+ assert !u.save
73
+ assert u.errors.invalid?('login')
74
+
75
+ u.login = "okbob"
76
+ assert u.save
77
+ assert u.errors.empty?
78
+
79
+ end
80
+
81
+
82
+ def test_collision
83
+ u = User.new
84
+ u.login = "existingbob"
85
+ u.change_password("bobs_secure_password")
86
+ assert !u.save
87
+ end
88
+
89
+
90
+ def test_create
91
+ u = User.new
92
+ u.login = "nonexistingbob"
93
+ u.change_password("bobs_secure_password")
94
+ u.email = "bobs@email.com"
95
+
96
+ assert u.save
97
+
98
+ end
99
+
100
+ def test_email_should_be_nominally_valid
101
+ u = User.new
102
+ u.login = "email_test"
103
+ u.change_password("email_test_password")
104
+
105
+ assert !u.save
106
+ assert u.errors.invalid?('email')
107
+
108
+ u.email = "invalid_email"
109
+ assert !u.save
110
+ assert u.errors.invalid?('email')
111
+
112
+ u.email = "valid@email.com"
113
+ assert u.save
114
+ end
115
+
116
+ end
@@ -0,0 +1,6 @@
1
+ # plugin init files for Ruby On Rails
2
+
3
+ require 'amrita2/rails_bridge'
4
+
5
+ ActionView::Base.register_template_handler "a2html", Amrita2View::Base
6
+
@@ -0,0 +1,201 @@
1
+
2
+ require 'rexml/document'
3
+ require 'amrita2'
4
+ require 'amrita2/testsupport'
5
+
6
+ include Amrita2::Filters
7
+ include Amrita2::Runtime
8
+
9
+ context "Attribute" do
10
+
11
+ specify "set attribute" do
12
+ #t = Amrita2::Template.new('<div><a am:src="aaa|Attr[:href]">aaa</a></div>')
13
+ t = Amrita2::Template.new <<-END
14
+ <<div<
15
+ <<a :aaa|Attr[:href]<
16
+ xyz
17
+ END
18
+ t.test_with(:aaa => {
19
+ :href=>"http://amrita2.rubyforge.org/",
20
+ }
21
+ ) do |r|
22
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/">xyz</a></div>'
23
+ end
24
+ end
25
+
26
+ specify "delete attribute" do
27
+ #t = Amrita2::Template.new('<div><a class="highlight" am:src="aaa|Attr[:href, :class, :body]">aaa</a></div>')
28
+ t = Amrita2::Template.new <<-END
29
+ <<div<
30
+ <<a.highlight :aaa | Attr[:href, :class, :body] <
31
+ aaa
32
+ END
33
+ t.test_with(:aaa => {
34
+ :href=>"http://amrita2.rubyforge.org/",
35
+ :class=>true,
36
+ :body=>"Amrita"
37
+ }
38
+ ) do |r|
39
+ r.should_be_samexml_as '<div><a class="highlight" href="http://amrita2.rubyforge.org/">Amrita</a></div>'
40
+ end
41
+ t.test_with(:aaa => {
42
+ :href=>"http://amrita2.rubyforge.org/",
43
+ :class=>false,
44
+ :body=>"Amrita"
45
+ }
46
+ ) do |r|
47
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/">Amrita</a></div>'
48
+ end
49
+ t.test_with(:aaa => {
50
+ :href=>"http://amrita2.rubyforge.org/",
51
+ :class=>nil,
52
+ :body=>"Amrita"
53
+ }
54
+ ) do |r|
55
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/">Amrita</a></div>'
56
+ end
57
+ end
58
+
59
+ specify "delete attribute2" do
60
+ t = Amrita2::Template.new('<<div#cart : | Attr[:style] >>')
61
+ t.test_with(:style => "display: none") do |r|
62
+ r.should_be_samexml_as '<div id="cart" style="display: none" />'
63
+ end
64
+ t.test_with(:style => nil) do |r|
65
+ r.should_be_samexml_as '<div id="cart" />'
66
+ end
67
+ t.test_with(:style => false) do |r|
68
+ r.should_be_samexml_as '<div id="cart" />'
69
+ end
70
+ t.test_with(:style => "") do |r|
71
+ r.should_be_samexml_as '<div id="cart" style=""/>'
72
+ end
73
+ end
74
+
75
+ specify "change attribute key" do
76
+ t = Amrita2::Template.new('<div><a am:src="aaa|Attr[:body=>true, :href=>:url]">aaa</a></div>')
77
+ t.test_with(:aaa => {
78
+ :url=>"http://amrita2.rubyforge.org/",
79
+ :body=>"Amrita"
80
+ }
81
+ ) do |r|
82
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/">Amrita</a></div>'
83
+ end
84
+ end
85
+
86
+ specify "change body key" do
87
+ t = Amrita2::Template.new('<div><a am:src="aaa|Attr[:href=>:url, :body=>:text]">aaa</a></div>')
88
+ t.test_with(:aaa => {
89
+ :url=>"http://amrita2.rubyforge.org/",
90
+ :text=>"Amrita"
91
+ }
92
+ ) do |r|
93
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/">Amrita</a></div>'
94
+ end
95
+ end
96
+
97
+ specify "inner dynamic element" do
98
+ #text = '<div><a am:src="aaa|Attr[:href=>:url]"><span am:src="inner"/></a></div>'
99
+ text = <<-END
100
+ <<div<
101
+ <<a :aaa | Attr[:href=>:url]<
102
+ <<:inner>>
103
+ END
104
+ t = Amrita2::Template.new(text)
105
+ t.test_with(:aaa => {
106
+ :url=>"http://amrita2.rubyforge.org/",
107
+ :inner=>"Amrita"
108
+ }
109
+ ) do |r|
110
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/">Amrita</a></div>'
111
+ end
112
+
113
+ text = '<div><a am:src="aaa|Attr[:href=>:url]">(<em><span am:src="inner"/></em>)</a></div>'
114
+ t = Amrita2::Template.new(text)
115
+ t.test_with(:aaa => {
116
+ :url=>"http://amrita2.rubyforge.org/",
117
+ :inner=>"Amrita"
118
+ }
119
+ ) do |r|
120
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/">(<em>Amrita</em>)</a></div>'
121
+ end
122
+ end
123
+
124
+ specify "render with struct" do
125
+ t = Amrita2::Template.new('<div><a am:src="aaa|Attr[:href=>:url, :body=>:text]">aaa</a></div>')
126
+ s = Struct.new(:url, :text)
127
+ data = s.new("http://amrita2.rubyforge.org/", "Amrita")
128
+ t.test_with(:aaa => data) do |r|
129
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/">Amrita</a></div>'
130
+ end
131
+ end
132
+
133
+ specify "static and dynamic attribute" do
134
+ t = Amrita2::Template.new('<div><a class="cls1" am:src="aaa|Attr[:href, :body]">aaa</a></div>')
135
+ t.render_with(:aaa => {
136
+ :href=>"http://amrita2.rubyforge.org/",
137
+ :body=>"Amrita"
138
+ }
139
+ ).should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/" class="cls1">Amrita</a></div>'
140
+ end
141
+
142
+ specify "render with binding" do
143
+ text = '<div><a class="cls1" am:filter="Attr[:body=>true, :href=>:url1]">aaa</a></div>'
144
+ t = Amrita2::Template.new(text, :inline_ruby)
145
+ @url1="http://amrita2.rubyforge.org/"
146
+ @body="Amrita"
147
+ t.test_with(binding) do |r|
148
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/" class="cls1">Amrita</a></div>'
149
+ end
150
+ end
151
+
152
+ specify "render with binding and local variables" do
153
+ text = '<div><a class="cls1" am:filter="Attr[:body=>true, :href=>:url1]">aaa</a></div>'
154
+ t = Amrita2::Template.new(text, :inline_ruby)
155
+ url1="http://amrita2.rubyforge.org/"
156
+ body="Amrita"
157
+ t.test_with(binding) do |r|
158
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/" class="cls1">Amrita</a></div>'
159
+ end
160
+ end
161
+
162
+ specify "pickup" do
163
+ #t = Amrita2::Template.new('<div><a am:src="aaa|NVarForAttr[:url]" href="$1"><span am:src="body" /></a></div>')
164
+ t = Amrita2::Template.new <<-END
165
+ <<div<
166
+ <<a href="$1" :aaa|NVarForAttr[:url] <
167
+ <<:body>>
168
+ END
169
+ t.test_with(:aaa => {
170
+ :url=>"http://amrita2.rubyforge.org/",
171
+ :body=>"Amrita"
172
+ }
173
+ ) do |r|
174
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/">Amrita</a></div>'
175
+ end
176
+ end
177
+
178
+ specify "pickup with erb" do
179
+ #t = Amrita2::Template.new('<div><a am:src="aaa|NVarForAttr[:url]" href="$1"><%= $_[:body] %></a></div>')
180
+ t = Amrita2::Template.new <<-END
181
+ <<div<
182
+ <<a href="$1" :aaa|NVarForAttr[:url] <
183
+ %= $_[:body]
184
+ END
185
+ t.test_with(:aaa => {
186
+ :url=>"http://amrita2.rubyforge.org/",
187
+ :body=>"Amrita"
188
+ }
189
+ ) do |r|
190
+ r.should_be_samexml_as '<div><a href="http://amrita2.rubyforge.org/">Amrita</a></div>'
191
+ end
192
+ end
193
+
194
+ specify "Nvar with $n" do
195
+ t = Amrita2::Template.new('<a am:filter="NVar[:aaa]" href="$1" class="$$1">$$2</a>')
196
+ t.test_with(:aaa => 'aaa') do |r|
197
+ r.should_be_samexml_as "<a href='aaa' class='$1'>$2</a>"
198
+ end
199
+ end
200
+ end
201
+
@@ -0,0 +1,231 @@
1
+ require 'rexml/document'
2
+ require 'rexml/element'
3
+ require 'amrita2'
4
+ require 'amrita2/testsupport'
5
+
6
+ include Amrita2
7
+ include Amrita2::Filters
8
+ include Amrita2::Runtime
9
+
10
+ context "DictionaryData" do
11
+ setup do
12
+ @t = Amrita2::Template.new('<span am:src="aaa" />')
13
+ end
14
+
15
+ specify "Hash" do
16
+ result = "Amrita2"
17
+ @t.render_with(:aaa=>'Amrita2').should_be_samexml_as(result)
18
+ end
19
+ specify "Struct" do
20
+ result = "Amrita2"
21
+ @t.render_with(Struct.new(:aaa).new('Amrita2')).should_be_samexml_as(result)
22
+ end
23
+ specify "Binding is a DictionaryData too" do
24
+ aaa = 'Amrita2'
25
+ @t.render_with(binding).should_be_samexml_as('Amrita2')
26
+ end
27
+
28
+ specify "Binding and instance variable" do
29
+ @aaa = 'Amrita2'
30
+ @t.render_with(binding).should_be_samexml_as('Amrita2')
31
+ end
32
+
33
+ specify "Binding checks local first" do
34
+ aaa = 'Amrita2'
35
+ @aaa = 'Amrita1'
36
+ @t.render_with(binding).should_be_samexml_as('Amrita2')
37
+ end
38
+
39
+ specify "Binding and a method" do
40
+ def self.aaa; 'Amrita2';end;
41
+ @t.render_with(binding).should_be_samexml_as('Amrita2')
42
+ end
43
+ end
44
+
45
+ context "NullObject" do
46
+ specify "nil for Scalar Context" do
47
+ t = Amrita2::Template.new('<div am:src="aaa" />')
48
+ t.test_with(nil) do|r|
49
+ r.should_be_samexml_as "<div />"
50
+ end
51
+ t.test_with(:aaa=>nil) do|r|
52
+ r.should_be_samexml_as "<div />"
53
+ end
54
+ end
55
+
56
+ specify "false for Scalar Context" do
57
+ t = Amrita2::Template.new('<div am:src="aaa" />')
58
+ proc { t.render_with(false)}.should raise_error
59
+ proc { t.render_with(:aaa=>false)}.should raise_error
60
+ end
61
+
62
+ specify "nil for Dictionary Context" do
63
+ t = Amrita2::Template.new('<div class="outer" am:src="aaa"><div class="inner" am:src="bbb" /></div>')
64
+ t.test_with(nil) do|r|
65
+ r.should_be_samexml_as '<div class = "outer"><div class = "inner"></div></div>'
66
+ end
67
+ t.test_with(:aaa=>nil) do|r|
68
+ r.should_be_samexml_as '<div class = "outer"><div class = "inner"></div></div>'
69
+ end
70
+ proc { t.render_with(false)}.should raise_error
71
+ proc { t.render_with(:aaa=>false)}.should raise_error
72
+ end
73
+
74
+ specify "AcceptData[nil]" do
75
+ t = Amrita2::Template.new('<div am:src="aaa|AcceptData[nil]" />')
76
+ t.test_with(:aaa=>nil) do|r|
77
+ r.should == ''
78
+ end
79
+ end
80
+
81
+ specify "AcceptData[nil]" do
82
+ t = Amrita2::Template.new('<div class="outer" am:src="aaa|AcceptData[nil]"><div class="inner" am:src="bbb" /></div>')
83
+ data = {
84
+ :aaa => [
85
+ { :bbb=>'1'},
86
+ nil,
87
+ { :bbb=>'2'},
88
+ ]
89
+ }
90
+ t.test_with(data) do|r|
91
+ r.should == '<div class = "outer"><div class = "inner">1</div></div><div class = "outer"><div class = "inner">2</div></div>'
92
+ end
93
+ end
94
+ end
95
+
96
+ context "True" do
97
+ specify "don't accept true for default" do
98
+ t = Amrita2::Template.new('<div am:src="aaa" />')
99
+ proc { t.render_with(true) }.should raise_error
100
+ end
101
+
102
+ specify "true for Scalar Context" do
103
+ t = Amrita2::Template.new('<div am:src="aaa|AcceptData[true]" />')
104
+ t.test_with(nil) do|r|
105
+ r.should_be_samexml_as "<div />"
106
+ end
107
+ end
108
+
109
+ specify "true for Dictionary Context" do
110
+ t = Amrita2::Template.new('<div class="outer" am:src="aaa|AcceptData[true]"><div class="inner" am:src="bbb" /></div>')
111
+ t.test_with(:aaa=>true) do|r|
112
+ r.should_be_samexml_as '<div class = "outer"><div class = "inner"></div></div>'
113
+ end
114
+ t.test_with(:aaa=>{ :bbb=>1 }) do|r|
115
+ r.should_be_samexml_as '<div class = "outer"><div class = "inner">1</div></div>'
116
+ end
117
+ t.test_with(:aaa=>nil) do|r|
118
+ r.should_be_samexml_as '<div class = "outer"><div class = "inner"></div></div>'
119
+ end
120
+ proc { t.render_with(:aaa=>false)}.should raise_error
121
+ end
122
+ end
123
+
124
+ # Hook
125
+ context "HookObject" do
126
+ specify "use stream" do
127
+ #t = Amrita2::Template.new('<div am:src="aaa|AcceptData[:hook]" />')
128
+ t = Amrita2::Template.new('<<div :aaa|AcceptData[:hook]>>')
129
+
130
+ hook = Amrita2::Core::Hook.new do
131
+ stream << "hook can write to stream direct"
132
+ render_me_with("hook can render self")
133
+ stream << "and add anything after that"
134
+ end
135
+
136
+ t.test_with(:aaa=>hook) do|r|
137
+ r.should_be_samexml_as "hook can write to stream direct<div>hook can render self</div>and add anything after that"
138
+ end
139
+ end
140
+
141
+ specify "control" do
142
+ t = Amrita2::Template.new('<span am:src="aaa|AcceptData[:hook]" />')
143
+
144
+ hook = Amrita2::Core::Hook.new do
145
+ stream << "("
146
+ [7,8,9].each do |n|
147
+ render_me_with(n)
148
+ end
149
+ stream << ")"
150
+ end
151
+
152
+ t.test_with(:aaa=>hook) do|r|
153
+ r.should_be_samexml_as "(789)"
154
+ end
155
+ end
156
+
157
+ specify "hook in erb" do
158
+ t = Amrita2::Template.new <<-END
159
+ <ul>
160
+ <%
161
+ hook_in_erb = Amrita2::Core::Hook.new do
162
+ [3, 4, 5].each do |n|
163
+ render_me_with(n)
164
+ end
165
+ end
166
+ %>
167
+ <li am:src="hook_in_erb|AcceptData[:hook]" />
168
+ </ul>
169
+ END
170
+
171
+ t.test_with(binding) do |r|
172
+ r.should_be_samexml_as "<ul><li>3</li><li>4</li><li>5</li></ul>"
173
+ end
174
+ end
175
+
176
+ specify "render child" do
177
+ t = Amrita2::Template.new <<-END
178
+ <%
179
+ odd_even = Amrita2::Core::Hook.new do
180
+ list.each_with_index do |item, n|
181
+ if (item % 2) == 0
182
+ render_child(:even, :item => item )
183
+ else
184
+ render_child(:odd, :item => item )
185
+ end
186
+ stream << " and " if n < list.size - 1
187
+ end
188
+ end
189
+ %>
190
+ <span am:src="odd_even|AcceptData[:hook]">
191
+ <span am:src="odd"><span am:src="item" /> is odd</span>
192
+ <span am:src="even"><span am:src="item" /> is even</span>
193
+ </span>
194
+ END
195
+
196
+ list=[4, 1, 7, 8]
197
+ t.test_with(binding) do |r|
198
+ r.strip.should == "4 is even and 1 is odd and 7 is odd and 8 is even"
199
+ end
200
+
201
+ list=[1,2,3]
202
+ t.test_with(binding) do |r|
203
+ r.strip.should_be_samexml_as "1 is odd and 2 is even and 3 is odd"
204
+ end
205
+
206
+ end
207
+
208
+ specify "use element as string" do
209
+ t = Amrita2::Template.new('<div am:src="aaa|AcceptData[:hook]"><a href="http://amrita2.rubyforge.org">Amrita2</a></div>')
210
+
211
+ hook = Amrita2::Core::Hook.new do
212
+ stream << "[[#{element_s}]]"
213
+ end
214
+
215
+ t.test_with(:aaa=>hook) do|r|
216
+ r.should_be_samexml_as "[[<div><a href='http://amrita2.rubyforge.org'>Amrita2</a></div>]]"
217
+ end
218
+ end
219
+
220
+ specify "use element " do
221
+ t = Amrita2::Template.new('<div am:src="aaa|AcceptData[:hook]"><a href="http://amrita2.rubyforge.org">Amrita2</a></div>')
222
+
223
+ hook = Amrita2::Core::Hook.new do
224
+ stream << "[[#{element_s }]]"
225
+ end
226
+
227
+ t.test_with(:aaa=>hook) do|r|
228
+ r.should_be_samexml_as "[[<div><a href='http://amrita2.rubyforge.org'>Amrita2</a></div>]]"
229
+ end
230
+ end
231
+ end
@@ -0,0 +1,68 @@
1
+
2
+ require 'rexml/document'
3
+ require 'amrita2'
4
+ require 'amrita2/testsupport'
5
+
6
+ include Amrita2
7
+
8
+ context "Dictionary Data" do
9
+ setup do
10
+ @t = Amrita2::Template.new('<div><span am:src="aaa" /></div>')
11
+ end
12
+
13
+ specify "Struct" do
14
+ s = Struct.new(:aaa, :bbb)
15
+ data = s.new(123, 456)
16
+ result = "<div>123</div>"
17
+ @t.render_with(data).should_be_samexml_as(result)
18
+ end
19
+
20
+ class MyData
21
+ include Amrita2::DictionaryData
22
+
23
+ def aaa
24
+ "MyData"
25
+ end
26
+ end
27
+
28
+ specify "Struct" do
29
+ data = MyData.new
30
+ result = "<div>MyData</div>"
31
+ @t.render_with(data).should_be_samexml_as(result)
32
+ end
33
+
34
+ specify "Array of Struct" do
35
+ t = Amrita2::Template.new('<div><span am:src="xxx"><span am:src="aaa"/></span></div>')
36
+ data = {
37
+ :xxx=> [MyData.new, MyData.new]
38
+ }
39
+ t.test_with(data) do |result|
40
+ result.should_be_samexml_as "<div>MyDataMyData</div>"
41
+ end
42
+ end
43
+
44
+ specify "Binding" do
45
+ @aaa = 'Binding'
46
+ @t.test_with(binding) do |result|
47
+ result.should_be_samexml_as "<div>Binding</div>"
48
+ end
49
+ end
50
+
51
+ specify "Binding for local variables" do
52
+ aaa = 'Binding'
53
+ @t.test_with(binding) do |result|
54
+ result.should_be_samexml_as "<div>Binding</div>"
55
+ end
56
+ end
57
+
58
+ specify "mix" do
59
+ t = Amrita2::Template.new('<div><span am:src="xxx"><span am:src="yyy"><span am:src="aaa"/></span></span></div>')
60
+ @xxx = {
61
+ :yyy=> MyData.new
62
+ }
63
+ t.test_with(binding) do |result|
64
+ result.should_be_samexml_as "<div>MyData</div>"
65
+ end
66
+ end
67
+ end
68
+