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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +472 -321
  3. data/bench/capturing.rb +4 -4
  4. data/bench/regexp.rb +21 -0
  5. data/lib/mustermann/ast/boundaries.rb +44 -0
  6. data/lib/mustermann/ast/compiler.rb +13 -7
  7. data/lib/mustermann/ast/expander.rb +45 -14
  8. data/lib/mustermann/ast/node.rb +69 -6
  9. data/lib/mustermann/ast/param_scanner.rb +20 -0
  10. data/lib/mustermann/ast/parser.rb +140 -22
  11. data/lib/mustermann/ast/pattern.rb +68 -21
  12. data/lib/mustermann/ast/template_generator.rb +28 -0
  13. data/lib/mustermann/ast/transformer.rb +32 -24
  14. data/lib/mustermann/ast/translator.rb +24 -10
  15. data/lib/mustermann/ast/validation.rb +10 -6
  16. data/lib/mustermann/caster.rb +116 -0
  17. data/lib/mustermann/composite.rb +101 -0
  18. data/lib/mustermann/expander.rb +204 -0
  19. data/lib/mustermann/identity.rb +56 -0
  20. data/lib/mustermann/mapper.rb +94 -0
  21. data/lib/mustermann/pattern.rb +193 -13
  22. data/lib/mustermann/pattern_cache.rb +49 -0
  23. data/lib/mustermann/regexp.rb +1 -0
  24. data/lib/mustermann/regexp_based.rb +19 -2
  25. data/lib/mustermann/regular.rb +29 -0
  26. data/lib/mustermann/router/rack.rb +47 -0
  27. data/lib/mustermann/router/simple.rb +142 -0
  28. data/lib/mustermann/router.rb +9 -0
  29. data/lib/mustermann/simple_match.rb +5 -0
  30. data/lib/mustermann/sinatra.rb +22 -5
  31. data/lib/mustermann/to_pattern.rb +50 -0
  32. data/lib/mustermann/version.rb +1 -1
  33. data/lib/mustermann.rb +102 -19
  34. data/mustermann.gemspec +2 -12
  35. data/spec/ast_spec.rb +14 -0
  36. data/spec/composite_spec.rb +147 -0
  37. data/spec/expander_spec.rb +120 -0
  38. data/spec/identity_spec.rb +44 -0
  39. data/spec/mapper_spec.rb +83 -0
  40. data/spec/mustermann_spec.rb +50 -11
  41. data/spec/pattern_spec.rb +34 -7
  42. data/spec/regexp_based_spec.rb +1 -1
  43. data/spec/regular_spec.rb +61 -0
  44. data/spec/router/rack_spec.rb +39 -0
  45. data/spec/router/simple_spec.rb +32 -0
  46. data/spec/simple_match_spec.rb +1 -1
  47. data/spec/sinatra_spec.rb +197 -9
  48. data/spec/to_pattern_spec.rb +69 -0
  49. metadata +44 -146
  50. data/.gitignore +0 -18
  51. data/.rspec +0 -2
  52. data/.travis.yml +0 -7
  53. data/.yardopts +0 -1
  54. data/Gemfile +0 -2
  55. data/LICENSE +0 -22
  56. data/Rakefile +0 -6
  57. data/internals.md +0 -64
  58. data/lib/mustermann/ast/tree_renderer.rb +0 -29
  59. data/lib/mustermann/rails.rb +0 -17
  60. data/lib/mustermann/shell.rb +0 -22
  61. data/lib/mustermann/simple.rb +0 -35
  62. data/lib/mustermann/template.rb +0 -47
  63. data/spec/rails_spec.rb +0 -521
  64. data/spec/shell_spec.rb +0 -108
  65. data/spec/simple_spec.rb +0 -236
  66. data/spec/support/coverage.rb +0 -18
  67. data/spec/support/env.rb +0 -6
  68. data/spec/support/expand_matcher.rb +0 -27
  69. data/spec/support/match_matcher.rb +0 -39
  70. data/spec/support/pattern.rb +0 -28
  71. data/spec/support.rb +0 -5
  72. data/spec/template_spec.rb +0 -814
