radius 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,321 @@
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
@@ -8,7 +8,9 @@ module RadiusTestHelper
8
8
  Radius::Context.new do |c|
9
9
  c.define_tag("reverse" ) { |tag| tag.expand.reverse }
10
10
  c.define_tag("capitalize") { |tag| tag.expand.upcase }
11
- c.define_tag("attr") { |tag| tag.attr.inspect }
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}]" }
12
14
  end
13
15
  end
14
16
 
@@ -130,10 +132,18 @@ class RadiusParserTest < Test::Unit::TestCase
130
132
  assert_parse_output angle, %{<r:attr angle=">"></r:attr>}
131
133
  end
132
134
 
135
+ def test_parse_quotes
136
+ assert_parse_output "test []", %{<r:echo value="test" /> <r:wrap attr="test"></r:wrap>}
137
+ end
138
+
133
139
  def test_things_that_should_be_left_alone
134
- # The parser currently fails on these assertions
135
- #assert_parsed_either_way_is_unchanged %{ test="2"="4" }
136
- #assert_parsed_either_way_is_unchanged %{="2" }
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
137
147
  end
138
148
 
139
149
  def test_parse_result_is_always_a_string
@@ -167,13 +177,10 @@ class RadiusParserTest < Test::Unit::TestCase
167
177
  assert_parse_output "extra * parent * child * nesting", "<r:extra:parent><r:child:nesting /></r:extra:parent>"
168
178
  assert_raises(Radius::UndefinedTagError) { @parser.parse("<r:child />") }
169
179
  end
170
-
171
180
  def test_parse_tag_nesting_2
172
181
  define_tag("parent", :for => '')
173
182
  define_tag("parent:child", :for => '')
174
- define_tag "content" do |tag|
175
- tag.nesting
176
- end
183
+ define_tag("content") { |tag| tag.nesting }
177
184
  assert_parse_output 'parent:child:content', '<r:parent><r:child:content /></r:parent>'
178
185
  end
179
186
 
@@ -284,11 +291,6 @@ class RadiusParserTest < Test::Unit::TestCase
284
291
  assert_parse_output something, something
285
292
  end
286
293
 
287
- def assert_parsed_either_way_is_unchanged(middle)
288
- assert_parsed_is_unchanged %{<r:attr#{middle}/>}
289
- assert_parsed_is_unchanged %{<r:attr#{middle}></r:attr>}
290
- end
291
-
292
294
  class User
293
295
  attr_accessor :name, :age, :email, :friend
294
296
  def initialize(name, age, email)
metadata CHANGED
@@ -1,57 +1,75 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.10
2
+ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: radius
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.0
7
- date: 2006-05-01
6
+ version: 0.5.1
7
+ date: 2006-06-20 00:00:00 -04:00
8
8
  summary: Powerful tag-based template system.
9
9
  require_paths:
10
- - lib
10
+ - lib
11
11
  email:
12
12
  homepage: http://radius.rubyforge.org
13
13
  rubyforge_project: radius
14
- description: "Radius is a small, but powerful tag-based template language for Ruby similar to
15
- the ones used in MovableType and TextPattern. It has tags similar to HTML or
16
- XML, but can be used to generate any form of plain text (not just HTML)."
14
+ description: Radius is a small, but powerful tag-based template language for Ruby similar to the ones used in MovableType and TextPattern. It has tags similar to HTML or XML, but can be used to generate any form of plain text (not just HTML).
17
15
  autorequire: radius
18
16
  default_executable:
19
17
  bindir: bin
20
18
  has_rdoc: true
21
19
  required_ruby_version: !ruby/object:Gem::Version::Requirement
22
20
  requirements:
23
- -
24
- - ">"
25
- - !ruby/object:Gem::Version
26
- version: 0.0.0
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
27
24
  version:
28
25
  platform: ruby
26
+ signing_key:
27
+ cert_chain:
29
28
  authors: []
29
+
30
30
  files:
31
- - CHANGELOG
32
- - lib
33
- - pkg
34
- - QUICKSTART
35
- - Rakefile
36
- - README
37
- - ROADMAP
38
- - test
39
- - lib/radius.rb
40
- - test/radius_test.rb
31
+ - CHANGELOG
32
+ - lib
33
+ - pkg
34
+ - QUICKSTART
35
+ - Rakefile
36
+ - README
37
+ - ROADMAP
38
+ - test
39
+ - lib/radius.rb
40
+ - pkg/radius-0.5.0
41
+ - pkg/radius-0.5.0.gem
42
+ - pkg/radius-0.5.0.tgz
43
+ - pkg/radius-0.5.0.zip
44
+ - pkg/radius-0.5.0/CHANGELOG
45
+ - pkg/radius-0.5.0/lib
46
+ - pkg/radius-0.5.0/pkg
47
+ - pkg/radius-0.5.0/QUICKSTART
48
+ - pkg/radius-0.5.0/Rakefile
49
+ - pkg/radius-0.5.0/README
50
+ - pkg/radius-0.5.0/ROADMAP
51
+ - pkg/radius-0.5.0/test
52
+ - pkg/radius-0.5.0/lib/radius.rb
53
+ - pkg/radius-0.5.0/test/radius_test.rb
54
+ - test/radius_test.rb
41
55
  test_files: []
56
+
42
57
  rdoc_options:
43
- - "--title"
44
- - "Radius -- Powerful Tag-Based Templates"
45
- - "--line-numbers"
46
- - "--main"
47
- - README
58
+ - --title
59
+ - Radius -- Powerful Tag-Based Templates
60
+ - --line-numbers
61
+ - --main
62
+ - README
48
63
  extra_rdoc_files:
49
- - README
50
- - QUICKSTART
51
- - ROADMAP
52
- - CHANGELOG
64
+ - README
65
+ - QUICKSTART
66
+ - ROADMAP
67
+ - CHANGELOG
53
68
  executables: []
69
+
54
70
  extensions: []
71
+
55
72
  requirements:
56
- - none
57
- dependencies: []
73
+ - none
74
+ dependencies: []
75
+