| 1 |
class MethodOverrideObject |
| 2 |
def method |
| 3 |
:foo |
| 4 |
end |
| 5 |
end |
| 6 |
|
| 7 |
class MethodMissingObject < Struct.new(:original) |
| 8 |
undef == |
| 9 |
|
| 10 |
def method_missing(name, *args, &block) |
| 11 |
original.__send__ name, *args, &block |
| 12 |
end |
| 13 |
end |
| 14 |
|
| 15 |
RSpec.describe "operator matchers", :uses_should do |
| 16 |
describe "should ==" do |
| 17 |
it "delegates message to target" do |
| 18 |
subject = "apple".dup |
| 19 |
expect(subject).to receive(:==).with("apple").and_return(true) |
| 20 |
subject.should == "apple" |
| 21 |
end |
| 22 |
|
| 23 |
it "returns true on success" do |
| 24 |
subject = "apple" |
| 25 |
(subject.should == "apple").should be_truthy |
| 26 |
end |
| 27 |
|
| 28 |
it "fails when target.==(actual) returns false" do |
| 29 |
subject = "apple" |
| 30 |
expect(RSpec::Expectations).to receive(:fail_with).with(%{expected: "orange"\n got: "apple" (using ==)}, "orange", "apple") |
| 31 |
subject.should == "orange" |
| 32 |
end |
| 33 |
|
| 34 |
it "works when #method is overridden" do |
| 35 |
myobj = MethodOverrideObject.new |
| 36 |
expect { |
| 37 |
myobj.should == myobj |
| 38 |
}.to_not raise_error |
| 39 |
end |
| 40 |
|
| 41 |
it "works when implemented via method_missing" do |
| 42 |
obj = Object.new |
| 43 |
|
| 44 |
myobj = MethodMissingObject.new(obj) |
| 45 |
(myobj.should == obj).nil? |
| 46 |
myobj.should_not == Object.new |
| 47 |
end |
| 48 |
end |
| 49 |
|
| 50 |
describe "unsupported operators", :if => RUBY_VERSION.to_f == 1.9 do |
| 51 |
it "raises an appropriate error for should != expected" do |
| 52 |
expect { |
| 53 |
"apple".should != "pear" |
| 54 |
}.to raise_error(/does not support `should != expected`. Use `should_not == expected`/) |
| 55 |
end |
| 56 |
|
| 57 |
it "raises an appropriate error for should_not != expected" do |
| 58 |
expect { |
| 59 |
"apple".should_not != "pear" |
| 60 |
}.to raise_error(/does not support `should_not != expected`. Use `should == expected`/) |
| 61 |
end |
| 62 |
|
| 63 |
it "raises an appropriate error for should !~ expected" do |
| 64 |
expect { |
| 65 |
"apple".should !~ /regex/ |
| 66 |
}.to raise_error(/does not support `should !~ expected`. Use `should_not =~ expected`/) |
| 67 |
end |
| 68 |
|
| 69 |
it "raises an appropriate error for should_not !~ expected" do |
| 70 |
expect { |
| 71 |
"apple".should_not !~ /regex/ |
| 72 |
}.to raise_error(/does not support `should_not !~ expected`. Use `should =~ expected`/) |
| 73 |
end |
| 74 |
end |
| 75 |
|
| 76 |
describe "should_not ==" do |
| 77 |
it "delegates message to target" do |
| 78 |
subject = "orange".dup |
| 79 |
expect(subject).to receive(:==).with("apple").and_return(false) |
| 80 |
subject.should_not == "apple" |
| 81 |
end |
| 82 |
|
| 83 |
it "returns true on success" do |
| 84 |
subject = "apple" |
| 85 |
(subject.should_not == "orange").should be_falsey |
| 86 |
end |
| 87 |
|
| 88 |
it "fails when target.==(actual) returns false" do |
| 89 |
subject = "apple" |
| 90 |
expect(RSpec::Expectations).to receive(:fail_with).with(%(expected not: == "apple"\n got: "apple"), "apple", "apple") |
| 91 |
subject.should_not == "apple" |
| 92 |
end |
| 93 |
end |
| 94 |
|
| 95 |
describe "should ===" do |
| 96 |
it "delegates message to target" do |
| 97 |
subject = "apple".dup |
| 98 |
expect(subject).to receive(:===).with("apple").and_return(true) |
| 99 |
subject.should === "apple" |
| 100 |
end |
| 101 |
|
| 102 |
it "fails when target.===(actual) returns false" do |
| 103 |
subject = "apple".dup |
| 104 |
expect(subject).to receive(:===).with("orange").and_return(false) |
| 105 |
expect(RSpec::Expectations).to receive(:fail_with).with(%{expected: "orange"\n got: "apple" (using ===)}, "orange", "apple") |
| 106 |
subject.should === "orange" |
| 107 |
end |
| 108 |
end |
| 109 |
|
| 110 |
describe "should_not ===" do |
| 111 |
it "delegates message to target" do |
| 112 |
subject = "orange".dup |
| 113 |
expect(subject).to receive(:===).with("apple").and_return(false) |
| 114 |
subject.should_not === "apple" |
| 115 |
end |
| 116 |
|
| 117 |
it "fails when target.===(actual) returns false" do |
| 118 |
subject = "apple".dup |
| 119 |
expect(subject).to receive(:===).with("apple").and_return(true) |
| 120 |
expect(RSpec::Expectations).to receive(:fail_with).with(%(expected not: === "apple"\n got: "apple"), "apple", "apple") |
| 121 |
subject.should_not === "apple" |
| 122 |
end |
| 123 |
end |
| 124 |
|
| 125 |
describe "should =~" do |
| 126 |
it "delegates message to target" do |
| 127 |
subject = "foo".dup |
| 128 |
expect(subject).to receive(:=~).with(/oo/).and_return(true) |
| 129 |
subject.should =~ /oo/ |
| 130 |
end |
| 131 |
|
| 132 |
it "fails when target.=~(actual) returns false" do |
| 133 |
subject = "fu".dup |
| 134 |
expect(subject).to receive(:=~).with(/oo/).and_return(false) |
| 135 |
expect(RSpec::Expectations).to receive(:fail_with).with(%{expected: /oo/\n got: "fu" (using =~)}, /oo/, "fu") |
| 136 |
subject.should =~ /oo/ |
| 137 |
end |
| 138 |
end |
| 139 |
|
| 140 |
describe "should_not =~" do |
| 141 |
it "delegates message to target" do |
| 142 |
subject = "fu".dup |
| 143 |
expect(subject).to receive(:=~).with(/oo/).and_return(false) |
| 144 |
subject.should_not =~ /oo/ |
| 145 |
end |
| 146 |
|
| 147 |
it "fails when target.=~(actual) returns false" do |
| 148 |
subject = "foo".dup |
| 149 |
expect(subject).to receive(:=~).with(/oo/).and_return(true) |
| 150 |
expect(RSpec::Expectations).to receive(:fail_with).with(%(expected not: =~ /oo/\n got: "foo"), /oo/, "foo") |
| 151 |
subject.should_not =~ /oo/ |
| 152 |
end |
| 153 |
end |
| 154 |
|
| 155 |
describe "should >" do |
| 156 |
it "passes if > passes" do |
| 157 |
4.should > 3 |
| 158 |
end |
| 159 |
|
| 160 |
it "fails if > fails" do |
| 161 |
expect(RSpec::Expectations).to receive(:fail_with).with("expected: > 5\n got: 4", 5, 4) |
| 162 |
4.should > 5 |
| 163 |
end |
| 164 |
end |
| 165 |
|
| 166 |
describe "should >=" do |
| 167 |
it "passes if actual == expected" do |
| 168 |
4.should >= 4 |
| 169 |
end |
| 170 |
|
| 171 |
it "passes if actual > expected" do |
| 172 |
4.should >= 3 |
| 173 |
end |
| 174 |
|
| 175 |
it "fails if > fails" do |
| 176 |
expect(RSpec::Expectations).to receive(:fail_with).with("expected: >= 5\n got: 4", 5, 4) |
| 177 |
4.should >= 5 |
| 178 |
end |
| 179 |
end |
| 180 |
|
| 181 |
describe "should <" do |
| 182 |
it "passes if < passes" do |
| 183 |
4.should < 5 |
| 184 |
end |
| 185 |
|
| 186 |
it "fails if > fails" do |
| 187 |
expect(RSpec::Expectations).to receive(:fail_with).with("expected: < 3\n got: 4", 3, 4) |
| 188 |
4.should < 3 |
| 189 |
end |
| 190 |
end |
| 191 |
|
| 192 |
describe "should <=" do |
| 193 |
it "passes if actual == expected" do |
| 194 |
4.should <= 4 |
| 195 |
end |
| 196 |
|
| 197 |
it "passes if actual < expected" do |
| 198 |
4.should <= 5 |
| 199 |
end |
| 200 |
|
| 201 |
it "fails if > fails" do |
| 202 |
expect(RSpec::Expectations).to receive(:fail_with).with("expected: <= 3\n got: 4", 3, 4) |
| 203 |
4.should <= 3 |
| 204 |
end |
| 205 |
end |
| 206 |
|
| 207 |
describe "OperatorMatcher registry" do |
| 208 |
let(:custom_klass) { Class.new } |
| 209 |
let(:custom_subklass) { Class.new(custom_klass) } |
| 210 |
|
| 211 |
after { |
| 212 |
RSpec::Matchers::BuiltIn::OperatorMatcher.unregister(custom_klass, "=~") |
| 213 |
} |
| 214 |
|
| 215 |
it "allows operator matchers to be registered for classes" do |
| 216 |
RSpec::Matchers::BuiltIn::OperatorMatcher.register(custom_klass, "=~", RSpec::Matchers::BuiltIn::Match) |
| 217 |
expect(RSpec::Matchers::BuiltIn::OperatorMatcher.get(custom_klass, "=~")).to eq(RSpec::Matchers::BuiltIn::Match) |
| 218 |
end |
| 219 |
|
| 220 |
it "considers ancestors when finding an operator matcher" do |
| 221 |
RSpec::Matchers::BuiltIn::OperatorMatcher.register(custom_klass, "=~", RSpec::Matchers::BuiltIn::Match) |
| 222 |
expect(RSpec::Matchers::BuiltIn::OperatorMatcher.get(custom_subklass, "=~")).to eq(RSpec::Matchers::BuiltIn::Match) |
| 223 |
end |
| 224 |
|
| 225 |
it "returns nil if there is no matcher registered for a class" do |
| 226 |
expect(RSpec::Matchers::BuiltIn::OperatorMatcher.get(custom_klass, "=~")).to be_nil |
| 227 |
end |
| 228 |
end |
| 229 |
|
| 230 |
describe RSpec::Matchers::BuiltIn::PositiveOperatorMatcher do |
| 231 |
let(:o) { Object.new } |
| 232 |
before { def o.send(*_args); raise "DOH! Library developers shouldn't use #send!"; end } |
| 233 |
|
| 234 |
it "works when the target has implemented #send" do |
| 235 |
expect { |
| 236 |
o.should == o |
| 237 |
}.not_to raise_error |
| 238 |
end |
| 239 |
|
| 240 |
it "complains when negated", :skip => RUBY_VERSION.to_f < 1.9 do |
| 241 |
expect { |
| 242 |
o.should !~ o |
| 243 |
}.to raise_error(/does not support `should !~ .*Use `should_not =~/) |
| 244 |
end |
| 245 |
|
| 246 |
it "is described correctly" do |
| 247 |
matcher = 7.should |
| 248 |
expect(matcher == 7).to be_truthy |
| 249 |
expect(matcher.description).to eq("== 7") |
| 250 |
end |
| 251 |
end |
| 252 |
|
| 253 |
describe RSpec::Matchers::BuiltIn::NegativeOperatorMatcher do |
| 254 |
let(:o) { Object.new } |
| 255 |
before { def o.send(*_args); raise "DOH! Library developers shouldn't use #send!"; end } |
| 256 |
|
| 257 |
it "works when the target has implemented #send" do |
| 258 |
expect { |
| 259 |
o.should_not == :foo |
| 260 |
}.not_to raise_error |
| 261 |
end |
| 262 |
|
| 263 |
it "complains when negated", :skip => RUBY_VERSION.to_f < 1.9 do |
| 264 |
expect { |
| 265 |
o.should_not !~ :foo |
| 266 |
}.to raise_error(/does not support `should_not !~ .*Use `should =~/) |
| 267 |
end |
| 268 |
|
| 269 |
it "is described correctly" do |
| 270 |
matcher = 7.should_not |
| 271 |
expect(matcher == 8).to be_falsey |
| 272 |
expect(matcher.description).to eq("== 8") |
| 273 |
end |
| 274 |
end |
| 275 |
end |