@@ -0,0 +1,147 @@
1
+ require 'support'
2
+ require 'mustermann'
3
+
4
+ describe Mustermann::Composite do
5
+ describe :new do
6
+ example 'with no argument' do
7
+ expect { Mustermann::Composite.new }.
8
+ to raise_error(ArgumentError, 'cannot create empty composite pattern')
9
+ end
10
+
11
+ example 'with one argument' do
12
+ pattern = Mustermann.new('/foo')
13
+ Mustermann::Composite.new(pattern).should be == pattern
14
+ end
15
+ end
16
+
17
+ context :| do
18
+ subject(:pattern) { Mustermann.new('/foo/:name', '/:first/:second') }
19
+
20
+ describe :== do
21
+ example { subject.should be == subject }
22
+ example { subject.should be == Mustermann.new('/foo/:name', '/:first/:second') }
23
+ example { subject.should_not be == Mustermann.new('/foo/:name') }
24
+ example { subject.should_not be == Mustermann.new('/foo/:name', '/:first/:second', operator: :&) }
25
+ end
26
+
27
+ describe :=== do
28
+ example { subject.should be === "/foo/bar" }
29
+ example { subject.should be === "/fox/bar" }
30
+ example { subject.should_not be === "/foo" }
31
+ end
32
+
33
+ describe :params do
34
+ example { subject.params("/foo/bar") .should be == { "name" => "bar" } }
35
+ example { subject.params("/fox/bar") .should be == { "first" => "fox", "second" => "bar" } }
36
+ example { subject.params("/foo") .should be_nil }
37
+ end
38
+
39
+ describe :=== do
40
+ example { subject.should match("/foo/bar") }
41
+ example { subject.should match("/fox/bar") }
42
+ example { subject.should_not match("/foo") }
43
+ end
44
+
45
+ describe :expand do
46
+ example { subject.should respond_to(:expand) }
47
+ example { subject.expand(name: 'bar') .should be == '/foo/bar' }
48
+ example { subject.expand(first: 'fox', second: 'bar') .should be == '/fox/bar' }
49
+
50
+ context "without expandable patterns" do
51
+ subject(:pattern) { Mustermann.new('/foo/:name', '/:first/:second', type: :simple) }
52
+ example { subject.should_not respond_to(:expand) }
53
+ example { expect { subject.expand(name: 'bar') }.to raise_error(NotImplementedError) }
54
+ end
55
+ end
56
+
57
+ describe :to_templates do
58
+ example { should respond_to(:to_templates) }
59
+ example { should generate_templates('/foo/{name}', '/{first}/{second}') }
60
+
61
+ context "without patterns implementing to_templates" do
62
+ subject(:pattern) { Mustermann.new('/foo/:name', '/:first/:second', type: :simple) }
63
+ example { should_not respond_to(:to_templates) }
64
+ example { expect { subject.to_templates }.to raise_error(NotImplementedError) }
65
+ end
66
+ end
67
+ end
68
+
69
+ context :& do
70
+ subject(:pattern) { Mustermann.new('/foo/:name', '/:first/:second', operator: :&) }
71
+
72
+ describe :== do
73
+ example { subject.should be == subject }
74
+ example { subject.should be == Mustermann.new('/foo/:name', '/:first/:second', operator: :&) }
75
+ example { subject.should_not be == Mustermann.new('/foo/:name') }
76
+ example { subject.should_not be == Mustermann.new('/foo/:name', '/:first/:second') }
77
+ end
78
+
79
+ describe :=== do
80
+ example { subject.should be === "/foo/bar" }
81
+ example { subject.should_not be === "/fox/bar" }
82
+ example { subject.should_not be === "/foo" }
83
+ end
84
+
85
+ describe :params do
86
+ example { subject.params("/foo/bar") .should be == { "name" => "bar" } }
87
+ example { subject.params("/fox/bar") .should be_nil }
88
+ example { subject.params("/foo") .should be_nil }
89
+ end
90
+
91
+ describe :match do
92
+ example { subject.should match("/foo/bar") }
93
+ example { subject.should_not match("/fox/bar") }
94
+ example { subject.should_not match("/foo") }
95
+ end
96
+
97
+ describe :expand do
98
+ example { subject.should_not respond_to(:expand) }
99
+ example { expect { subject.expand(name: 'bar') }.to raise_error(NotImplementedError) }
100
+ end
101
+ end
102
+
103
+ context :^ do
104
+ subject(:pattern) { Mustermann.new('/foo/:name', '/:first/:second', operator: :^) }
105
+
106
+ describe :== do
107
+ example { subject.should be == subject }
108
+ example { subject.should_not be == Mustermann.new('/foo/:name', '/:first/:second') }
109
+ example { subject.should_not be == Mustermann.new('/foo/:name') }
110
+ example { subject.should_not be == Mustermann.new('/foo/:name', '/:first/:second', operator: :&) }
111
+ end
112
+
113
+ describe :=== do
114
+ example { subject.should_not be === "/foo/bar" }
115
+ example { subject.should be === "/fox/bar" }
116
+ example { subject.should_not be === "/foo" }
117
+ end
118
+
119
+ describe :params do
120
+ example { subject.params("/foo/bar") .should be_nil }
121
+ example { subject.params("/fox/bar") .should be == { "first" => "fox", "second" => "bar" } }
122
+ example { subject.params("/foo") .should be_nil }
123
+ end
124
+
125
+ describe :match do
126
+ example { subject.should_not match("/foo/bar") }
127
+ example { subject.should match("/fox/bar") }
128
+ example { subject.should_not match("/foo") }
129
+ end
130
+
131
+ describe :expand do
132
+ example { subject.should_not respond_to(:expand) }
133
+ example { expect { subject.expand(name: 'bar') }.to raise_error(NotImplementedError) }
134
+ end
135
+ end
136
+
137
+ describe :inspect do
138
+ let(:sinatra) { Mustermann.new('x') }
139
+ let(:rails) { Mustermann.new('x', type: :rails) }
140
+ let(:identity) { Mustermann.new('x', type: :identity) }
141
+
142
+ example { (sinatra | rails) .inspect.should include('(sinatra:"x" | rails:"x")') }
143
+ example { (sinatra ^ rails) .inspect.should include('(sinatra:"x" ^ rails:"x")') }
144
+ example { (sinatra | rails | identity) .inspect.should include('(sinatra:"x" | rails:"x" | identity:"x")') }
145
+ example { (sinatra | rails & identity) .inspect.should include('(sinatra:"x" | (rails:"x" & identity:"x"))') }
146
+ end
147
+ end
@@ -0,0 +1,120 @@
1
+ require 'support'
2
+ require 'mustermann/expander'
3
+
4
+ describe Mustermann::Expander do
5
+ it 'expands a pattern' do
6
+ expander = Mustermann::Expander.new("/:foo.jpg")
7
+ expander.expand(foo: 42).should be == "/42.jpg"
8
+ end
9
+
10
+ it 'expands multiple patterns' do
11
+ expander = Mustermann::Expander.new << "/:foo.:ext" << "/:foo"
12
+ expander.expand(foo: 42, ext: 'jpg').should be == "/42.jpg"
13
+ expander.expand(foo: 23).should be == "/23"
14
+ end
15
+
16
+ it 'supports setting pattern options' do
17
+ expander = Mustermann::Expander.new(type: :rails) << "/:foo(.:ext)" << "/:bar"
18
+ expander.expand(foo: 42, ext: 'jpg').should be == "/42.jpg"
19
+ expander.expand(foo: 42).should be == "/42"
20
+ end
21
+
22
+ it 'supports combining different pattern styles' do
23
+ expander = Mustermann::Expander.new << Mustermann.new("/:foo(.:ext)", type: :rails) << Mustermann.new("/:bar", type: :sinatra)
24
+ expander.expand(foo: 'pony', ext: 'jpg').should be == '/pony.jpg'
25
+ expander.expand(bar: 23).should be == "/23"
26
+ end
27
+
28
+ it 'ignores nil values' do
29
+ expander = Mustermann::Expander.new << Mustermann.new("/:foo(.:ext)?")
30
+ expander.expand(foo: 'pony', ext: nil).should be == '/pony'
31
+ end
32
+
33
+ it 'supports splat' do
34
+ expander = Mustermann::Expander.new << Mustermann.new("/foo/*/baz")
35
+ expander.expand(splat: 'bar').should be == '/foo/bar/baz'
36
+ end
37
+
38
+ it 'supports multiple splats' do
39
+ expander = Mustermann::Expander.new << Mustermann.new("/foo/*/bar/*")
40
+ expander.expand(splat: [123, 456]).should be == '/foo/123/bar/456'
41
+ end
42
+
43
+ it 'supports identity patterns' do
44
+ expander = Mustermann::Expander.new('/:foo', type: :identity)
45
+ expander.expand.should be == '/:foo'
46
+ end
47
+
48
+ describe :additional_values do
49
+ context "illegal value" do
50
+ example { expect { Mustermann::Expander.new(additional_values: :foo) }.to raise_error(ArgumentError) }
51
+ example { expect { Mustermann::Expander.new('/').expand(:foo, a: 10) }.to raise_error(ArgumentError) }
52
+ end
53
+
54
+ context :raise do
55
+ subject(:expander) { Mustermann::Expander.new('/:a', additional_values: :raise) }
56
+ example { expander.expand(a: ?a).should be == '/a' }
57
+ example { expect { expander.expand(a: ?a, b: ?b) }.to raise_error(Mustermann::ExpandError) }
58
+ example { expect { expander.expand(b: ?b) }.to raise_error(Mustermann::ExpandError) }
59
+ end
60
+
61
+ context :ignore do
62
+ subject(:expander) { Mustermann::Expander.new('/:a', additional_values: :ignore) }
63
+ example { expander.expand(a: ?a).should be == '/a' }
64
+ example { expander.expand(a: ?a, b: ?b).should be == '/a' }
65
+ example { expect { expander.expand(b: ?b) }.to raise_error(Mustermann::ExpandError) }
66
+ end
67
+
68
+ context :append do
69
+ subject(:expander) { Mustermann::Expander.new('/:a', additional_values: :append) }
70
+ example { expander.expand(a: ?a).should be == '/a' }
71
+ example { expander.expand(a: ?a, b: ?b).should be == '/a?b=b' }
72
+ example { expect { expander.expand(b: ?b) }.to raise_error(Mustermann::ExpandError) }
73
+ end
74
+ end
75
+
76
+ describe :cast do
77
+ subject(:expander) { Mustermann::Expander.new('/:a(/:b)?') }
78
+
79
+ example { expander.cast { "FOOBAR" }.expand(a: "foo") .should be == "/FOOBAR" }
80
+ example { expander.cast { |v| v.upcase }.expand(a: "foo") .should be == "/FOO" }
81
+ example { expander.cast { |v| v.upcase }.expand(a: "foo", b: "bar") .should be == "/FOO/BAR" }
82
+ example { expander.cast(:a) { |v| v.upcase }.expand(a: "foo", b: "bar") .should be == "/FOO/bar" }
83
+ example { expander.cast(:a, :b) { |v| v.upcase }.expand(a: "foo", b: "bar") .should be == "/FOO/BAR" }
84
+ example { expander.cast(Integer) { |k,v| "#{k}_#{v}" }.expand(a: "foo", b: 42) .should be == "/foo/b_42" }
85
+
86
+ example do
87
+ expander.cast(:a) { |v| v.upcase }
88
+ expander.cast(:b) { |v| v.downcase }
89
+ expander.expand(a: "fOo", b: "bAr").should be == "/FOO/bar"
90
+ end
91
+ end
92
+
93
+ describe :== do
94
+ example { Mustermann::Expander.new('/foo') .should be == Mustermann::Expander.new('/foo') }
95
+ example { Mustermann::Expander.new('/foo') .should_not be == Mustermann::Expander.new('/bar') }
96
+ example { Mustermann::Expander.new('/foo', type: :rails) .should be == Mustermann::Expander.new('/foo', type: :rails) }
97
+ example { Mustermann::Expander.new('/foo', type: :rails) .should_not be == Mustermann::Expander.new('/foo', type: :sinatra) }
98
+ end
99
+
100
+ describe :hash do
101
+ example { Mustermann::Expander.new('/foo') .hash.should be == Mustermann::Expander.new('/foo').hash }
102
+ example { Mustermann::Expander.new('/foo') .hash.should_not be == Mustermann::Expander.new('/bar').hash }
103
+ example { Mustermann::Expander.new('/foo', type: :rails) .hash.should be == Mustermann::Expander.new('/foo', type: :rails).hash }
104
+ example { Mustermann::Expander.new('/foo', type: :rails) .hash.should_not be == Mustermann::Expander.new('/foo', type: :sinatra).hash }
105
+ end
106
+
107
+ describe :eql? do
108
+ example { Mustermann::Expander.new('/foo') .should be_eql Mustermann::Expander.new('/foo') }
109
+ example { Mustermann::Expander.new('/foo') .should_not be_eql Mustermann::Expander.new('/bar') }
110
+ example { Mustermann::Expander.new('/foo', type: :rails) .should be_eql Mustermann::Expander.new('/foo', type: :rails) }
111
+ example { Mustermann::Expander.new('/foo', type: :rails) .should_not be_eql Mustermann::Expander.new('/foo', type: :sinatra) }
112
+ end
113
+
114
+ describe :equal? do
115
+ example { Mustermann::Expander.new('/foo') .should_not be_equal Mustermann::Expander.new('/foo') }
116
+ example { Mustermann::Expander.new('/foo') .should_not be_equal Mustermann::Expander.new('/bar') }
117
+ example { Mustermann::Expander.new('/foo', type: :rails) .should_not be_equal Mustermann::Expander.new('/foo', type: :rails) }
118
+ example { Mustermann::Expander.new('/foo', type: :rails) .should_not be_equal Mustermann::Expander.new('/foo', type: :sinatra) }
119
+ end
120
+ end
@@ -7,6 +7,21 @@ describe Mustermann::Identity do
7
7
  pattern '' do
