Skip to content

Commit

Permalink
Draft: Rewrite BeInstanceOfExpectation to match mspec behaviour
Browse files Browse the repository at this point in the history
I don't really like the result. We should probably split up the matcher
and the expectation and create more generic code.
  • Loading branch information
herwinw committed Jan 18, 2025
1 parent b358ea6 commit 0bf93a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
17 changes: 0 additions & 17 deletions test/mspec/spec/matchers/be_an_instance_of_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
require 'spec_helper'
#require 'mspec/expectations/expectations'
#require 'mspec/matchers'
class BeAnInstanceOfMatcher
attr_reader :failure_message, :negative_failure_message

def initialize(klass)
@wrapped = BeInstanceOfExpectation.new(klass)
@klass = klass
end

def matches?(subject)
@wrapped.match(subject)
true
rescue SpecFailedException => e
@failure_message = ["Expected #{subject.inspect} (#{subject.class})", "to be an instance of #{@klass}"]
@negative_failure_message = ["Expected #{subject.inspect} (#{subject.class})", "not to be an instance of #{@klass}"]
nil
end
end

module BeAnInOfSpecs
class A
Expand Down
21 changes: 17 additions & 4 deletions test/support/spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -567,17 +567,30 @@ def inverted_match(subject)
end
end

class BeInstanceOfExpectation
class BeAnInstanceOfMatcher
def initialize(klass)
@klass = klass
end

def match(subject)
raise SpecFailedException, "#{subject.inspect} (#{subject.class}) should be an instance of #{@klass}" if !subject.instance_of?(@klass)
raise SpecFailedException, failure_message.join(' ') unless matches?(subject)
end

def inverted_match(subject)
raise SpecFailedException, "#{subject.inspect} (#{subject.class}) should not be an instance of #{@klass}" if subject.instance_of?(@klass)
raise SpecFailedException, negative_failure_message.join(' ') if matched?(subject)
end

def matches?(subject)
@message_prelude = "Expected #{subject.inspect} (#{subject.class})"
subject.instance_of?(@klass)
end

def failure_message
[@message_prelude, "to be an instance of #{@klass}"]
end

def negative_failure_message
[@message_prelude, "not to be an instance of #{@klass}"]
end
end

Expand Down Expand Up @@ -1500,7 +1513,7 @@ def be_kind_of(klass)
end

def be_an_instance_of(klass)
BeInstanceOfExpectation.new(klass)
BeAnInstanceOfMatcher.new(klass)
end

def be_ancestor_of(klass)
Expand Down

0 comments on commit 0bf93a7

Please sign in to comment.