Skip to content

Commit bc841cc

Browse files
committed
Generalize should == into should.predicate for any predicate
1 parent 1350b6c commit bc841cc

File tree

6 files changed

+150
-177
lines changed

6 files changed

+150
-177
lines changed

lib/mspec/expectations/expectations.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,15 @@ def self.fail_with(expected, actual)
1818
end
1919
raise SpecExpectationNotMetError, message
2020
end
21+
22+
def self.fail_predicate(receiver, predicate, args, block, result, expectation)
23+
receiver_to_s = MSpec.format(receiver)
24+
before_method = predicate.to_s =~ /^[a-z]/ ? "." : " "
25+
predicate_to_s = "#{before_method}#{predicate}"
26+
predicate_to_s += " " unless args.empty?
27+
args_to_s = args.map { |arg| MSpec.format(arg) }.join(', ')
28+
args_to_s += " { ... }" if block
29+
result_to_s = MSpec.format(result)
30+
raise SpecExpectationNotMetError, "Expected #{receiver_to_s}#{predicate_to_s}#{args_to_s}\n#{expectation} but was #{result_to_s}"
31+
end
2132
end

lib/mspec/matchers/base.rb

Lines changed: 20 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,98 +10,52 @@ class Module
1010
include MSpecMatchers
1111
end
1212

13-
class SpecPositiveOperatorMatcher
13+
class SpecPositiveOperatorMatcher < BasicObject
1414
def initialize(actual)
1515
@actual = actual
1616
end
1717

1818
def ==(expected)
19-
unless @actual == expected
20-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
21-
"to equal #{MSpec.format(expected)}")
22-
end
19+
method_missing(:==, expected)
2320
end
2421

25-
def <(expected)
26-
unless @actual < expected
27-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
28-
"to be less than #{MSpec.format(expected)}")
29-
end
30-
end
31-
32-
def <=(expected)
33-
unless @actual <= expected
34-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
35-
"to be less than or equal to #{MSpec.format(expected)}")
36-
end
22+
def !=(expected)
23+
method_missing(:!=, expected)
3724
end
3825

39-
def >(expected)
40-
unless @actual > expected
41-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
42-
"to be greater than #{MSpec.format(expected)}")
43-
end
44-
end
45-
46-
def >=(expected)
47-
unless @actual >= expected
48-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
49-
"to be greater than or equal to #{MSpec.format(expected)}")
50-
end
26+
def equal?(expected)
27+
method_missing(:equal?, expected)
5128
end
5229

53-
def =~(expected)
54-
unless @actual =~ expected
55-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
56-
"to match #{MSpec.format(expected)}")
30+
def method_missing(name, *args, &block)
31+
result = @actual.__send__(name, *args, &block)
32+
unless result
33+
::SpecExpectation.fail_predicate(@actual, name, args, block, result, "to be truthy")
5734
end
5835
end
5936
end
6037

61-
class SpecNegativeOperatorMatcher
38+
class SpecNegativeOperatorMatcher < BasicObject
6239
def initialize(actual)
6340
@actual = actual
6441
end
6542

6643
def ==(expected)
67-
if @actual == expected
68-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
69-
"not to equal #{MSpec.format(expected)}")
70-
end
44+
method_missing(:==, expected)
7145
end
7246

73-
def <(expected)
74-
if @actual < expected
75-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
76-
"not to be less than #{MSpec.format(expected)}")
77-
end
78-
end
79-
80-
def <=(expected)
81-
if @actual <= expected
82-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
83-
"not to be less than or equal to #{MSpec.format(expected)}")
84-
end
47+
def !=(expected)
48+
method_missing(:!=, expected)
8549
end
8650

87-
def >(expected)
88-
if @actual > expected
89-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
90-
"not to be greater than #{MSpec.format(expected)}")
91-
end
92-
end
93-
94-
def >=(expected)
95-
if @actual >= expected
96-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
97-
"not to be greater than or equal to #{MSpec.format(expected)}")
98-
end
51+
def equal?(expected)
52+
method_missing(:equal?, expected)
9953
end
10054

101-
def =~(expected)
102-
if @actual =~ expected
103-
SpecExpectation.fail_with("Expected #{MSpec.format(@actual)}",
104-
"not to match #{MSpec.format(expected)}")
55+
def method_missing(name, *args, &block)
56+
result = @actual.__send__(name, *args, &block)
57+
if result
58+
::SpecExpectation.fail_predicate(@actual, name, args, block, result, "to be falsy")
10559
end
10660
end
10761
end

spec/expectations/should_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
@out.should include <<-EOS
1515
1)
1616
MSpec expectation method #should causes a failue to be recorded FAILED
17-
Expected 1 to equal 2
17+
Expected 1 == 2
18+
to be truthy but was false
1819
EOS
1920
end
2021

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

spec/integration/run_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
66
1)
77
Foo#bar errors FAILED
8-
Expected 1 to equal 2
8+
Expected 1 == 2
9+
to be truthy but was false
910
CWD/spec/fixtures/a_spec.rb:8:in `block (2 levels) in <top (required)>'
1011
CWD/spec/fixtures/a_spec.rb:2:in `<top (required)>'
1112
CWD/bin/mspec-run:7:in `<main>'

spec/integration/tag_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@
2424
2525
1)
2626
Tag#me errors FAILED
27-
Expected 1 to equal 2
27+
Expected 1 == 2
28+
to be truthy but was false
2829
CWD/spec/fixtures/tagging_spec.rb:9:in `block (2 levels) in <top (required)>'
2930
CWD/spec/fixtures/tagging_spec.rb:3:in `<top (required)>'
3031
CWD/bin/mspec-tag:7:in `<main>'
3132
3233
2)
3334
Tag#me érròrs in unicode FAILED
34-
Expected 1 to equal 2
35+
Expected 1 == 2
36+
to be truthy but was false
3537
CWD/spec/fixtures/tagging_spec.rb:13:in `block (2 levels) in <top (required)>'
3638
CWD/spec/fixtures/tagging_spec.rb:3:in `<top (required)>'
3739
CWD/bin/mspec-tag:7:in `<main>'

0 commit comments

Comments
 (0)