File: composite_spec.rb

package info (click to toggle)
ruby-mustermann19 0.4.3%2Bgit20160621-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 756 kB
  • ctags: 445
  • sloc: ruby: 7,197; makefile: 3
file content (147 lines) | stat: -rw-r--r-- 6,269 bytes parent folder | download
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
require 'support'
require 'mustermann'

describe Mustermann::Composite do
  describe :new do
    example 'with no argument' do
      expect { Mustermann::Composite.new }.
        to raise_error(ArgumentError, 'cannot create empty composite pattern')
    end

    example 'with one argument' do
      pattern = Mustermann.new('/foo')
      Mustermann::Composite.new(pattern).should be == pattern
    end
  end

  context :| do
    subject(:pattern) { Mustermann.new('/foo/:name', '/:first/:second') }

    describe :== do
      example { subject.should     be == subject                                                       }
      example { subject.should     be == Mustermann.new('/foo/:name', '/:first/:second')               }
      example { subject.should_not be == Mustermann.new('/foo/:name')                                  }
      example { subject.should_not be == Mustermann.new('/foo/:name', '/:first/:second', operator: :&) }
    end

    describe :=== do
      example { subject.should     be === "/foo/bar" }
      example { subject.should     be === "/fox/bar" }
      example { subject.should_not be === "/foo"     }
    end

    describe :params do
      example { subject.params("/foo/bar") .should be == { "name"  => "bar" }                    }
      example { subject.params("/fox/bar") .should be == { "first" => "fox", "second" => "bar" } }
      example { subject.params("/foo")     .should be_nil                                        }
    end

    describe :=== do
      example { subject.should     match("/foo/bar") }
      example { subject.should     match("/fox/bar") }
      example { subject.should_not match("/foo")     }
    end

    describe :expand do
      example { subject.should respond_to(:expand) }
      example { subject.expand(name: 'bar')                 .should be == '/foo/bar' }
      example { subject.expand(first: 'fox', second: 'bar') .should be == '/fox/bar' }

      context "without expandable patterns" do
        subject(:pattern) { Mustermann.new('/foo/:name', '/:first/:second', type: :simple) }
        example { subject.should_not respond_to(:expand) }
        example { expect { subject.expand(name: 'bar') }.to raise_error(NotImplementedError) }
      end
    end

    describe :to_templates do
      example { should respond_to(:to_templates) }
      example { should generate_templates('/foo/{name}', '/{first}/{second}') }

      context "without patterns implementing to_templates" do
        subject(:pattern) { Mustermann.new('/foo/:name', '/:first/:second', type: :simple) }
        example { should_not respond_to(:to_templates) }
        example { expect { subject.to_templates }.to raise_error(NotImplementedError) }
      end
    end
  end

  context :& do
    subject(:pattern) { Mustermann.new('/foo/:name', '/:first/:second', operator: :&) }

    describe :== do
      example { subject.should     be == subject                                                       }
      example { subject.should     be == Mustermann.new('/foo/:name', '/:first/:second', operator: :&) }
      example { subject.should_not be == Mustermann.new('/foo/:name')                                  }
      example { subject.should_not be == Mustermann.new('/foo/:name', '/:first/:second')               }
    end

    describe :=== do
      example { subject.should     be === "/foo/bar" }
      example { subject.should_not be === "/fox/bar" }
      example { subject.should_not be === "/foo"     }
    end

    describe :params do
      example { subject.params("/foo/bar") .should be == { "name"  => "bar" } }
      example { subject.params("/fox/bar") .should be_nil                     }
      example { subject.params("/foo")     .should be_nil                     }
    end

    describe :match do
      example { subject.should     match("/foo/bar") }
      example { subject.should_not match("/fox/bar") }
      example { subject.should_not match("/foo")     }
    end

    describe :expand do
      example { subject.should_not respond_to(:expand) }
      example { expect { subject.expand(name: 'bar') }.to raise_error(NotImplementedError) }
    end
  end

  context :^ do
    subject(:pattern) { Mustermann.new('/foo/:name', '/:first/:second', operator: :^) }

    describe :== do
      example { subject.should     be == subject                                                       }
      example { subject.should_not be == Mustermann.new('/foo/:name', '/:first/:second')               }
      example { subject.should_not be == Mustermann.new('/foo/:name')                                  }
      example { subject.should_not be == Mustermann.new('/foo/:name', '/:first/:second', operator: :&) }
    end

    describe :=== do
      example { subject.should_not be === "/foo/bar" }
      example { subject.should     be === "/fox/bar" }
      example { subject.should_not be === "/foo"     }
    end

    describe :params do
      example { subject.params("/foo/bar") .should be_nil                                        }
      example { subject.params("/fox/bar") .should be == { "first" => "fox", "second" => "bar" } }
      example { subject.params("/foo")     .should be_nil                                        }
    end

    describe :match do
      example { subject.should_not match("/foo/bar") }
      example { subject.should     match("/fox/bar") }
      example { subject.should_not match("/foo")     }
    end

    describe :expand do
      example { subject.should_not respond_to(:expand) }
      example { expect { subject.expand(name: 'bar') }.to raise_error(NotImplementedError) }
    end
  end

  describe :inspect do
    let(:sinatra)  { Mustermann.new('x')                  }
    let(:rails)    { Mustermann.new('x', type: :rails)    }
    let(:identity) { Mustermann.new('x', type: :identity) }

    example { (sinatra | rails)            .inspect.should include('(sinatra:"x" | rails:"x")')                  }
    example { (sinatra ^ rails)            .inspect.should include('(sinatra:"x" ^ rails:"x")')                  }
    example { (sinatra | rails | identity) .inspect.should include('(sinatra:"x" | rails:"x" | identity:"x")')   }
    example { (sinatra | rails & identity) .inspect.should include('(sinatra:"x" | (rails:"x" & identity:"x"))') }
  end
end