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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
|
require 'spec_helper'
class UnexpectedError < StandardError; end
module MatcherHelperModule
def self.included(base)
base.module_eval do
def included_method; end
end
end
def self.extended(base)
base.instance_eval do
def extended_method; end
end
end
def greeting
"Hello, World"
end
end
module RSpec::Matchers::DSL
describe Matcher do
it "can be stored aside and used later" do
# Supports using rspec-expectation matchers as argument matchers in
# rspec-mocks.
RSpec::Matchers.define :example_matcher do |expected|
match do |actual|
actual == expected
end
end
m1 = example_matcher(1)
m2 = example_matcher(2)
expect(m1.matches?(1)).to be_true
expect(m2.matches?(2)).to be_true
end
context "with an included module" do
let(:matcher) do
RSpec::Matchers::DSL::Matcher.new(:be_a_greeting) do
include MatcherHelperModule
match { |actual| actual == greeting }
end.for_expected
end
it "has access to the module's methods" do
matcher.matches?("Hello, World")
end
it "runs the module's included hook" do
expect(matcher).to respond_to(:included_method)
end
it "does not run the module's extended hook" do
expect(matcher).not_to respond_to(:extended_method)
end
it 'allows multiple modules to be included at once' do
m = RSpec::Matchers::DSL::Matcher.new(:multiple_modules) do
include Enumerable, Comparable
end.for_expected
expect(m).to be_a(Enumerable)
expect(m).to be_a(Comparable)
end
end
context "without overrides" do
before(:each) do
@matcher = RSpec::Matchers::DSL::Matcher.new(:be_a_multiple_of) do |multiple|
match do |actual|
actual % multiple == 0
end
end.for_expected(3)
end
it "provides a default description" do
expect(@matcher.description).to eq "be a multiple of 3"
end
it "provides a default failure message for #should" do
@matcher.matches?(8)
expect(@matcher.failure_message_for_should).to eq "expected 8 to be a multiple of 3"
end
it "provides a default failure message for #should_not" do
@matcher.matches?(9)
expect(@matcher.failure_message_for_should_not).to eq "expected 9 not to be a multiple of 3"
end
end
context "with separate match logic for should and should not" do
let(:matcher) do
RSpec::Matchers::DSL::Matcher.new(:to_be_composed_of) do |a, b|
match_for_should do |actual|
actual == a * b
end
match_for_should_not do |actual|
actual == a + b
end
end.for_expected(7, 11)
end
it "invokes the match_for_should block for #matches?" do
expect(matcher.matches?(77)).to be_true
expect(matcher.matches?(18)).to be_false
end
it "invokes the match_for_should_not block for #does_not_match?" do
expect(matcher.does_not_match?(77)).to be_false
expect(matcher.does_not_match?(18)).to be_true
end
it "provides a default failure message for #should_not" do
matcher.does_not_match?(77)
expect(matcher.failure_message_for_should_not).to eq "expected 77 not to to be composed of 7 and 11"
end
end
it "allows helper methods to be defined with #define_method to have access to matcher parameters" do
matcher = RSpec::Matchers::DSL::Matcher.new(:name) do |a, b|
define_method(:sum) { a + b }
end.for_expected(3,4)
expect(matcher.sum).to eq 7
end
it "is not diffable by default" do
matcher = RSpec::Matchers::DSL::Matcher.new(:name) {}
expect(matcher).not_to be_diffable
end
it "is diffable when told to be" do
matcher = RSpec::Matchers::DSL::Matcher.new(:name) { diffable }.for_expected
expect(matcher).to be_diffable
end
it "provides expected" do
matcher = RSpec::Matchers::DSL::Matcher.new(:name) {}.for_expected('expected string')
expect(matcher.expected).to eq ['expected string']
end
it "provides actual" do
matcher = RSpec::Matchers::DSL::Matcher.new(:name) do
match {|actual|}
end.for_expected('expected string')
matcher.matches?('actual string')
expect(matcher.actual).to eq 'actual string'
end
context "wrapping another expectation (should == ...)" do
it "returns true if the wrapped expectation passes" do
matcher = RSpec::Matchers::DSL::Matcher.new(:name) do |expected|
match do |actual|
expect(actual).to eq expected
end
end.for_expected('value')
expect(matcher.matches?('value')).to be_true
end
it "returns false if the wrapped expectation fails" do
matcher = RSpec::Matchers::DSL::Matcher.new(:name) do |expected|
match do |actual|
expect(actual).to eq expected
end
end.for_expected('value')
expect(matcher.matches?('other value')).to be_false
end
end
context "with overrides" do
before(:each) do
@matcher = RSpec::Matchers::DSL::Matcher.new(:be_boolean) do |boolean|
match do |actual|
actual
end
description do
"be the boolean #{boolean}"
end
failure_message_for_should do |actual|
"expected #{actual} to be the boolean #{boolean}"
end
failure_message_for_should_not do |actual|
"expected #{actual} not to be the boolean #{boolean}"
end
end.for_expected(true)
end
it "does not hide result of match block when true" do
expect(@matcher.matches?(true)).to be_true
end
it "does not hide result of match block when false" do
expect(@matcher.matches?(false)).to be_false
end
it "overrides the description" do
expect(@matcher.description).to eq "be the boolean true"
end
it "overrides the failure message for #should" do
@matcher.matches?(false)
expect(@matcher.failure_message_for_should).to eq "expected false to be the boolean true"
end
it "overrides the failure message for #should_not" do
@matcher.matches?(true)
expect(@matcher.failure_message_for_should_not).to eq "expected true not to be the boolean true"
end
end
context "#new" do
it "passes matches? arg to match block" do
matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) do
match do |actual|
actual == 5
end
end.for_expected
expect(matcher.matches?(5)).to be_true
end
it "exposes arg submitted through #new to matcher block" do
matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) do |expected|
match do |actual|
actual > expected
end
end.for_expected(4)
expect(matcher.matches?(5)).to be_true
end
end
context "with no args" do
before(:each) do
@matcher = RSpec::Matchers::DSL::Matcher.new(:matcher_name) do
match do |actual|
actual == 5
end
end.for_expected
end
it "matches" do
expect(@matcher.matches?(5)).to be_true
end
it "describes" do
expect(@matcher.description).to eq "matcher name"
end
end
context "with 1 arg" do
before(:each) do
@matcher = RSpec::Matchers::DSL::Matcher.new(:matcher_name) do |expected|
match do |actual|
actual == 5 && expected == 1
end
end.for_expected(1)
end
it "matches" do
expect(@matcher.matches?(5)).to be_true
end
it "describes" do
expect(@matcher.description).to eq "matcher name 1"
end
end
context "with multiple args" do
before(:each) do
@matcher = RSpec::Matchers::DSL::Matcher.new(:matcher_name) do |a,b,c,d|
match do |sum|
a + b + c + d == sum
end
end.for_expected(1,2,3,4)
end
it "matches" do
expect(@matcher.matches?(10)).to be_true
end
it "describes" do
expect(@matcher.description).to eq "matcher name 1, 2, 3, and 4"
end
end
it "supports helper methods" do
matcher = RSpec::Matchers::DSL::Matcher.new(:be_similar_to) do |sample|
match do |actual|
similar?(sample, actual)
end
def similar?(a, b)
a.sort == b.sort
end
end.for_expected([1,2,3])
expect(matcher.matches?([2,3,1])).to be_true
end
it "supports fluent interface" do
matcher = RSpec::Matchers::DSL::Matcher.new(:first_word) do
def second_word
self
end
end.for_expected
expect(matcher.second_word).to eq matcher
end
it "treats method missing normally for undeclared methods" do
matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) { }.for_expected
expect { matcher.non_existent_method }.to raise_error(NoMethodError)
end
it "has access to other matchers" do
matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) do |expected|
match do |actual|
extend RSpec::Matchers
expect(actual).to eql(5 + expected)
end
end.for_expected(3)
expect(matcher.matches?(8)).to be_true
end
context 'when multiple instances of the same matcher are used in the same example' do
RSpec::Matchers.define(:be_like_a) do |expected|
match { |actual| actual == expected }
description { "be like a #{expected}" }
failure_message_for_should { "expected to be like a #{expected}" }
failure_message_for_should_not { "expected not to be like a #{expected}" }
end
# Note: these bugs were only exposed when creating both instances
# first, then checking their descriptions/failure messages.
#
# That's why we eager-instantiate them here.
let!(:moose) { be_like_a("moose") }
let!(:horse) { be_like_a("horse") }
it 'allows them to use the expected value in the description' do
expect(horse.description).to eq("be like a horse")
expect(moose.description).to eq("be like a moose")
end
it 'allows them to use the expected value in the positive failure message' do
expect(moose.failure_message_for_should).to eq("expected to be like a moose")
expect(horse.failure_message_for_should).to eq("expected to be like a horse")
end
it 'allows them to use the expected value in the negative failure message' do
expect(moose.failure_message_for_should_not).to eq("expected not to be like a moose")
expect(horse.failure_message_for_should_not).to eq("expected not to be like a horse")
end
it 'allows them to match separately' do
expect("moose").to moose
expect("horse").to horse
expect("horse").not_to moose
expect("moose").not_to horse
end
end
describe "#match_unless_raises" do
context "with an assertion" do
let(:mod) do
Module.new do
def assert_equal(a,b)
a == b ? nil : (raise UnexpectedError.new("#{b} does not equal #{a}"))
end
end
end
let(:matcher) do
m = mod
RSpec::Matchers::DSL::Matcher.new :equal do |expected|
extend m
match_unless_raises UnexpectedError do
assert_equal expected, actual
end
end.for_expected(4)
end
context "with passing assertion" do
it "passes" do
expect(matcher.matches?(4)).to be_true
end
end
context "with failing assertion" do
it "fails" do
expect(matcher.matches?(5)).to be_false
end
it "provides the raised exception" do
matcher.matches?(5)
expect(matcher.rescued_exception.message).to eq("5 does not equal 4")
end
end
end
context "with an unexpected error" do
let(:matcher) do
RSpec::Matchers::DSL::Matcher.new :foo do |expected|
match_unless_raises SyntaxError do |actual|
raise "unexpected exception"
end
end.for_expected(:bar)
end
it "raises the error" do
expect do
matcher.matches?(:bar)
end.to raise_error("unexpected exception")
end
end
end
it "can define chainable methods" do
matcher = RSpec::Matchers::DSL::Matcher.new(:name) do
chain(:expecting) do |expected_value|
@expected_value = expected_value
end
match { |actual| actual == @expected_value }
end.for_expected
expect(matcher.expecting('value').matches?('value')).to be_true
expect(matcher.expecting('value').matches?('other value')).to be_false
end
it "prevents name collisions on chainable methods from different matchers" do
m1 = RSpec::Matchers::DSL::Matcher.new(:m1) { chain(:foo) { raise "foo in m1" } }.for_expected
m2 = RSpec::Matchers::DSL::Matcher.new(:m2) { chain(:foo) { raise "foo in m2" } }.for_expected
expect { m1.foo }.to raise_error("foo in m1")
expect { m2.foo }.to raise_error("foo in m2")
end
context "defined using the dsl" do
def a_method_in_the_example
"method defined in the example"
end
it "can access methods in the running example" do
RSpec::Matchers.define(:__access_running_example) do
match do |actual|
a_method_in_the_example == "method defined in the example"
end
end
expect(example).to __access_running_example
end
it "raises NoMethodError for methods not in the running_example" do
RSpec::Matchers.define(:__raise_no_method_error) do
match do |actual|
a_method_not_in_the_example == "method defined in the example"
end
end
expect do
expect(example).to __raise_no_method_error
end.to raise_error(/RSpec::Matchers::DSL::Matcher/)
end
end
end
end
|