mustermann 0.1.0 → 0.4.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.
- checksums.yaml +4 -4
- data/README.md +472 -321
- data/bench/capturing.rb +4 -4
- data/bench/regexp.rb +21 -0
- data/lib/mustermann/ast/boundaries.rb +44 -0
- data/lib/mustermann/ast/compiler.rb +13 -7
- data/lib/mustermann/ast/expander.rb +45 -14
- data/lib/mustermann/ast/node.rb +69 -6
- data/lib/mustermann/ast/param_scanner.rb +20 -0
- data/lib/mustermann/ast/parser.rb +140 -22
- data/lib/mustermann/ast/pattern.rb +68 -21
- data/lib/mustermann/ast/template_generator.rb +28 -0
- data/lib/mustermann/ast/transformer.rb +32 -24
- data/lib/mustermann/ast/translator.rb +24 -10
- data/lib/mustermann/ast/validation.rb +10 -6
- data/lib/mustermann/caster.rb +116 -0
- data/lib/mustermann/composite.rb +101 -0
- data/lib/mustermann/expander.rb +204 -0
- data/lib/mustermann/identity.rb +56 -0
- data/lib/mustermann/mapper.rb +94 -0
- data/lib/mustermann/pattern.rb +193 -13
- data/lib/mustermann/pattern_cache.rb +49 -0
- data/lib/mustermann/regexp.rb +1 -0
- data/lib/mustermann/regexp_based.rb +19 -2
- data/lib/mustermann/regular.rb +29 -0
- data/lib/mustermann/router/rack.rb +47 -0
- data/lib/mustermann/router/simple.rb +142 -0
- data/lib/mustermann/router.rb +9 -0
- data/lib/mustermann/simple_match.rb +5 -0
- data/lib/mustermann/sinatra.rb +22 -5
- data/lib/mustermann/to_pattern.rb +50 -0
- data/lib/mustermann/version.rb +1 -1
- data/lib/mustermann.rb +102 -19
- data/mustermann.gemspec +2 -12
- data/spec/ast_spec.rb +14 -0
- data/spec/composite_spec.rb +147 -0
- data/spec/expander_spec.rb +120 -0
- data/spec/identity_spec.rb +44 -0
- data/spec/mapper_spec.rb +83 -0
- data/spec/mustermann_spec.rb +50 -11
- data/spec/pattern_spec.rb +34 -7
- data/spec/regexp_based_spec.rb +1 -1
- data/spec/regular_spec.rb +61 -0
- data/spec/router/rack_spec.rb +39 -0
- data/spec/router/simple_spec.rb +32 -0
- data/spec/simple_match_spec.rb +1 -1
- data/spec/sinatra_spec.rb +197 -9
- data/spec/to_pattern_spec.rb +69 -0
- metadata +44 -146
- data/.gitignore +0 -18
- data/.rspec +0 -2
- data/.travis.yml +0 -7
- data/.yardopts +0 -1
- data/Gemfile +0 -2
- data/LICENSE +0 -22
- data/Rakefile +0 -6
- data/internals.md +0 -64
- data/lib/mustermann/ast/tree_renderer.rb +0 -29
- data/lib/mustermann/rails.rb +0 -17
- data/lib/mustermann/shell.rb +0 -22
- data/lib/mustermann/simple.rb +0 -35
- data/lib/mustermann/template.rb +0 -47
- data/spec/rails_spec.rb +0 -521
- data/spec/shell_spec.rb +0 -108
- data/spec/simple_spec.rb +0 -236
- data/spec/support/coverage.rb +0 -18
- data/spec/support/env.rb +0 -6
- data/spec/support/expand_matcher.rb +0 -27
- data/spec/support/match_matcher.rb +0 -39
- data/spec/support/pattern.rb +0 -28
- data/spec/support.rb +0 -5
- data/spec/template_spec.rb +0 -814
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require 'mustermann/router/rack'
|
|
2
|
+
|
|
3
|
+
describe Mustermann::Router::Rack do
|
|
4
|
+
include Rack::Test::Methods
|
|
5
|
+
subject(:app) { Mustermann::Router::Rack.new }
|
|
6
|
+
|
|
7
|
+
context 'matching' do
|
|
8
|
+
before { app.on('/foo') { [418, {'Content-Type' => 'text/plain'}, 'bar'] } }
|
|
9
|
+
example { get('/foo').status.should be == 418 }
|
|
10
|
+
example { get('/bar').status.should be == 404 }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
context "params" do
|
|
14
|
+
before { app.on('/:name') { |e| [200, {'Content-Type' => 'text/plain'}, e['mustermann.params']['name']] } }
|
|
15
|
+
example { get('/foo').body.should be == 'foo' }
|
|
16
|
+
example { get('/bar').body.should be == 'bar' }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
context 'X-Cascade: pass' do
|
|
20
|
+
before do
|
|
21
|
+
app.on('/') { [200, { 'X-Cascade' => 'pass' }, ['a']] }
|
|
22
|
+
app.on('/') { [200, { 'x-cascade' => 'pass' }, ['b']] }
|
|
23
|
+
app.on('/') { [200, { 'Content-Type' => 'text/plain' }, ['c']] }
|
|
24
|
+
app.on('/') { [200, { 'Content-Type' => 'text/plain' }, ['d']] }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
example { get('/').body.should be == 'c' }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
context 'throw :pass' do
|
|
31
|
+
before do
|
|
32
|
+
app.on('/') { throw :pass }
|
|
33
|
+
app.on('/') { [200, { 'Content-Type' => 'text/plain' }, ['b']] }
|
|
34
|
+
app.on('/') { [200, { 'Content-Type' => 'text/plain' }, ['c']] }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
example { get('/').body.should be == 'b' }
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'mustermann/router/simple'
|
|
2
|
+
|
|
3
|
+
describe Mustermann::Router::Simple do
|
|
4
|
+
describe :initialize do
|
|
5
|
+
context "with implicit receiver" do
|
|
6
|
+
subject(:router) { Mustermann::Router::Simple.new { on('/foo') { 'bar' } } }
|
|
7
|
+
example { router.call('/foo').should be == 'bar' }
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
context "with explicit receiver" do
|
|
11
|
+
subject(:router) { Mustermann::Router::Simple.new { |r| r.on('/foo') { 'bar' } } }
|
|
12
|
+
example { router.call('/foo').should be == 'bar' }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
context "with default" do
|
|
16
|
+
subject(:router) { Mustermann::Router::Simple.new(default: 'bar') }
|
|
17
|
+
example { router.call('/foo').should be == 'bar' }
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
describe :[]= do
|
|
22
|
+
subject(:router) { Mustermann::Router::Simple.new }
|
|
23
|
+
before { router['/:name'] = proc { |*a| a } }
|
|
24
|
+
example { router.call('/foo').should be == ['/foo', "name" => 'foo'] }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
describe :[] do
|
|
28
|
+
subject(:router) { Mustermann::Router::Simple.new }
|
|
29
|
+
before { router.on('/x') { 42 } }
|
|
30
|
+
example { router['/x'].call.should be == 42 }
|
|
31
|
+
end
|
|
32
|
+
end
|
data/spec/simple_match_spec.rb
CHANGED
|
@@ -2,7 +2,7 @@ require 'support'
|
|
|
2
2
|
require 'mustermann/simple_match'
|
|
3
3
|
|
|
4
4
|
describe Mustermann::SimpleMatch do
|
|
5
|
-
subject {
|
|
5
|
+
subject { Mustermann::SimpleMatch.new('example') }
|
|
6
6
|
its(:to_s) { should be == 'example' }
|
|
7
7
|
its(:names) { should be == [] }
|
|
8
8
|
its(:captures) { should be == [] }
|
data/spec/sinatra_spec.rb
CHANGED
|
@@ -7,6 +7,11 @@ describe Mustermann::Sinatra do
|
|
|
7
7
|
pattern '' do
|
|
8
8
|
it { should match('') }
|
|
9
9
|
it { should_not match('/') }
|
|
10
|
+
|
|
11
|
+
it { should generate_template('') }
|
|
12
|
+
|
|
13
|
+
it { should respond_to(:expand) }
|
|
14
|
+
it { should respond_to(:to_templates) }
|
|
10
15
|
end
|
|
11
16
|
|
|
12
17
|
pattern '/' do
|
|
@@ -43,6 +48,8 @@ describe Mustermann::Sinatra do
|
|
|
43
48
|
it { should_not match('/foo/bar') }
|
|
44
49
|
it { should_not match('/') }
|
|
45
50
|
it { should_not match('/foo/') }
|
|
51
|
+
|
|
52
|
+
it { should generate_template('/{foo}') }
|
|
46
53
|
end
|
|
47
54
|
|
|
48
55
|
pattern '/föö' do
|
|
@@ -61,10 +68,34 @@ describe Mustermann::Sinatra do
|
|
|
61
68
|
|
|
62
69
|
example { pattern.params('/bar/foo').should be == {"foo" => "bar", "bar" => "foo"} }
|
|
63
70
|
example { pattern.params('').should be_nil }
|
|
71
|
+
|
|
72
|
+
it { should generate_template('/{foo}/{bar}') }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
pattern "/{foo}/{bar}" do
|
|
76
|
+
it { should match('/foo/bar') .capturing foo: 'foo', bar: 'bar' }
|
|
77
|
+
it { should match('/foo.bar/bar.foo') .capturing foo: 'foo.bar', bar: 'bar.foo' }
|
|
78
|
+
it { should match('/user@example.com/name') .capturing foo: 'user@example.com', bar: 'name' }
|
|
79
|
+
it { should match('/10.1/te.st') .capturing foo: '10.1', bar: 'te.st' }
|
|
80
|
+
it { should match('/10.1.2/te.st') .capturing foo: '10.1.2', bar: 'te.st' }
|
|
81
|
+
|
|
82
|
+
it { should_not match('/foo%2Fbar') }
|
|
83
|
+
it { should_not match('/foo%2fbar') }
|
|
84
|
+
|
|
85
|
+
example { pattern.params('/bar/foo').should be == {"foo" => "bar", "bar" => "foo"} }
|
|
86
|
+
example { pattern.params('').should be_nil }
|
|
87
|
+
|
|
88
|
+
it { should generate_template('/{foo}/{bar}') }
|
|
64
89
|
end
|
|
65
90
|
|
|
66
91
|
pattern '/hello/:person' do
|
|
67
92
|
it { should match('/hello/Frank').capturing person: 'Frank' }
|
|
93
|
+
it { should generate_template('/hello/{person}') }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
pattern '/hello/{person}' do
|
|
97
|
+
it { should match('/hello/Frank').capturing person: 'Frank' }
|
|
98
|
+
it { should generate_template('/hello/{person}') }
|
|
68
99
|
end
|
|
69
100
|
|
|
70
101
|
pattern '/?:foo?/?:bar?' do
|
|
@@ -73,32 +104,91 @@ describe Mustermann::Sinatra do
|
|
|
73
104
|
it { should match('/') .capturing foo: nil, bar: nil }
|
|
74
105
|
it { should match('') .capturing foo: nil, bar: nil }
|
|
75
106
|
|
|
76
|
-
it {
|
|
107
|
+
it { should expand(foo: 'hello') .to('/hello/') }
|
|
108
|
+
it { should expand(foo: 'hello', bar: 'world') .to('/hello/world') }
|
|
109
|
+
it { should expand(bar: 'world') .to('//world') }
|
|
110
|
+
it { should expand .to('//') }
|
|
111
|
+
it { should_not expand(baz: '') }
|
|
77
112
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
# it { should expand(bar: 'world') .to('//world') }
|
|
81
|
-
# it { should expand .to('') }
|
|
82
|
-
# it { should_not expand(baz: '') }
|
|
113
|
+
it { should_not match('/hello/world/') }
|
|
114
|
+
it { should generate_templates("", "/", "//", "//{bar}", "/{bar}", "/{foo}", "/{foo}/", "/{foo}/{bar}", "/{foo}{bar}", "{bar}", "{foo}", "{foo}/", "{foo}/{bar}", "{foo}{bar}") }
|
|
83
115
|
end
|
|
84
116
|
|
|
85
117
|
pattern '/:foo_bar' do
|
|
86
118
|
it { should match('/hello').capturing foo_bar: 'hello' }
|
|
119
|
+
it { should generate_template('/{foo_bar}') }
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
pattern '/{foo.bar}' do
|
|
123
|
+
it { should match('/hello').capturing :"foo.bar" => 'hello' }
|
|
124
|
+
it { should generate_template('/{foo.bar}') }
|
|
87
125
|
end
|
|
88
126
|
|
|
89
127
|
pattern '/*' do
|
|
90
128
|
it { should match('/') .capturing splat: '' }
|
|
91
129
|
it { should match('/foo') .capturing splat: 'foo' }
|
|
92
130
|
it { should match('/foo/bar') .capturing splat: 'foo/bar' }
|
|
131
|
+
it { should generate_template('/{+splat}') }
|
|
132
|
+
|
|
133
|
+
example { pattern.params('/foo').should be == {"splat" => ["foo"]} }
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
pattern '/{+splat}' do
|
|
137
|
+
it { should match('/') .capturing splat: '' }
|
|
138
|
+
it { should match('/foo') .capturing splat: 'foo' }
|
|
139
|
+
it { should match('/foo/bar') .capturing splat: 'foo/bar' }
|
|
140
|
+
it { should generate_template('/{+splat}') }
|
|
93
141
|
|
|
94
142
|
example { pattern.params('/foo').should be == {"splat" => ["foo"]} }
|
|
95
143
|
end
|
|
96
144
|
|
|
145
|
+
pattern '/*foo' do
|
|
146
|
+
it { should match('/') .capturing foo: '' }
|
|
147
|
+
it { should match('/foo') .capturing foo: 'foo' }
|
|
148
|
+
it { should match('/foo/bar') .capturing foo: 'foo/bar' }
|
|
149
|
+
it { should generate_template('/{+foo}') }
|
|
150
|
+
|
|
151
|
+
example { pattern.params('/foo') .should be == {"foo" => "foo" } }
|
|
152
|
+
example { pattern.params('/foo/bar') .should be == {"foo" => "foo/bar" } }
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
pattern '/{+foo}' do
|
|
156
|
+
it { should match('/') .capturing foo: '' }
|
|
157
|
+
it { should match('/foo') .capturing foo: 'foo' }
|
|
158
|
+
it { should match('/foo/bar') .capturing foo: 'foo/bar' }
|
|
159
|
+
it { should generate_template('/{+foo}') }
|
|
160
|
+
|
|
161
|
+
example { pattern.params('/foo') .should be == {"foo" => "foo" } }
|
|
162
|
+
example { pattern.params('/foo/bar') .should be == {"foo" => "foo/bar" } }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
pattern '/*foo/*bar' do
|
|
166
|
+
it { should match('/foo/bar') .capturing foo: 'foo', bar: 'bar' }
|
|
167
|
+
it { should generate_template('/{+foo}/{+bar}') }
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
pattern '/{+foo}/{+bar}' do
|
|
171
|
+
it { should match('/foo/bar') .capturing foo: 'foo', bar: 'bar' }
|
|
172
|
+
it { should generate_template('/{+foo}/{+bar}') }
|
|
173
|
+
end
|
|
174
|
+
|
|
97
175
|
pattern '/:foo/*' do
|
|
98
176
|
it { should match("/foo/bar/baz") .capturing foo: 'foo', splat: 'bar/baz' }
|
|
99
177
|
it { should match("/foo/") .capturing foo: 'foo', splat: '' }
|
|
100
178
|
it { should match('/h%20w/h%20a%20y') .capturing foo: 'h%20w', splat: 'h%20a%20y' }
|
|
101
179
|
it { should_not match('/foo') }
|
|
180
|
+
it { should generate_template('/{foo}/{+splat}') }
|
|
181
|
+
|
|
182
|
+
example { pattern.params('/bar/foo').should be == {"splat" => ["foo"], "foo" => "bar"} }
|
|
183
|
+
example { pattern.params('/bar/foo/f%20o').should be == {"splat" => ["foo/f o"], "foo" => "bar"} }
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
pattern '/{foo}/*' do
|
|
187
|
+
it { should match("/foo/bar/baz") .capturing foo: 'foo', splat: 'bar/baz' }
|
|
188
|
+
it { should match("/foo/") .capturing foo: 'foo', splat: '' }
|
|
189
|
+
it { should match('/h%20w/h%20a%20y') .capturing foo: 'h%20w', splat: 'h%20a%20y' }
|
|
190
|
+
it { should_not match('/foo') }
|
|
191
|
+
it { should generate_template('/{foo}/{+splat}') }
|
|
102
192
|
|
|
103
193
|
example { pattern.params('/bar/foo').should be == {"splat" => ["foo"], "foo" => "bar"} }
|
|
104
194
|
example { pattern.params('/bar/foo/f%20o').should be == {"splat" => ["foo/f o"], "foo" => "bar"} }
|
|
@@ -118,12 +208,18 @@ describe Mustermann::Sinatra do
|
|
|
118
208
|
it { should match('/path%20with%20spaces') }
|
|
119
209
|
it { should match('/path%2Bwith%2Bspaces') }
|
|
120
210
|
it { should match('/path+with+spaces') }
|
|
211
|
+
|
|
212
|
+
it { should generate_template('/path%20with%20spaces') }
|
|
121
213
|
end
|
|
122
214
|
|
|
123
215
|
pattern '/foo&bar' do
|
|
124
216
|
it { should match('/foo&bar') }
|
|
125
217
|
end
|
|
126
218
|
|
|
219
|
+
pattern '/foo\{bar' do
|
|
220
|
+
it { should match('/foo%7Bbar') }
|
|
221
|
+
end
|
|
222
|
+
|
|
127
223
|
pattern '/*/:foo/*/*' do
|
|
128
224
|
it { should match('/bar/foo/bling/baz/boom') }
|
|
129
225
|
|
|
@@ -140,13 +236,29 @@ describe Mustermann::Sinatra do
|
|
|
140
236
|
end
|
|
141
237
|
end
|
|
142
238
|
|
|
239
|
+
pattern '/{+splat}/{foo}/{+splat}/{+splat}' do
|
|
240
|
+
it { should match('/bar/foo/bling/baz/boom') }
|
|
241
|
+
|
|
242
|
+
it "should capture all splat parts" do
|
|
243
|
+
match = pattern.match('/bar/foo/bling/baz/boom')
|
|
244
|
+
match.captures.should be == ['bar', 'foo', 'bling', 'baz/boom']
|
|
245
|
+
match.names.should be == ['splat', 'foo']
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
it 'should map to proper params' do
|
|
249
|
+
pattern.params('/bar/foo/bling/baz/boom').should be == {
|
|
250
|
+
"foo" => "foo", "splat" => ['bar', 'bling', 'baz/boom']
|
|
251
|
+
}
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
|
|
143
255
|
pattern '/test.bar' do
|
|
144
256
|
it { should match('/test.bar') }
|
|
145
257
|
it { should_not match('/test0bar') }
|
|
146
258
|
end
|
|
147
259
|
|
|
148
260
|
pattern '/:file.:ext' do
|
|
149
|
-
it { should match('/pony.jpg')
|
|
261
|
+
it { should match('/pony.jpg') .capturing file: 'pony', ext: 'jpg' }
|
|
150
262
|
it { should match('/pony%2Ejpg') .capturing file: 'pony', ext: 'jpg' }
|
|
151
263
|
it { should match('/pony%2ejpg') .capturing file: 'pony', ext: 'jpg' }
|
|
152
264
|
|
|
@@ -166,12 +278,18 @@ describe Mustermann::Sinatra do
|
|
|
166
278
|
it { should match('/ax') .capturing a: 'a' }
|
|
167
279
|
it { should match('/axax') .capturing a: 'axa' }
|
|
168
280
|
it { should match('/axaxx') .capturing a: 'axax' }
|
|
281
|
+
|
|
282
|
+
it { should generate_template('/{a}x') }
|
|
283
|
+
it { should generate_template('/{a}') }
|
|
169
284
|
end
|
|
170
285
|
|
|
171
286
|
pattern '/:user(@:host)?' do
|
|
172
287
|
it { should match('/foo@bar') .capturing user: 'foo', host: 'bar' }
|
|
173
288
|
it { should match('/foo.foo@bar') .capturing user: 'foo.foo', host: 'bar' }
|
|
174
289
|
it { should match('/foo@bar.bar') .capturing user: 'foo', host: 'bar.bar' }
|
|
290
|
+
|
|
291
|
+
it { should generate_template('/{user}') }
|
|
292
|
+
it { should generate_template('/{user}@{host}') }
|
|
175
293
|
end
|
|
176
294
|
|
|
177
295
|
pattern '/:file(.:ext)?' do
|
|
@@ -182,6 +300,10 @@ describe Mustermann::Sinatra do
|
|
|
182
300
|
it { should match('/pony.png.jpg') .capturing file: 'pony.png', ext: 'jpg' }
|
|
183
301
|
it { should match('/pony.') .capturing file: 'pony.' }
|
|
184
302
|
it { should_not match('/.jpg') }
|
|
303
|
+
|
|
304
|
+
it { should generate_template('/{file}') }
|
|
305
|
+
it { should generate_template('/{file}.{ext}') }
|
|
306
|
+
it { should_not generate_template('/{file}.') }
|
|
185
307
|
end
|
|
186
308
|
|
|
187
309
|
pattern '/:id/test.bar' do
|
|
@@ -310,6 +432,37 @@ describe Mustermann::Sinatra do
|
|
|
310
432
|
it { should match('/foo/bar/baz').capturing foo: 'foo', bar: 'bar', baz: 'baz' }
|
|
311
433
|
end
|
|
312
434
|
|
|
435
|
+
pattern "/(foo|bar)" do
|
|
436
|
+
it { should match("/foo") }
|
|
437
|
+
it { should match("/bar") }
|
|
438
|
+
|
|
439
|
+
it { should generate_template('/foo') }
|
|
440
|
+
it { should generate_template('/bar') }
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
pattern "/(foo\\|bar)" do
|
|
444
|
+
it { should match "/foo%7Cbar" }
|
|
445
|
+
it { should generate_template "/foo%7Cbar" }
|
|
446
|
+
|
|
447
|
+
it { should_not match("/foo") }
|
|
448
|
+
it { should_not match("/bar") }
|
|
449
|
+
|
|
450
|
+
it { should_not generate_template('/foo') }
|
|
451
|
+
it { should_not generate_template('/bar') }
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
pattern "/(:a/:b|:c)" do
|
|
455
|
+
it { should match("/foo") .capturing c: 'foo' }
|
|
456
|
+
it { should match("/foo/bar") .capturing a: 'foo', b: 'bar' }
|
|
457
|
+
|
|
458
|
+
it { should generate_template('/{a}/{b}') }
|
|
459
|
+
it { should generate_template('/{c}') }
|
|
460
|
+
|
|
461
|
+
it { should expand(a: 'foo', b: 'bar') .to('/foo/bar') }
|
|
462
|
+
it { should expand(c: 'foo') .to('/foo') }
|
|
463
|
+
it { should_not expand(a: 'foo', b: 'bar', c: 'baz') }
|
|
464
|
+
end
|
|
465
|
+
|
|
313
466
|
pattern '/:foo', capture: /\d+/ do
|
|
314
467
|
it { should match('/1') .capturing foo: '1' }
|
|
315
468
|
it { should match('/123') .capturing foo: '123' }
|
|
@@ -495,6 +648,11 @@ describe Mustermann::Sinatra do
|
|
|
495
648
|
to raise_error(Mustermann::ParseError, 'unexpected ? while parsing "foo??bar"')
|
|
496
649
|
end
|
|
497
650
|
|
|
651
|
+
example '| outside of group' do
|
|
652
|
+
expect { Mustermann::Sinatra.new('foo|bar') }.
|
|
653
|
+
to raise_error(Mustermann::ParseError, 'unexpected | while parsing "foo|bar"')
|
|
654
|
+
end
|
|
655
|
+
|
|
498
656
|
example 'dangling escape' do
|
|
499
657
|
expect { Mustermann::Sinatra.new('foo\\') }.
|
|
500
658
|
to raise_error(Mustermann::ParseError, 'unexpected end of string while parsing "foo\\\\"')
|
|
@@ -550,11 +708,41 @@ describe Mustermann::Sinatra do
|
|
|
550
708
|
end
|
|
551
709
|
|
|
552
710
|
describe :to_regexp do
|
|
553
|
-
example('empty pattern') { Mustermann::Sinatra.new('').to_regexp.should be == /\A\Z/ }
|
|
711
|
+
example('empty pattern') { Mustermann::Sinatra.new('').to_regexp.should be == /\A(?-mix:)\Z/ }
|
|
554
712
|
|
|
555
713
|
context 'Regexp.try_convert' do
|
|
556
|
-
example('empty pattern') { Regexp.try_convert(Mustermann::Sinatra.new('')).should be == /\A\Z/ }
|
|
714
|
+
example('empty pattern') { Regexp.try_convert(Mustermann::Sinatra.new('')).should be == /\A(?-mix:)\Z/ }
|
|
557
715
|
end
|
|
558
716
|
end
|
|
559
717
|
end
|
|
718
|
+
|
|
719
|
+
context 'Proc compatibility' do
|
|
720
|
+
describe :to_proc do
|
|
721
|
+
example { Mustermann::Sinatra.new("/").to_proc.should be_a(Proc) }
|
|
722
|
+
example('non-matching') { Mustermann::Sinatra.new("/") .to_proc.call('/foo').should be == false }
|
|
723
|
+
example('matching') { Mustermann::Sinatra.new("/:foo") .to_proc.call('/foo').should be == true }
|
|
724
|
+
end
|
|
725
|
+
end
|
|
726
|
+
|
|
727
|
+
context "peeking" do
|
|
728
|
+
subject(:pattern) { Mustermann::Sinatra.new(":name") }
|
|
729
|
+
|
|
730
|
+
describe :peek_size do
|
|
731
|
+
example { pattern.peek_size("foo bar/blah") .should be == "foo bar".size }
|
|
732
|
+
example { pattern.peek_size("foo%20bar/blah") .should be == "foo%20bar".size }
|
|
733
|
+
example { pattern.peek_size("/foo bar") .should be_nil }
|
|
734
|
+
end
|
|
735
|
+
|
|
736
|
+
describe :peek_match do
|
|
737
|
+
example { pattern.peek_match("foo bar/blah") .to_s .should be == "foo bar" }
|
|
738
|
+
example { pattern.peek_match("foo%20bar/blah") .to_s .should be == "foo%20bar" }
|
|
739
|
+
example { pattern.peek_match("/foo bar") .should be_nil }
|
|
740
|
+
end
|
|
741
|
+
|
|
742
|
+
describe :peek_params do
|
|
743
|
+
example { pattern.peek_params("foo bar/blah") .should be == [{"name" => "foo bar"}, "foo bar".size] }
|
|
744
|
+
example { pattern.peek_params("foo%20bar/blah") .should be == [{"name" => "foo bar"}, "foo%20bar".size] }
|
|
745
|
+
example { pattern.peek_params("/foo bar") .should be_nil }
|
|
746
|
+
end
|
|
747
|
+
end
|
|
560
748
|
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require 'support'
|
|
2
|
+
require 'mustermann/to_pattern'
|
|
3
|
+
require 'delegate'
|
|
4
|
+
|
|
5
|
+
describe Mustermann::ToPattern do
|
|
6
|
+
context String do
|
|
7
|
+
example { "".to_pattern .should be_a(Mustermann::Sinatra) }
|
|
8
|
+
example { "".to_pattern(type: :rails) .should be_a(Mustermann::Rails) }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
context Regexp do
|
|
12
|
+
example { //.to_pattern .should be_a(Mustermann::Regular) }
|
|
13
|
+
example { //.to_pattern(type: :rails) .should be_a(Mustermann::Regular) }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
context Symbol do
|
|
17
|
+
example { :foo.to_pattern .should be_a(Mustermann::Sinatra) }
|
|
18
|
+
example { :foo.to_pattern(type: :rails) .should be_a(Mustermann::Sinatra) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
context Array do
|
|
22
|
+
example { [:foo, :bar].to_pattern .should be_a(Mustermann::Composite) }
|
|
23
|
+
example { [:foo, :bar].to_pattern(type: :rails) .should be_a(Mustermann::Composite) }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
context Mustermann::Pattern do
|
|
27
|
+
subject(:pattern) { Mustermann.new('') }
|
|
28
|
+
example { pattern.to_pattern.should be == pattern }
|
|
29
|
+
example { pattern.to_pattern(type: :rails).should be_a(Mustermann::Sinatra) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context 'custom class' do
|
|
33
|
+
let(:example_class) do
|
|
34
|
+
Class.new do
|
|
35
|
+
include Mustermann::ToPattern
|
|
36
|
+
def to_s
|
|
37
|
+
":foo/:bar"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
example { example_class.new.to_pattern .should be_a(Mustermann::Sinatra) }
|
|
43
|
+
example { example_class.new.to_pattern(type: :rails) .should be_a(Mustermann::Rails) }
|
|
44
|
+
example { Mustermann.new(example_class.new) .should be_a(Mustermann::Sinatra) }
|
|
45
|
+
example { Mustermann.new(example_class.new, type: :rails) .should be_a(Mustermann::Rails) }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
context 'primitive delegate' do
|
|
49
|
+
let(:example_class) do
|
|
50
|
+
Class.new(DelegateClass(Array)) do
|
|
51
|
+
include Mustermann::ToPattern
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
example { example_class.new([:foo, :bar]).to_pattern .should be_a(Mustermann::Composite) }
|
|
56
|
+
example { example_class.new([:foo, :bar]).to_pattern(type: :rails) .should be_a(Mustermann::Composite) }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
context 'primitive subclass' do
|
|
60
|
+
let(:example_class) do
|
|
61
|
+
Class.new(Array) do
|
|
62
|
+
include Mustermann::ToPattern
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
example { example_class.new([:foo, :bar]).to_pattern .should be_a(Mustermann::Composite) }
|
|
67
|
+
example { example_class.new([:foo, :bar]).to_pattern(type: :rails) .should be_a(Mustermann::Composite) }
|
|
68
|
+
end
|
|
69
|
+
end
|