8
8
  it { should match('') }
9
9
  it { should_not match('/') }
10
+
11
+ it { should respond_to(:expand) }
12
+ it { should respond_to(:to_templates) }
13
+
14
+
15
+ it { should generate_template('') }
16
+ it { should expand.to('') }
17
+ it { should expand(:ignore, a: 10).to('') }
18
+ it { should expand(:append, a: 10).to('?a=10') }
19
+ it { should_not expand(:raise, a: 10) }
20
+ it { should_not expand(a: 10) }
21
+
22
+ example do
23
+ pattern.match('').inspect.should be == '#<Mustermann::SimpleMatch "">'
24
+ end
10
25
  end
11
26
 
12
27
  pattern '/' do
@@ -15,6 +30,9 @@ describe Mustermann::Identity do
15
30
 
16
31
  example { pattern.params('/').should be == {} }
17
32
  example { pattern.params('').should be_nil }
33
+
34
+ it { should generate_template('/') }
35
+ it { should expand.to('/') }
18
36
  end
19
37
 
20
38
  pattern '/foo' do
@@ -37,6 +55,9 @@ describe Mustermann::Identity do
37
55
  it { should_not match('/foo/bar') }
38
56
  it { should_not match('/') }
39
57
  it { should_not match('/foo/') }
58
+
59
+ it { should generate_template('/:foo') }
60
+ it { should expand.to('/:foo') }
40
61
  end
