radius 0.5.1 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,321 +0,0 @@
1
- require 'test/unit'
2
- require 'radius'
3
-
4
- module RadiusTestHelper
5
- class TestContext < Radius::Context; end
6
-
7
- def new_context
8
- Radius::Context.new do |c|
9
- c.define_tag("reverse" ) { |tag| tag.expand.reverse }
10
- c.define_tag("capitalize") { |tag| tag.expand.upcase }
11
- c.define_tag("attr" ) { |tag| tag.attr.inspect }
12
- c.define_tag("echo" ) { |tag| tag.attr['value'] }
13
- c.define_tag("wrap" ) { |tag| "[#{tag.expand}]" }
14
- end
15
- end
16
-
17
- def define_tag(name, options = {}, &block)
18
- @context.define_tag name, options, &block
19
- end
20
- end
21
-
22
- class RadiusContextTest < Test::Unit::TestCase
23
- include RadiusTestHelper
24
-
25
- def setup
26
- @context = new_context
27
- end
28
-
29
- def test_initialize
30
- @context = Radius::Context.new
31
- end
32
-
33
- def test_initialize_with_block
34
- @context = Radius::Context.new do |c|
35
- assert_kind_of Radius::Context, c
36
- c.define_tag('test') { 'just a test' }
37
- end
38
- assert_not_equal Hash.new, @context.definitions
39
- end
40
-
41
- def test_with
42
- got = @context.with do |c|
43
- assert_equal @context, c
44
- end
45
- assert_equal @context, got
46
- end
47
-
48
- def test_render_tag
49
- define_tag "hello" do |tag|
50
- "Hello #{tag.attr['name'] || 'World'}!"
51
- end
52
- assert_render_tag_output 'Hello World!', 'hello'
53
- assert_render_tag_output 'Hello John!', 'hello', 'name' => 'John'
54
- end
55
-
56
- def test_render_tag__undefined_tag
57
- e = assert_raises(Radius::UndefinedTagError) { @context.render_tag('undefined_tag') }
58
- assert_equal "undefined tag `undefined_tag'", e.message
59
- end
60
-
61
- def test_tag_missing
62
- class << @context
63
- def tag_missing(tag, attr, &block)
64
- "undefined tag `#{tag}' with attributes #{attr.inspect}"
65
- end
66
- end
67
-
68
- text = ''
69
- expected = %{undefined tag `undefined_tag' with attributes {"cool"=>"beans"}}
70
- assert_nothing_raised { text = @context.render_tag('undefined_tag', 'cool' => 'beans') }
71
- assert_equal expected, text
72
- end
73
-
74
- private
75
-
76
- def assert_render_tag_output(output, *render_tag_params)
77
- assert_equal output, @context.render_tag(*render_tag_params)
78
- end
79
-
80
- end
81
-
82
- class RadiusParserTest < Test::Unit::TestCase
83
- include RadiusTestHelper
84
-
85
- def setup
86
- @context = new_context
87
- @parser = Radius::Parser.new(@context, :tag_prefix => 'r')
88
- end
89
-
90
- def test_initialize
91
- @parser = Radius::Parser.new
92
- assert_kind_of Radius::Context, @parser.context
93
- end
94
-
95
- def test_initialize_with_params
96
- @parser = Radius::Parser.new(TestContext.new)
97
- assert_kind_of TestContext, @parser.context
98
-
99
- @parser = Radius::Parser.new(:context => TestContext.new)
100
- assert_kind_of TestContext, @parser.context
101
-
102
- @parser = Radius::Parser.new('context' => TestContext.new)
103
- assert_kind_of TestContext, @parser.context
104
-
105
- @parser = Radius::Parser.new(:tag_prefix => 'r')
106
- assert_kind_of Radius::Context, @parser.context
107
- assert_equal 'r', @parser.tag_prefix
108
-
109
- @parser = Radius::Parser.new(TestContext.new, :tag_prefix => 'r')
110
- assert_kind_of TestContext, @parser.context
111
- assert_equal 'r', @parser.tag_prefix
112
- end
113
-
114
- def test_parse_individual_tags_and_parameters
115
- define_tag "add" do |tag|
116
- tag.attr["param1"].to_i + tag.attr["param2"].to_i
117
- end
118
- assert_parse_output "<3>", %{<<r:add param1="1" param2='2'/>>}
119
- end
120
-
121
- def test_parse_attributes
122
- attributes = %{{"a"=>"1", "b"=>"2", "c"=>"3", "d"=>"'"}}
123
- assert_parse_output attributes, %{<r:attr a="1" b='2'c="3"d="'" />}
124
- assert_parse_output attributes, %{<r:attr a="1" b='2'c="3"d="'"></r:attr>}
125
- end
126
-
127
- def test_parse_attributes_with_slashes_or_angle_brackets
128
- slash = %{{"slash"=>"/"}}
129
- angle = %{{"angle"=>">"}}
130
- assert_parse_output slash, %{<r:attr slash="/"></r:attr>}
131
- assert_parse_output slash, %{<r:attr slash="/"><r:attr /></r:attr>}
132
- assert_parse_output angle, %{<r:attr angle=">"></r:attr>}
133
- end
134
-
135
- def test_parse_quotes
136
- assert_parse_output "test []", %{<r:echo value="test" /> <r:wrap attr="test"></r:wrap>}
137
- end
138
-
139
- def test_things_that_should_be_left_alone
140
- [
141
- %{ test="2"="4" },
142
- %{="2" }
143
- ].each do |middle|
144
- assert_parsed_is_unchanged "<r:attr#{middle}/>"
145
- assert_parsed_is_unchanged "<r:attr#{middle}>"
146
- end
147
- end
148
-
149
- def test_parse_result_is_always_a_string
150
- define_tag("twelve") { 12 }
151
- assert_parse_output "12", "<r:twelve />"
152
- end
153
-
154
- def test_parse_double_tags
155
- assert_parse_output "test".reverse, "<r:reverse>test</r:reverse>"
156
- assert_parse_output "tset TEST", "<r:reverse>test</r:reverse> <r:capitalize>test</r:capitalize>"
157
- end
158
-
159
- def test_parse_tag_nesting
160
- define_tag("parent", :for => '')
161
- define_tag("parent:child", :for => '')
162
- define_tag("extra", :for => '')
163
- define_tag("nesting") { |tag| tag.nesting }
164
- define_tag("extra:nesting") { |tag| tag.nesting.gsub(':', ' > ') }
165
- define_tag("parent:child:nesting") { |tag| tag.nesting.gsub(':', ' * ') }
166
- assert_parse_output "nesting", "<r:nesting />"
167
- assert_parse_output "parent:nesting", "<r:parent:nesting />"
168
- assert_parse_output "extra > nesting", "<r:extra:nesting />"
169
- assert_parse_output "parent * child * nesting", "<r:parent:child:nesting />"
170
- assert_parse_output "parent > extra > nesting", "<r:parent:extra:nesting />"
171
- assert_parse_output "parent > child > extra > nesting", "<r:parent:child:extra:nesting />"
172
- assert_parse_output "parent * extra * child * nesting", "<r:parent:extra:child:nesting />"
173
- assert_parse_output "parent > extra > child > extra > nesting", "<r:parent:extra:child:extra:nesting />"
174
- assert_parse_output "parent > extra > child > extra > nesting", "<r:parent><r:extra><r:child><r:extra><r:nesting /></r:extra></r:child></r:extra></r:parent>"
175
- assert_parse_output "extra * parent * child * nesting", "<r:extra:parent:child:nesting />"
176
- assert_parse_output "extra > parent > nesting", "<r:extra><r:parent:nesting /></r:extra>"
177
- assert_parse_output "extra * parent * child * nesting", "<r:extra:parent><r:child:nesting /></r:extra:parent>"
178
- assert_raises(Radius::UndefinedTagError) { @parser.parse("<r:child />") }
179
- end
180
- def test_parse_tag_nesting_2
181
- define_tag("parent", :for => '')
182
- define_tag("parent:child", :for => '')
183
- define_tag("content") { |tag| tag.nesting }
184
- assert_parse_output 'parent:child:content', '<r:parent><r:child:content /></r:parent>'
185
- end
186
-
187
- def test_parse_tag__binding_do_missing
188
- define_tag 'test' do |tag|
189
- tag.missing!
190
- end
191
- e = assert_raises(Radius::UndefinedTagError) { @parser.parse("<r:test />") }
192
- assert_equal "undefined tag `test'", e.message
193
- end
194
-
195
- def test_parse_tag__binding_render_tag
196
- define_tag('test') { |tag| "Hello #{tag.attr['name']}!" }
197
- define_tag('hello') { |tag| tag.render('test', tag.attr) }
198
- assert_parse_output 'Hello John!', '<r:hello name="John" />'
199
- end
200
- def test_parse_tag__binding_render_tag_with_block
201
- define_tag('test') { |tag| "Hello #{tag.expand}!" }
202
- define_tag('hello') { |tag| tag.render('test') { tag.expand } }
203
- assert_parse_output 'Hello John!', '<r:hello>John</r:hello>'
204
- end
205
-
206
- def test_tag_locals
207
- define_tag "outer" do |tag|
208
- tag.locals.var = 'outer'
209
- tag.expand
210
- end
211
- define_tag "outer:inner" do |tag|
212
- tag.locals.var = 'inner'
213
- tag.expand
214
- end
215
- define_tag "outer:var" do |tag|
216
- tag.locals.var
217
- end
218
- assert_parse_output 'outer', "<r:outer><r:var /></r:outer>"
219
- assert_parse_output 'outer:inner:outer', "<r:outer><r:var />:<r:inner><r:var /></r:inner>:<r:var /></r:outer>"
220
- assert_parse_output 'outer:inner:outer:inner:outer', "<r:outer><r:var />:<r:inner><r:var />:<r:outer><r:var /></r:outer>:<r:var /></r:inner>:<r:var /></r:outer>"
221
- assert_parse_output 'outer', "<r:outer:var />"
222
- end
223
-
224
- def test_tag_globals
225
- define_tag "set" do |tag|
226
- tag.globals.var = tag.attr['value']
227
- ''
228
- end
229
- define_tag "var" do |tag|
230
- tag.globals.var
231
- end
232
- assert_parse_output " true false", %{<r:var /> <r:set value="true" /> <r:var /> <r:set value="false" /> <r:var />}
233
- end
234
-
235
- def test_parse_loops
236
- @item = nil
237
- define_tag "each" do |tag|
238
- result = []
239
- ["Larry", "Moe", "Curly"].each do |item|
240
- tag.locals.item = item
241
- result << tag.expand
242
- end
243
- result.join(tag.attr["between"] || "")
244
- end
245
- define_tag "each:item" do |tag|
246
- tag.locals.item
247
- end
248
- assert_parse_output %{Three Stooges: "Larry", "Moe", "Curly"}, %{Three Stooges: <r:each between=", ">"<r:item />"</r:each>}
249
- end
250
-
251
- def test_tag_option_for
252
- define_tag 'fun', :for => 'just for kicks'
253
- assert_parse_output 'just for kicks', '<r:fun />'
254
- end
255
-
256
- def test_tag_expose_option
257
- define_tag 'user', :for => users.first, :expose => ['name', :age]
258
- assert_parse_output 'John', '<r:user:name />'
259
- assert_parse_output '25', '<r:user><r:age /></r:user>'
260
- e = assert_raises(Radius::UndefinedTagError) { @parser.parse "<r:user:email />" }
261
- assert_equal "undefined tag `email'", e.message
262
- end
263
-
264
- def test_tag_expose_attributes_option_on_by_default
265
- define_tag 'user', :for => user_with_attributes
266
- assert_parse_output 'John', '<r:user:name />'
267
- end
268
- def test_tag_expose_attributes_set_to_false
269
- define_tag 'user_without_attributes', :for => user_with_attributes, :attributes => false
270
- assert_raises(Radius::UndefinedTagError) { @parser.parse "<r:user_without_attributes:name />" }
271
- end
272
-
273
- def test_tag_options_must_contain_a_for_option_if_methods_are_exposed
274
- e = assert_raises(ArgumentError) { define_tag('fun', :expose => :today) { 'test' } }
275
- assert_equal "tag definition must contain a :for option when used with the :expose option", e.message
276
- end
277
-
278
- def test_parse_fail_on_missing_end_tag
279
- assert_raises(Radius::MissingEndTagError) { @parser.parse("<r:reverse>") }
280
- assert_raises(Radius::MissingEndTagError) { @parser.parse("<r:reverse><r:capitalize></r:reverse>") }
281
- end
282
-
283
- protected
284
-
285
- def assert_parse_output(output, input, message = nil)
286
- r = @parser.parse(input)
287
- assert_equal(output, r, message)
288
- end
289
-
290
- def assert_parsed_is_unchanged(something)
291
- assert_parse_output something, something
292
- end
293
-
294
- class User
295
- attr_accessor :name, :age, :email, :friend
296
- def initialize(name, age, email)
297
- @name, @age, @email = name, age, email
298
- end
299
- def <=>(other)
300
- name <=> other.name
301
- end
302
- end
303
-
304
- class UserWithAttributes < User
305
- def attributes
306
- { :name => name, :age => age, :email => email }
307
- end
308
- end
309
-
310
- def users
311
- [
312
- User.new('John', 25, 'test@johnwlong.com'),
313
- User.new('James', 27, 'test@jameslong.com')
314
- ]
315
- end
316
-
317
- def user_with_attributes
318
- UserWithAttributes.new('John', 25, 'test@johnwlong.com')
319
- end
320
-
321
- end