1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
require 'abstract_unit'
module ActionDispatch
module Journey
module Path
class TestPattern < ActiveSupport::TestCase
x = /.+/
{
'/:controller(/:action)' => %r{\A/(#{x})(?:/([^/.?]+))?\Z},
'/:controller/foo' => %r{\A/(#{x})/foo\Z},
'/:controller/:action' => %r{\A/(#{x})/([^/.?]+)\Z},
'/:controller' => %r{\A/(#{x})\Z},
'/:controller(/:action(/:id))' => %r{\A/(#{x})(?:/([^/.?]+)(?:/([^/.?]+))?)?\Z},
'/:controller/:action.xml' => %r{\A/(#{x})/([^/.?]+)\.xml\Z},
'/:controller.:format' => %r{\A/(#{x})\.([^/.?]+)\Z},
'/:controller(.:format)' => %r{\A/(#{x})(?:\.([^/.?]+))?\Z},
'/:controller/*foo' => %r{\A/(#{x})/(.+)\Z},
'/:controller/*foo/bar' => %r{\A/(#{x})/(.+)/bar\Z},
}.each do |path, expected|
define_method(:"test_to_regexp_#{path}") do
strexp = Router::Strexp.new(
path,
{ :controller => /.+/ },
["/", ".", "?"]
)
path = Pattern.new strexp
assert_equal(expected, path.to_regexp)
end
end
{
'/:controller(/:action)' => %r{\A/(#{x})(?:/([^/.?]+))?},
'/:controller/foo' => %r{\A/(#{x})/foo},
'/:controller/:action' => %r{\A/(#{x})/([^/.?]+)},
'/:controller' => %r{\A/(#{x})},
'/:controller(/:action(/:id))' => %r{\A/(#{x})(?:/([^/.?]+)(?:/([^/.?]+))?)?},
'/:controller/:action.xml' => %r{\A/(#{x})/([^/.?]+)\.xml},
'/:controller.:format' => %r{\A/(#{x})\.([^/.?]+)},
'/:controller(.:format)' => %r{\A/(#{x})(?:\.([^/.?]+))?},
'/:controller/*foo' => %r{\A/(#{x})/(.+)},
'/:controller/*foo/bar' => %r{\A/(#{x})/(.+)/bar},
}.each do |path, expected|
define_method(:"test_to_non_anchored_regexp_#{path}") do
strexp = Router::Strexp.new(
path,
{ :controller => /.+/ },
["/", ".", "?"],
false
)
path = Pattern.new strexp
assert_equal(expected, path.to_regexp)
end
end
{
'/:controller(/:action)' => %w{ controller action },
'/:controller/foo' => %w{ controller },
'/:controller/:action' => %w{ controller action },
'/:controller' => %w{ controller },
'/:controller(/:action(/:id))' => %w{ controller action id },
'/:controller/:action.xml' => %w{ controller action },
'/:controller.:format' => %w{ controller format },
'/:controller(.:format)' => %w{ controller format },
'/:controller/*foo' => %w{ controller foo },
'/:controller/*foo/bar' => %w{ controller foo },
}.each do |path, expected|
define_method(:"test_names_#{path}") do
strexp = Router::Strexp.new(
path,
{ :controller => /.+/ },
["/", ".", "?"]
)
path = Pattern.new strexp
assert_equal(expected, path.names)
end
end
def test_to_raise_exception_with_bad_expression
assert_raise(ArgumentError, "Bad expression: []") { Pattern.new [] }
end
def test_to_regexp_with_extended_group
strexp = Router::Strexp.new(
'/page/:name',
{ :name => /
#ROFL
(tender|love
#MAO
)/x },
["/", ".", "?"]
)
path = Pattern.new strexp
assert_match(path, '/page/tender')
assert_match(path, '/page/love')
assert_no_match(path, '/page/loving')
end
def test_optional_names
[
['/:foo(/:bar(/:baz))', %w{ bar baz }],
['/:foo(/:bar)', %w{ bar }],
['/:foo(/:bar)/:lol(/:baz)', %w{ bar baz }],
].each do |pattern, list|
path = Pattern.new pattern
assert_equal list.sort, path.optional_names.sort
end
end
def test_to_regexp_match_non_optional
strexp = Router::Strexp.new(
'/:name',
{ :name => /\d+/ },
["/", ".", "?"]
)
path = Pattern.new strexp
assert_match(path, '/123')
assert_no_match(path, '/')
end
def test_to_regexp_with_group
strexp = Router::Strexp.new(
'/page/:name',
{ :name => /(tender|love)/ },
["/", ".", "?"]
)
path = Pattern.new strexp
assert_match(path, '/page/tender')
assert_match(path, '/page/love')
assert_no_match(path, '/page/loving')
end
def test_ast_sets_regular_expressions
requirements = { :name => /(tender|love)/, :value => /./ }
strexp = Router::Strexp.new(
'/page/:name/:value',
requirements,
["/", ".", "?"]
)
assert_equal requirements, strexp.requirements
path = Pattern.new strexp
nodes = path.ast.grep(Nodes::Symbol)
assert_equal 2, nodes.length
nodes.each do |node|
assert_equal requirements[node.to_sym], node.regexp
end
end
def test_match_data_with_group
strexp = Router::Strexp.new(
'/page/:name',
{ :name => /(tender|love)/ },
["/", ".", "?"]
)
path = Pattern.new strexp
match = path.match '/page/tender'
assert_equal 'tender', match[1]
assert_equal 2, match.length
end
def test_match_data_with_multi_group
strexp = Router::Strexp.new(
'/page/:name/:id',
{ :name => /t(((ender|love)))()/ },
["/", ".", "?"]
)
path = Pattern.new strexp
match = path.match '/page/tender/10'
assert_equal 'tender', match[1]
assert_equal '10', match[2]
assert_equal 3, match.length
assert_equal %w{ tender 10 }, match.captures
end
def test_star_with_custom_re
z = /\d+/
strexp = Router::Strexp.new(
'/page/*foo',
{ :foo => z },
["/", ".", "?"]
)
path = Pattern.new strexp
assert_equal(%r{\A/page/(#{z})\Z}, path.to_regexp)
end
def test_insensitive_regexp_with_group
strexp = Router::Strexp.new(
'/page/:name/aaron',
{ :name => /(tender|love)/i },
["/", ".", "?"]
)
path = Pattern.new strexp
assert_match(path, '/page/TENDER/aaron')
assert_match(path, '/page/loVE/aaron')
assert_no_match(path, '/page/loVE/AAron')
end
def test_to_regexp_with_strexp
strexp = Router::Strexp.new('/:controller', { }, ["/", ".", "?"])
path = Pattern.new strexp
x = %r{\A/([^/.?]+)\Z}
assert_equal(x.source, path.source)
end
def test_to_regexp_defaults
path = Pattern.new '/:controller(/:action(/:id))'
expected = %r{\A/([^/.?]+)(?:/([^/.?]+)(?:/([^/.?]+))?)?\Z}
assert_equal expected, path.to_regexp
end
def test_failed_match
path = Pattern.new '/:controller(/:action(/:id(.:format)))'
uri = 'content'
assert_not path =~ uri
end
def test_match_controller
path = Pattern.new '/:controller(/:action(/:id(.:format)))'
uri = '/content'
match = path =~ uri
assert_equal %w{ controller action id format }, match.names
assert_equal 'content', match[1]
assert_nil match[2]
assert_nil match[3]
assert_nil match[4]
end
def test_match_controller_action
path = Pattern.new '/:controller(/:action(/:id(.:format)))'
uri = '/content/list'
match = path =~ uri
assert_equal %w{ controller action id format }, match.names
assert_equal 'content', match[1]
assert_equal 'list', match[2]
assert_nil match[3]
assert_nil match[4]
end
def test_match_controller_action_id
path = Pattern.new '/:controller(/:action(/:id(.:format)))'
uri = '/content/list/10'
match = path =~ uri
assert_equal %w{ controller action id format }, match.names
assert_equal 'content', match[1]
assert_equal 'list', match[2]
assert_equal '10', match[3]
assert_nil match[4]
end
def test_match_literal
path = Path::Pattern.new "/books(/:action(.:format))"
uri = '/books'
match = path =~ uri
assert_equal %w{ action format }, match.names
assert_nil match[1]
assert_nil match[2]
end
def test_match_literal_with_action
path = Path::Pattern.new "/books(/:action(.:format))"
uri = '/books/list'
match = path =~ uri
assert_equal %w{ action format }, match.names
assert_equal 'list', match[1]
assert_nil match[2]
end
def test_match_literal_with_action_and_format
path = Path::Pattern.new "/books(/:action(.:format))"
uri = '/books/list.rss'
match = path =~ uri
assert_equal %w{ action format }, match.names
assert_equal 'list', match[1]
assert_equal 'rss', match[2]
end
end
end
end
end
|