41
62
 
42
63
  pattern '/föö' do
@@ -57,6 +78,7 @@ describe Mustermann::Identity do
57
78
  it { should match('/path%20with%20spaces') }
58
79
  it { should_not match('/path%2Bwith%2Bspaces') }
59
80
  it { should_not match('/path+with+spaces') }
81
+ it { should generate_template('/path%20with%20spaces') }
60
82
  end
61
83
 
62
84
  pattern '/foo&bar' do
@@ -79,4 +101,26 @@ describe Mustermann::Identity do
79
101
  it { should_not match('/path%2Bwith%2Bspaces') }
80
102
  it { should_not match('/path+with+spaces') }
81
103
  end
104
+
105
+ context "peeking" do
106
+ subject(:pattern) { Mustermann::Identity.new("foo bar") }
107
+
108
+ describe :peek_size do
109
+ example { pattern.peek_size("foo bar blah") .should be == "foo bar".size }
110
+ example { pattern.peek_size("foo%20bar blah") .should be == "foo%20bar".size }
111
+ example { pattern.peek_size("foobar") .should be_nil }
112
+ end
113
+
114
+ describe :peek_match do
115
+ example { pattern.peek_match("foo bar blah").to_s .should be == "foo bar" }
116
+ example { pattern.peek_match("foo%20bar blah").to_s .should be == "foo%20bar" }
117
+ example { pattern.peek_match("foobar") .should be_nil }
118
+ end
119
+
120
+ describe :peek_params do
121
+ example { pattern.peek_params("foo bar blah") .should be == [{}, "foo bar".size] }
122
+ example { pattern.peek_params("foo%20bar blah") .should be == [{}, "foo%20bar".size] }
123
+ example { pattern.peek_params("foobar") .should be_nil }
124
+ end
125
+ end
82
126
  end
