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
|
# frozen_string_literal: true
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
example 'with supported type specific arguments' do
Mustermann::Composite.new("/a", "/b", greedy: true)
end
example 'with unsupported type specific arguments' do
expect { Mustermann::Composite.new("/a", "/b", greedy: true, type: :identity) }.to raise_error(ArgumentError)
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' }
xcontext "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}') }
xcontext "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
describe :eql? do
example { should be_eql(pattern) }
example { should be_eql(Mustermann.new('/foo/:name', '/:first/:second', operator: :|)) }
example { should_not be_eql(Mustermann.new('/bar/:name', '/:first/:second', operator: :|)) }
example { should_not be_eql(Mustermann.new('/foo/:name', '/:first/:second', operator: :&)) }
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
xdescribe :inspect do
let(:sinatra) { Mustermann.new('x') }
let(:shell) { Mustermann.new('x', type: :shell) }
let(:identity) { Mustermann.new('x', type: :identity) }
example { (sinatra | shell) .inspect.should include('(sinatra:"x" | shell:"x")') }
example { (sinatra ^ shell) .inspect.should include('(sinatra:"x" ^ shell:"x")') }
example { (sinatra | shell | identity) .inspect.should include('(sinatra:"x" | shell:"x" | identity:"x")') }
example { (sinatra | shell & identity) .inspect.should include('(sinatra:"x" | (shell:"x" & identity:"x"))') }
end
end
|