Skip to content

Commit 3aa5de6

Browse files
committed
Use literal calls for equal?, == and !=
* This avoids bugs like https://bugs.ruby-lang.org/issues/18671 * Might also make these common matchers slightly faster
1 parent d2a9ae8 commit 3aa5de6

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

spec/mspec/lib/mspec/expectations/expectations.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ def self.fail_predicate(receiver, predicate, args, block, result, expectation)
3232
result_to_s = MSpec.format(result)
3333
raise SpecExpectationNotMetError, "Expected #{receiver_to_s}#{predicate_to_s}#{args_to_s}\n#{expectation} but was #{result_to_s}"
3434
end
35+
36+
def self.fail_single_arg_predicate(receiver, predicate, arg, result, expectation)
37+
fail_predicate(receiver, predicate, [arg], nil, result, expectation)
38+
end
3539
end

spec/mspec/lib/mspec/matchers/base.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@ def initialize(actual)
1616
end
1717

1818
def ==(expected)
19-
method_missing(:==, expected)
19+
result = @actual == expected
20+
unless result
21+
::SpecExpectation.fail_single_arg_predicate(@actual, :==, expected, result, "to be truthy")
22+
end
2023
end
2124

2225
def !=(expected)
23-
method_missing(:!=, expected)
26+
result = @actual != expected
27+
unless result
28+
::SpecExpectation.fail_single_arg_predicate(@actual, :!=, expected, result, "to be truthy")
29+
end
2430
end
2531

2632
def equal?(expected)
27-
method_missing(:equal?, expected)
33+
result = @actual.equal?(expected)
34+
unless result
35+
::SpecExpectation.fail_single_arg_predicate(@actual, :equal?, expected, result, "to be truthy")
36+
end
2837
end
2938

3039
def method_missing(name, *args, &block)
@@ -41,15 +50,24 @@ def initialize(actual)
4150
end
4251

4352
def ==(expected)
44-
method_missing(:==, expected)
53+
result = @actual == expected
54+
if result
55+
::SpecExpectation.fail_single_arg_predicate(@actual, :==, expected, result, "to be falsy")
56+
end
4557
end
4658

4759
def !=(expected)
48-
method_missing(:!=, expected)
60+
result = @actual != expected
61+
if result
62+
::SpecExpectation.fail_single_arg_predicate(@actual, :!=, expected, result, "to be falsy")
63+
end
4964
end
5065

5166
def equal?(expected)
52-
method_missing(:equal?, expected)
67+
result = @actual.equal?(expected)
68+
if result
69+
::SpecExpectation.fail_single_arg_predicate(@actual, :equal?, expected, result, "to be falsy")
70+
end
5371
end
5472

5573
def method_missing(name, *args, &block)

0 commit comments

Comments
 (0)