@@ -0,0 +1,83 @@
1
+ require 'support'
2
+ require 'mustermann/mapper'
3
+
4
+ describe Mustermann::Mapper do
5
+ describe :initialize do
6
+ context 'accepts a block with no arguments, using the return value' do
7
+ subject(:mapper) { Mustermann::Mapper.new(additional_values: :raise) {{ "/foo" => "/bar" }}}
8
+ its(:to_h) { should be == { Mustermann.new("/foo") => Mustermann::Expander.new("/bar") } }
9
+ example { mapper['/foo'].should be == '/bar' }
10
+ example { mapper['/fox'].should be == '/fox' }
11
+ end
12
+
13
+ context 'accepts a block with argument, passes instance to it' do
14
+ subject(:mapper) { Mustermann::Mapper.new(additional_values: :raise) { |m| m["/foo"] = "/bar" }}
15
+ its(:to_h) { should be == { Mustermann.new("/foo") => Mustermann::Expander.new("/bar") } }
16
+ example { mapper['/foo'].should be == '/bar' }
17
+ example { mapper['/fox'].should be == '/fox' }
18
+ end
19
+
20
+ context 'accepts mappings followed by options' do
21
+ subject(:mapper) { Mustermann::Mapper.new("/foo" => "/bar", additional_values: :raise) }
22
+ its(:to_h) { should be == { Mustermann.new("/foo") => Mustermann::Expander.new("/bar") } }
23
+ example { mapper['/foo'].should be == '/bar' }
24
+ example { mapper['/fox'].should be == '/fox' }
25
+ end
26
+
27
+ context 'accepts options followed by mappings' do
28
+ subject(:mapper) { Mustermann::Mapper.new(additional_values: :raise, "/foo" => "/bar") }
29
+ its(:to_h) { should be == { Mustermann.new("/foo") => Mustermann::Expander.new("/bar") } }
30
+ example { mapper['/foo'].should be == '/bar' }
31
+ example { mapper['/fox'].should be == '/fox' }
32
+ end
33
+
34
+ context 'allows specifying type' do
35
+ subject(:mapper) { Mustermann::Mapper.new(additional_values: :raise, type: :rails, "/foo" => "/bar") }
36
+ its(:to_h) { should be == { Mustermann.new("/foo", type: :rails) => Mustermann::Expander.new("/bar", type: :rails) } }
37
+ example { mapper['/foo'].should be == '/bar' }
38
+ example { mapper['/fox'].should be == '/fox' }
39
+ end
40
+ end
41
+
42
+ describe :convert do
43
+ subject(:mapper) { Mustermann::Mapper.new }
44
+
45
+ context 'it maps params' do
46
+ before { mapper["/:a"] = "/:a.html" }
47
+ example { mapper["/foo"] .should be == "/foo.html" }
48
+ example { mapper["/foo/bar"] .should be == "/foo/bar" }
49
+ end
50
+
51
+ context 'it supports named splats' do
52
+ before { mapper["/*a"] = "/*a.html" }
53
+ example { mapper["/foo"] .should be == "/foo.html" }
54
+ example { mapper["/foo/bar"] .should be == "/foo/bar.html" }
55
+ end
56
+
57
+ context 'can map from patterns' do
58
+ before { mapper[Mustermann.new("/:a")] = "/:a.html" }
59
+ example { mapper["/foo"] .should be == "/foo.html" }
60
+ example { mapper["/foo/bar"] .should be == "/foo/bar" }
61
+ end
62
+
63
+ context 'can map to patterns' do
64
+ before { mapper[Mustermann.new("/:a")] = Mustermann.new("/:a.html") }
65
+ example { mapper["/foo"] .should be == "/foo.html" }
66
+ example { mapper["/foo/bar"] .should be == "/foo/bar" }
67
+ end
68
+
69
+ context 'can map to expanders' do
70
+ before { mapper[Mustermann.new("/:a")] = Mustermann::Expander.new("/:a.html") }
71
+ example { mapper["/foo"] .should be == "/foo.html" }
72
+ example { mapper["/foo/bar"] .should be == "/foo/bar" }
73
+ end
74
+
75
+ context 'can map to array' do
76
+ before { mapper["/:a"] = ["/:a.html", "/:a.:f"] }
77
+ example { mapper["/foo"] .should be == "/foo.html" }
78
+ example { mapper["/foo", "f" => 'x'] .should be == "/foo.x" }
79
+ example { mapper["/foo", f: 'x'] .should be == "/foo.x" }
80
+ example { mapper["/foo/bar"] .should be == "/foo/bar" }
81
+ end
82
+ end
83
+ end
@@ -5,16 +5,50 @@ require 'sinatra/base'
5
5
 
