radius19-radiant 1.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.
@@ -0,0 +1,53 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class TestDOS
4
+ attr_accessor :name
5
+ end
6
+
7
+ class RadiusDelegatingOpenStructtTest < Test::Unit::TestCase
8
+ def new_dos(obj = nil)
9
+ Radius::DelegatingOpenStruct.new obj
10
+ end
11
+
12
+ def test_initialize_wo_object
13
+ o = new_dos nil
14
+ assert_nil o.object
15
+ end
16
+
17
+ def test_initialize_w_object
18
+ x = TestDOS.new
19
+ o = new_dos x
20
+ assert_equal o.object, x
21
+ end
22
+
23
+ def test_assign_and_read_vars
24
+ o = new_dos
25
+ o.age = 15
26
+ o.height = 3.5
27
+ assert_equal o.age, 15
28
+ assert_equal o.height, 3.5
29
+ end
30
+
31
+ def test_read_object
32
+ o = new_dos TestDOS.new
33
+ o.object.name = 'Peter'
34
+ assert_equal o.name, 'Peter'
35
+ assert_equal o.object.name, 'Peter'
36
+ end
37
+
38
+ def test_read_and_assign_mixed
39
+ o = new_dos TestDOS.new
40
+ o.object.name = 'Peter'
41
+ o.age = 15
42
+ assert_equal o.name, 'Peter'
43
+ assert_equal o.object.name, 'Peter'
44
+ assert_equal o.age, 15
45
+ end
46
+
47
+ def test_raise_on_unknown_method
48
+ o = new_dos TestDOS.new
49
+ assert_raise NoMethodError do
50
+ o.age
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,242 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class RadiusParserTest < Test::Unit::TestCase
4
+ include RadiusTestHelper
5
+
6
+ def setup
7
+ @context = new_context
8
+ @parser = Radius::Parser.new(@context, :tag_prefix => 'r')
9
+ end
10
+
11
+ def test_initialize
12
+ @parser = Radius::Parser.new
13
+ assert_kind_of Radius::Context, @parser.context
14
+ end
15
+
16
+ def test_initialize_with_params
17
+ @parser = Radius::Parser.new(TestContext.new)
18
+ assert_kind_of TestContext, @parser.context
19
+
20
+ @parser = Radius::Parser.new(:context => TestContext.new)
21
+ assert_kind_of TestContext, @parser.context
22
+
23
+ @parser = Radius::Parser.new('context' => TestContext.new)
24
+ assert_kind_of TestContext, @parser.context
25
+
26
+ @parser = Radius::Parser.new(:tag_prefix => 'r')
27
+ assert_kind_of Radius::Context, @parser.context
28
+ assert_equal 'r', @parser.tag_prefix
29
+
30
+ @parser = Radius::Parser.new(TestContext.new, :tag_prefix => 'r')
31
+ assert_kind_of TestContext, @parser.context
32
+ assert_equal 'r', @parser.tag_prefix
33
+ end
34
+
35
+ def test_parse_individual_tags_and_parameters
36
+ define_tag "add" do |tag|
37
+ tag.attr["param1"].to_i + tag.attr["param2"].to_i
38
+ end
39
+ assert_parse_output "<3>", %{<<r:add param1="1" param2='2'/>>}
40
+ end
41
+
42
+ def test_parse_attributes
43
+ attributes = %{{"a"=>"1", "b"=>"2", "c"=>"3", "d"=>"'"}}
44
+ assert_parse_output attributes, %{<r:attr a="1" b='2'c="3"d="'" />}
45
+ assert_parse_output attributes, %{<r:attr a="1" b='2'c="3"d="'"></r:attr>}
46
+ end
47
+
48
+ def test_parse_attributes_with_slashes_or_angle_brackets
49
+ slash = %{{"slash"=>"/"}}
50
+ angle = %{{"angle"=>">"}}
51
+ assert_parse_output slash, %{<r:attr slash="/"></r:attr>}
52
+ assert_parse_output slash, %{<r:attr slash="/"><r:attr /></r:attr>}
53
+ assert_parse_output angle, %{<r:attr angle=">"></r:attr>}
54
+ end
55
+
56
+ def test_parse_quotes
57
+ assert_parse_output "test []", %{<r:echo value="test" /> <r:wrap attr="test"></r:wrap>}
58
+ end
59
+
60
+ def test_things_that_should_be_left_alone
61
+ [
62
+ %{ test="2"="4" },
63
+ %{="2" }
64
+ ].each do |middle|
65
+ assert_parsed_is_unchanged "<r:attr#{middle}/>"
66
+ assert_parsed_is_unchanged "<r:attr#{middle}>"
67
+ end
68
+ end
69
+
70
+ def test_parse_result_is_always_a_string
71
+ define_tag("twelve") { 12 }
72
+ assert_parse_output "12", "<r:twelve />"
73
+ end
74
+
75
+ def test_parse_double_tags
76
+ assert_parse_output "test".reverse, "<r:reverse>test</r:reverse>"
77
+ assert_parse_output "tset TEST", "<r:reverse>test</r:reverse> <r:capitalize>test</r:capitalize>"
78
+ end
79
+
80
+ def test_parse_tag_nesting
81
+ define_tag("parent", :for => '')
82
+ define_tag("parent:child", :for => '')
83
+ define_tag("extra", :for => '')
84
+ define_tag("nesting") { |tag| tag.nesting }
85
+ define_tag("extra:nesting") { |tag| tag.nesting.gsub(':', ' > ') }
86
+ define_tag("parent:child:nesting") { |tag| tag.nesting.gsub(':', ' * ') }
87
+ assert_parse_output "nesting", "<r:nesting />"
88
+ assert_parse_output "parent:nesting", "<r:parent:nesting />"
89
+ assert_parse_output "extra > nesting", "<r:extra:nesting />"
90
+ assert_parse_output "parent * child * nesting", "<r:parent:child:nesting />"
91
+ assert_parse_output "parent > extra > nesting", "<r:parent:extra:nesting />"
92
+ assert_parse_output "parent > child > extra > nesting", "<r:parent:child:extra:nesting />"
93
+ assert_parse_output "parent * extra * child * nesting", "<r:parent:extra:child:nesting />"
94
+ assert_parse_output "parent > extra > child > extra > nesting", "<r:parent:extra:child:extra:nesting />"
95
+ 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>"
96
+ assert_parse_output "extra * parent * child * nesting", "<r:extra:parent:child:nesting />"
97
+ assert_parse_output "extra > parent > nesting", "<r:extra><r:parent:nesting /></r:extra>"
98
+ assert_parse_output "extra * parent * child * nesting", "<r:extra:parent><r:child:nesting /></r:extra:parent>"
99
+ assert_raises(Radius::UndefinedTagError) { @parser.parse("<r:child />") }
100
+ end
101
+ def test_parse_tag_nesting_2
102
+ define_tag("parent", :for => '')
103
+ define_tag("parent:child", :for => '')
104
+ define_tag("content") { |tag| tag.nesting }
105
+ assert_parse_output 'parent:child:content', '<r:parent><r:child:content /></r:parent>'
106
+ end
107
+
108
+ def test_parse_tag__binding_do_missing
109
+ define_tag 'test' do |tag|
110
+ tag.missing!
111
+ end
112
+ e = assert_raises(Radius::UndefinedTagError) { @parser.parse("<r:test />") }
113
+ assert_equal "undefined tag `test'", e.message
114
+ end
115
+
116
+ def test_parse_tag__binding_render_tag
117
+ define_tag('test') { |tag| "Hello #{tag.attr['name']}!" }
118
+ define_tag('hello') { |tag| tag.render('test', tag.attr) }
119
+ assert_parse_output 'Hello John!', '<r:hello name="John" />'
120
+ end
121
+ def test_parse_tag__binding_render_tag_with_block
122
+ define_tag('test') { |tag| "Hello #{tag.expand}!" }
123
+ define_tag('hello') { |tag| tag.render('test') { tag.expand } }
124
+ assert_parse_output 'Hello John!', '<r:hello>John</r:hello>'
125
+ end
126
+
127
+ def test_tag_locals
128
+ define_tag "outer" do |tag|
129
+ tag.locals.var = 'outer'
130
+ tag.expand
131
+ end
132
+ define_tag "outer:inner" do |tag|
133
+ tag.locals.var = 'inner'
134
+ tag.expand
135
+ end
136
+ define_tag "outer:var" do |tag|
137
+ tag.locals.var
138
+ end
139
+ assert_parse_output 'outer', "<r:outer><r:var /></r:outer>"
140
+ assert_parse_output 'outer:inner:outer', "<r:outer><r:var />:<r:inner><r:var /></r:inner>:<r:var /></r:outer>"
141
+ 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>"
142
+ assert_parse_output 'outer', "<r:outer:var />"
143
+ end
144
+
145
+ def test_tag_globals
146
+ define_tag "set" do |tag|
147
+ tag.globals.var = tag.attr['value']
148
+ ''
149
+ end
150
+ define_tag "var" do |tag|
151
+ tag.globals.var
152
+ end
153
+ assert_parse_output " true false", %{<r:var /> <r:set value="true" /> <r:var /> <r:set value="false" /> <r:var />}
154
+ end
155
+
156
+ def test_parse_loops
157
+ @item = nil
158
+ define_tag "each" do |tag|
159
+ result = []
160
+ ["Larry", "Moe", "Curly"].each do |item|
161
+ tag.locals.item = item
162
+ result << tag.expand
163
+ end
164
+ result.join(tag.attr["between"] || "")
165
+ end
166
+ define_tag "each:item" do |tag|
167
+ tag.locals.item
168
+ end
169
+ assert_parse_output %{Three Stooges: "Larry", "Moe", "Curly"}, %{Three Stooges: <r:each between=", ">"<r:item />"</r:each>}
170
+ end
171
+
172
+ def test_tag_option_for
173
+ define_tag 'fun', :for => 'just for kicks'
174
+ assert_parse_output 'just for kicks', '<r:fun />'
175
+ end
176
+
177
+ def test_tag_expose_option
178
+ define_tag 'user', :for => users.first, :expose => ['name', :age]
179
+ assert_parse_output 'John', '<r:user:name />'
180
+ assert_parse_output '25', '<r:user><r:age /></r:user>'
181
+ e = assert_raises(Radius::UndefinedTagError) { @parser.parse "<r:user:email />" }
182
+ assert_equal "undefined tag `email'", e.message
183
+ end
184
+
185
+ def test_tag_expose_attributes_option_on_by_default
186
+ define_tag 'user', :for => user_with_attributes
187
+ assert_parse_output 'John', '<r:user:name />'
188
+ end
189
+ def test_tag_expose_attributes_set_to_false
190
+ define_tag 'user_without_attributes', :for => user_with_attributes, :attributes => false
191
+ assert_raises(Radius::UndefinedTagError) { @parser.parse "<r:user_without_attributes:name />" }
192
+ end
193
+
194
+ def test_tag_options_must_contain_a_for_option_if_methods_are_exposed
195
+ e = assert_raises(ArgumentError) { define_tag('fun', :expose => :today) { 'test' } }
196
+ assert_equal "tag definition must contain a :for option when used with the :expose option", e.message
197
+ end
198
+
199
+ def test_parse_fail_on_missing_end_tag
200
+ assert_raises(Radius::MissingEndTagError) { @parser.parse("<r:reverse>") }
201
+ assert_raises(Radius::MissingEndTagError) { @parser.parse("<r:reverse><r:capitalize></r:reverse>") }
202
+ end
203
+
204
+ protected
205
+
206
+ def assert_parse_output(output, input, message = nil)
207
+ r = @parser.parse(input)
208
+ assert_equal(output, r, message)
209
+ end
210
+
211
+ def assert_parsed_is_unchanged(something)
212
+ assert_parse_output something, something
213
+ end
214
+
215
+ class User
216
+ attr_accessor :name, :age, :email, :friend
217
+ def initialize(name, age, email)
218
+ @name, @age, @email = name, age, email
219
+ end
220
+ def <=>(other)
221
+ name <=> other.name
222
+ end
223
+ end
224
+
225
+ class UserWithAttributes < User
226
+ def attributes
227
+ { :name => name, :age => age, :email => email }
228
+ end
229
+ end
230
+
231
+ def users
232
+ [
233
+ User.new('John', 25, 'test@johnwlong.com'),
234
+ User.new('James', 27, 'test@jameslong.com')
235
+ ]
236
+ end
237
+
238
+ def user_with_attributes
239
+ UserWithAttributes.new('John', 25, 'test@johnwlong.com')
240
+ end
241
+
242
+ end
@@ -0,0 +1,50 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class RadiusTagBindingTest < Test::Unit::TestCase
4
+ include RadiusTestHelper
5
+
6
+ def new_tag_binding(opts = {})
7
+ opts = {
8
+ :cnx => new_context,
9
+ :locals => Radius::DelegatingOpenStruct.new,
10
+ :name => :test,
11
+ :attrs => {},
12
+ :blk => Proc.new { 'hello!'}
13
+ }.update(opts)
14
+ Radius::TagBinding.new opts[:cnx], opts[:locals], opts[:name], opts[:attrs], opts[:blk]
15
+ end
16
+
17
+ def test_initialize
18
+ nc = new_context
19
+ ls = Radius::DelegatingOpenStruct.new
20
+ nm = :xyz
21
+ as = { :a => 1, :b => 2 }
22
+ bk = Proc.new { 'hello!'}
23
+ tb = new_tag_binding :cnx => nc, :locals => ls, :name => nm, :attrs => as, :blk => bk
24
+ assert_equal tb.context, nc
25
+ assert_equal tb.locals, ls
26
+ assert_equal tb.name, nm
27
+ assert_equal tb.attributes, as
28
+ assert_equal tb.attr, as
29
+ assert_equal tb.block, bk
30
+ end
31
+
32
+ def test_expand
33
+ tb = new_tag_binding
34
+ assert_equal tb.expand, 'hello!'
35
+ end
36
+
37
+ def test_single_double
38
+ tb1 = new_tag_binding :blk => Proc.new{ 'hello!'}
39
+ assert_equal tb1.single?, false
40
+ assert_equal tb1.double?, true
41
+ tb2 = new_tag_binding :blk => nil
42
+ assert_equal tb2.single?, true
43
+ assert_equal tb2.double?, false
44
+ end
45
+
46
+ def test_globals
47
+ tb = new_tag_binding
48
+ assert_equal tb.globals, tb.context.globals
49
+ end
50
+ end
@@ -0,0 +1,61 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ARLike < Radius::DelegatingOpenStruct
4
+ def attributes
5
+ @hash
6
+ end
7
+ end
8
+
9
+ class RadiusTagDefinitionsTest < Test::Unit::TestCase
10
+ include RadiusTestHelper
11
+
12
+ def new_tag_factory
13
+ Radius::TagDefinitions::TagFactory.new new_context
14
+ end
15
+
16
+ def test_initialize
17
+ new_tag_factory
18
+ end
19
+
20
+ def test_prepare_options_w_for
21
+ tf = new_tag_factory
22
+ ar = ARLike.new
23
+ ar.x, ar.y, ar.z = 1, 2, 3
24
+ res = tf.send :prepare_options, :test, { 'for' => ar, 'other' => 1, 'more' => 'mas' }
25
+ assert_equal res.keys.size, 5
26
+ assert_equal res[:for], ar
27
+ assert_equal res[:expose].size, 3
28
+ assert res[:expose].include?(:x)
29
+ assert res[:expose].include?(:y)
30
+ assert res[:expose].include?(:z)
31
+ assert_equal res[:attributes], true
32
+ assert_equal res[:other], 1
33
+ assert_equal res[:more], 'mas'
34
+ end
35
+
36
+ def test_prepare_options_wo_for
37
+ tf = new_tag_factory
38
+ res = tf.send :prepare_options, :test, { 'other' => 1, 'more' => 'mas' }
39
+ assert_equal res.keys.size, 4
40
+ assert_equal res[:expose], []
41
+ assert_equal res[:attributes], false
42
+ assert_equal res[:other], 1
43
+ assert_equal res[:more], 'mas'
44
+ end
45
+
46
+ def test_validate_params
47
+ tf = new_tag_factory
48
+ assert_raise(ArgumentError) { tf.send :validate_params, :echo, {} }
49
+ assert_raise(ArgumentError) { tf.send :validate_params, :echo, { :expose => [:x]} }
50
+ end
51
+
52
+ def test_expand_array_option
53
+ tf = new_tag_factory
54
+ assert_equal tf.send(:expand_array_option, ['a', 'b', nil, 'c']), [:a, :b, :c]
55
+ end
56
+
57
+ def test_last_part
58
+ tf = new_tag_factory
59
+ assert_equal tf.send(:last_part, 'a:b:c'), 'c'
60
+ end
61
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/radius19'
4
+
5
+ class Test::Unit::TestCase
6
+ end
7
+
8
+ module RadiusTestHelper
9
+ class TestContext < Radius::Context; end
10
+
11
+ def new_context
12
+ Radius::Context.new do |c|
13
+ c.define_tag("reverse" ) { |tag| tag.expand.reverse }
14
+ c.define_tag("capitalize") { |tag| tag.expand.upcase }
15
+ c.define_tag("attr" ) { |tag| tag.attr.inspect }
16
+ c.define_tag("echo" ) { |tag| tag.attr['value'] }
17
+ c.define_tag("wrap" ) { |tag| "[#{tag.expand}]" }
18
+ end
19
+ end
20
+
21
+ def define_tag(name, options = {}, &block)
22
+ @context.define_tag name, options, &block
23
+ end
24
+ end
25
+
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class RadiusUtilTest < Test::Unit::TestCase
4
+
5
+ def test_symbolize_keys
6
+ h = Radius::Util.symbolize_keys({ 'a' => 1, :b => 2 })
7
+ assert_equal h[:a], 1
8
+ assert_equal h[:b], 2
9
+ end
10
+
11
+ def test_impartial_hash_delete
12
+ h = { 'a' => 1, :b => 2 }
13
+ assert_equal Radius::Util.impartial_hash_delete(h, :a), 1
14
+ assert_equal Radius::Util.impartial_hash_delete(h, 'b'), 2
15
+ assert_equal h.empty?, true
16
+ end
17
+
18
+ def test_constantize
19
+ assert_equal Radius::Util.constantize('String'), String
20
+ end
21
+
22
+ def test_camelize
23
+ assert_equal Radius::Util.camelize('ab_cd_ef'), 'AbCdEf'
24
+ end
25
+ end