Skip to content
Permalink
Browse files

Generalize should == into should.predicate for any predicate

  • Loading branch information...
eregon committed Aug 16, 2019
1 parent 1350b6c commit bc841cc0a4cdc7b93ba55f440ce8047945cf37b2
@@ -18,4 +18,15 @@ def self.fail_with(expected, actual)
end
raise SpecExpectationNotMetError, message
end

def self.fail_predicate(receiver, predicate, args, block, result, expectation)
receiver_to_s = MSpec.format(receiver)
before_method = predicate.to_s =~ /^[a-z]/ ? "." : " "
predicate_to_s = "#{before_method}#{predicate}"
predicate_to_s += " " unless args.empty?
args_to_s = args.map { |arg| MSpec.format(arg) }.join(', ')
args_to_s += " { ... }" if block
result_to_s = MSpec.format(result)
raise SpecExpectationNotMetError, "Expected #{receiver_to_s}#{predicate_to_s}#{args_to_s}\n#{expectation} but was #{result_to_s}"
end
end
@@ -10,98 +10,52 @@ class Module
include MSpecMatchers
end

class SpecPositiveOperatorMatcher
class SpecPositiveOperatorMatcher < BasicObject
def initialize(actual)
@actual = actual
end

def ==(expected)
unless @actual == expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"to equal #{MSpec.format(expected)}")
end
method_missing(:==, expected)
end

def <(expected)
unless @actual < expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"to be less than #{MSpec.format(expected)}")
end
end

def <=(expected)
unless @actual <= expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"to be less than or equal to #{MSpec.format(expected)}")
end
def !=(expected)
method_missing(:!=, expected)
end

def >(expected)
unless @actual > expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"to be greater than #{MSpec.format(expected)}")
end
end

def >=(expected)
unless @actual >= expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"to be greater than or equal to #{MSpec.format(expected)}")
end
def equal?(expected)
method_missing(:equal?, expected)
end

def =~(expected)
unless @actual =~ expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"to match #{MSpec.format(expected)}")
def method_missing(name, *args, &block)
result = @actual.__send__(name, *args, &block)
unless result
::SpecExpectation.fail_predicate(@actual, name, args, block, result, "to be truthy")
end
end
end

class SpecNegativeOperatorMatcher
class SpecNegativeOperatorMatcher < BasicObject
def initialize(actual)
@actual = actual
end

def ==(expected)
if @actual == expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"not to equal #{MSpec.format(expected)}")
end
method_missing(:==, expected)
end

def <(expected)
if @actual < expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"not to be less than #{MSpec.format(expected)}")
end
end

def <=(expected)
if @actual <= expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"not to be less than or equal to #{MSpec.format(expected)}")
end
def !=(expected)
method_missing(:!=, expected)
end

def >(expected)
if @actual > expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"not to be greater than #{MSpec.format(expected)}")
end
end

def >=(expected)
if @actual >= expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"not to be greater than or equal to #{MSpec.format(expected)}")
end
def equal?(expected)
method_missing(:equal?, expected)
end

def =~(expected)
if @actual =~ expected
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
"not to match #{MSpec.format(expected)}")
def method_missing(name, *args, &block)
result = @actual.__send__(name, *args, &block)
if result
::SpecExpectation.fail_predicate(@actual, name, args, block, result, "to be falsy")
end
end
end
@@ -14,7 +14,8 @@
@out.should include <<-EOS
1)
MSpec expectation method #should causes a failue to be recorded FAILED
Expected 1 to equal 2
Expected 1 == 2
to be truthy but was false
EOS
end

@@ -32,7 +33,8 @@
@out.should include <<-EOS
3)
MSpec expectation method #should_not causes a failure to be recorded FAILED
Expected 1 not to equal 1
Expected 1 == 1
to be falsy but was true
EOS
end

@@ -5,7 +5,8 @@
1)
Foo#bar errors FAILED
Expected 1 to equal 2
Expected 1 == 2
to be truthy but was false
CWD/spec/fixtures/a_spec.rb:8:in `block (2 levels) in <top (required)>'
CWD/spec/fixtures/a_spec.rb:2:in `<top (required)>'
CWD/bin/mspec-run:7:in `<main>'
@@ -24,14 +24,16 @@

1)
Tag#me errors FAILED
Expected 1 to equal 2
Expected 1 == 2
to be truthy but was false
CWD/spec/fixtures/tagging_spec.rb:9:in `block (2 levels) in <top (required)>'
CWD/spec/fixtures/tagging_spec.rb:3:in `<top (required)>'
CWD/bin/mspec-tag:7:in `<main>'

2)
Tag#me érròrs in unicode FAILED
Expected 1 to equal 2
Expected 1 == 2
to be truthy but was false
CWD/spec/fixtures/tagging_spec.rb:13:in `block (2 levels) in <top (required)>'
CWD/spec/fixtures/tagging_spec.rb:3:in `<top (required)>'
CWD/bin/mspec-tag:7:in `<main>'

0 comments on commit bc841cc

Please sign in to comment.
You can’t perform that action at this time.