6
6
  describe Mustermann do
7
7
  describe :new do
8
- example { Mustermann.new('') .should be_a(Mustermann::Sinatra) }
9
- example { Mustermann.new('', type: :identity) .should be_a(Mustermann::Identity) }
10
- example { Mustermann.new('', type: :rails) .should be_a(Mustermann::Rails) }
11
- example { Mustermann.new('', type: :shell) .should be_a(Mustermann::Shell) }
12
- example { Mustermann.new('', type: :sinatra) .should be_a(Mustermann::Sinatra) }
13
- example { Mustermann.new('', type: :simple) .should be_a(Mustermann::Simple) }
14
- example { Mustermann.new('', type: :template) .should be_a(Mustermann::Template) }
15
-
16
- example { expect { Mustermann.new('', foo: :bar) }.to raise_error(ArgumentError, "unsupported option :foo for Mustermann::Sinatra") }
17
- example { expect { Mustermann.new('', type: :ast) }.to raise_error(ArgumentError, "unsupported type :ast") }
8
+ context "string argument" do
9
+ example { Mustermann.new('') .should be_a(Mustermann::Sinatra) }
10
+ example { Mustermann.new('', type: :identity) .should be_a(Mustermann::Identity) }
11
+ example { Mustermann.new('', type: :rails) .should be_a(Mustermann::Rails) }
12
+ example { Mustermann.new('', type: :shell) .should be_a(Mustermann::Shell) }
13
+ example { Mustermann.new('', type: :sinatra) .should be_a(Mustermann::Sinatra) }
14
+ example { Mustermann.new('', type: :simple) .should be_a(Mustermann::Simple) }
15
+ example { Mustermann.new('', type: :template) .should be_a(Mustermann::Template) }
16
+
17
+ example { expect { Mustermann.new('', foo: :bar) }.to raise_error(ArgumentError, "unsupported option :foo for Mustermann::Sinatra") }
18
+ example { expect { Mustermann.new('', type: :ast) }.to raise_error(ArgumentError, "unsupported type :ast (cannot load such file -- mustermann/ast)") }
19
+ end
20
+
21
+ context "pattern argument" do
22
+ subject(:pattern) { Mustermann.new('') }
23
+ example { Mustermann.new(pattern).should be == pattern }
24
+ example { Mustermann.new(pattern, type: :rails).should be_a(Mustermann::Sinatra) }
25
+ end
26
+
27
+ context "regexp argument" do
28
+ example { Mustermann.new(//) .should be_a(Mustermann::Regular) }
29
+ example { Mustermann.new(//, type: :rails) .should be_a(Mustermann::Regular) }
30
+ end
31
+
32
+ context "argument implementing #to_pattern" do
33
+ subject(:pattern) { Class.new { def to_pattern(**o) Mustermann.new('foo', **o) end }.new }
34
+ example { Mustermann.new(pattern) .should be_a(Mustermann::Sinatra) }
35
+ example { Mustermann.new(pattern, type: :rails) .should be_a(Mustermann::Rails) }
36
+ example { Mustermann.new(pattern).to_s.should be == 'foo' }
37
+ end
38
+
39
+ context "multiple arguments" do
40
+ example { Mustermann.new('', '') .should be_a(Mustermann::Composite) }
41
+ example { Mustermann.new('', '').patterns.first .should be_a(Mustermann::Sinatra) }
42
+ example { Mustermann.new('', '').operator .should be == :| }
43
+ example { Mustermann.new('', '', operator: :&).operator .should be == :& }
44
+ example { Mustermann.new('', '', greedy: true) .should be_a(Mustermann::Composite) }
45
+ end
46
+
47
+ context "invalid arguments" do
48
+ it "raise a TypeError for unsupported types" do
49
+ expect { Mustermann.new(10) }.to raise_error(TypeError, "Fixnum can't be coerced into Mustermann::Pattern")
50
+ end
51
+ end
18
52
  end
19
53
 
20
54
  describe :[] do
@@ -25,7 +59,8 @@ describe Mustermann do
25
59
  example { Mustermann[:simple] .should be == Mustermann::Simple }
26
60
  example { Mustermann[:template] .should be == Mustermann::Template }
27
61
 
28
- example { expect { Mustermann[:ast] }.to raise_error(ArgumentError, "unsupported type :ast") }
62
+ example { expect { Mustermann[:ast] }.to raise_error(ArgumentError, "unsupported type :ast (cannot load such file -- mustermann/ast)") }
63
+ example { expect { Mustermann[:expander] }.to raise_error(ArgumentError, "unsupported type :expander") }
29
64
  end
30
65
 
31
66
  describe :extend_object do
@@ -38,4 +73,8 @@ describe Mustermann do
38
73
  example { Sinatra .new.extend(Mustermann).should be_a(Mustermann::Extension) }
39
74
  end
40
75
  end
76
+
77
+ describe :=== do
78
+ example { Mustermann.should be === Mustermann.new("") }
79
+ end
41
80
  end
data/spec/pattern_spec.rb CHANGED
@@ -1,26 +1,53 @@
1
1
  require 'support'
2
2
  require 'mustermann/pattern'
3
+ require 'mustermann/sinatra'
4
+ require 'mustermann/rails'
3
5
 
4
6
  describe Mustermann::Pattern do
5
7
  describe :=== do
6
8
  it 'raises a NotImplementedError when used directly' do
7
- expect { described_class.new("") === "" }.to raise_error(NotImplementedError)
9
+ expect { Mustermann::Pattern.new("") === "" }.to raise_error(NotImplementedError)
8
10
  end
9
11
  end
10
12
 
11
13
  describe :initialize do
12
14
  it 'raises an ArgumentError for unknown options' do
13
- expect { described_class.new("", foo: :bar) }.to raise_error(ArgumentError)
15
+ expect { Mustermann::Pattern.new("", foo: :bar) }.to raise_error(ArgumentError)
14
16
  end
15
17
 
16
18
  it 'does not complain about unknown options if ignore_unknown_options is enabled' do
17
- expect { described_class.new("", foo: :bar, ignore_unknown_options: true) }.not_to raise_error
19
+ expect { Mustermann::Pattern.new("", foo: :bar, ignore_unknown_options: true) }.not_to raise_error
18
20
  end
19
21
  end
20
22
 
21
- describe :expand do
22
- subject(:pattern) { described_class.new("") }
23
- it { should_not respond_to(:expand) }
24
- it { expect { pattern.expand }.to raise_error(NotImplementedError) }
23
+ describe :respond_to? do
24
+ subject(:pattern) { Mustermann::Pattern.new("") }
25
+
26
+ it { should_not respond_to(:expand) }
27
+ it { should_not respond_to(:to_templates) }
28
+
29
+ it { expect { pattern.expand } .to raise_error(NotImplementedError) }
30
+ it { expect { pattern.to_templates } .to raise_error(NotImplementedError) }
31
+ end
32
+
33
+ describe :== do
34
+ example { Mustermann::Pattern.new('/foo') .should be == Mustermann::Pattern.new('/foo') }
35
+ example { Mustermann::Pattern.new('/foo') .should_not be == Mustermann::Pattern.new('/bar') }
36
+ example { Mustermann::Sinatra.new('/foo') .should be == Mustermann::Sinatra.new('/foo') }
37
+ example { Mustermann::Rails.new('/foo') .should_not be == Mustermann::Sinatra.new('/foo') }
38
+ end
39
+
40
+ describe :eql? do
41
+ example { Mustermann::Pattern.new('/foo') .should be_eql Mustermann::Pattern.new('/foo') }
42
+ example { Mustermann::Pattern.new('/foo') .should_not be_eql Mustermann::Pattern.new('/bar') }
43
+ example { Mustermann::Sinatra.new('/foo') .should be_eql Mustermann::Sinatra.new('/foo') }
44
+ example { Mustermann::Rails.new('/foo') .should_not be_eql Mustermann::Sinatra.new('/foo') }
45
+ end
46
+
47
+ describe :equal? do
48
+ example { Mustermann::Pattern.new('/foo') .should be_equal Mustermann::Pattern.new('/foo') }
49
+ example { Mustermann::Pattern.new('/foo') .should_not be_equal Mustermann::Pattern.new('/bar') }
50
+ example { Mustermann::Sinatra.new('/foo') .should be_equal Mustermann::Sinatra.new('/foo') }
51
+ example { Mustermann::Rails.new('/foo') .should_not be_equal Mustermann::Sinatra.new('/foo') }
25
52
  end
26
53
  end
@@ -3,6 +3,6 @@ require 'mustermann/regexp_based'
3
3
 
4
4
  describe Mustermann::RegexpBased do
5
5
  it 'raises a NotImplementedError when used directly' do
6
- expect { described_class.new("") === "" }.to raise_error(NotImplementedError)
6
+ expect { Mustermann::RegexpBased.new("") === "" }.to raise_error(NotImplementedError)
7
7
  end
8
8
  end
@@ -0,0 +1,61 @@
1
+ require 'support'
2
+ require 'mustermann/regular'
3
+
4
+ describe Mustermann::Regular do
5
+ extend Support::Pattern
6
+
7
+ pattern '' do
8
+ it { should match('') }
9
+ it { should_not match('/') }
10
+
11
+ it { should_not respond_to(:expand) }
12
+ it { should_not respond_to(:to_templates) }
13
+ end
14
+
15
+ pattern '/' do
16
+ it { should match('/') }
17
+ it { should_not match('/foo') }
18
+ end
19
+
20
+ pattern '/foo' do
21
+ it { should match('/foo') }
22
+ it { should_not match('/bar') }
23
+ it { should_not match('/foo.bar') }
24
+ end
25
+
26
+ pattern '/foo/bar' do
27
+ it { should match('/foo/bar') }
28
+ it { should_not match('/foo%2Fbar') }
29
+ it { should_not match('/foo%2fbar') }
30
+ end
31
+
32
+ pattern '/(?<foo>.*)' do
33
+ it { should match('/foo') .capturing foo: 'foo' }
34
+ it { should match('/bar') .capturing foo: 'bar' }
35
+ it { should match('/foo.bar') .capturing foo: 'foo.bar' }
36
+ it { should match('/%0Afoo') .capturing foo: '%0Afoo' }
37
+ it { should match('/foo%2Fbar') .capturing foo: 'foo%2Fbar' }
38
+ end
39
+
40
+ context "peeking" do
41
+ subject(:pattern) { Mustermann::Regular.new("(?<name>[^/]+)") }
42
+
43
+ describe :peek_size do
44
+ example { pattern.peek_size("foo bar/blah") .should be == "foo bar".size }
45
+ example { pattern.peek_size("foo%20bar/blah") .should be == "foo%20bar".size }
46
+ example { pattern.peek_size("/foo bar") .should be_nil }
47
+ end
48
+
49
+ describe :peek_match do
50
+ example { pattern.peek_match("foo bar/blah") .to_s .should be == "foo bar" }
51
+ example { pattern.peek_match("foo%20bar/blah") .to_s .should be == "foo%20bar" }
52
+ example { pattern.peek_match("/foo bar") .should be_nil }
53
+ end
54
+
55
+ describe :peek_params do
56
+ example { pattern.peek_params("foo bar/blah") .should be == [{"name" => "foo bar"}, "foo bar".size] }
57
+ example { pattern.peek_params("foo%20bar/blah") .should be == [{"name" => "foo bar"}, "foo%20bar".size] }
58
+ example { pattern.peek_params("/foo bar") .should be_nil }
59
+ end
60
+ end
61
+ end