Skip to content

Commit 8a6712a

Browse files
committed
Support has_* matcher
1 parent fcf1ea2 commit 8a6712a

File tree

5 files changed

+599
-0
lines changed

5 files changed

+599
-0
lines changed

lib/super_diff/rspec/matcher_text_builders.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ module MatcherTextBuilders
1010
:ContainExactly,
1111
"super_diff/rspec/matcher_text_builders/contain_exactly",
1212
)
13+
autoload(
14+
:HavePredicate,
15+
"super_diff/rspec/matcher_text_builders/have_predicate",
16+
)
1317
autoload :Match, "super_diff/rspec/matcher_text_builders/match"
1418
autoload(
1519
:RaiseError,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module SuperDiff
2+
module RSpec
3+
module MatcherTextBuilders
4+
class HavePredicate < Base
5+
def initialize(predicate_accessible:, private_predicate:, **rest)
6+
super(**rest)
7+
@predicate_accessible = predicate_accessible
8+
@private_predicate = private_predicate
9+
end
10+
11+
protected
12+
13+
def expected_action_for_failure_message
14+
if predicate_accessible?
15+
"return a truthy result for"
16+
elsif private_predicate?
17+
"have a public method"
18+
else
19+
"respond to"
20+
end
21+
end
22+
23+
def beta_color
24+
:yellow
25+
end
26+
27+
def add_actual_value
28+
template.add_text_in_color(beta_color) do
29+
description_of(actual)
30+
end
31+
end
32+
33+
def add_expected_value_to_failure_message(template)
34+
template.add_text " "
35+
template.add_text_in_color(
36+
alpha_color,
37+
expected_for_failure_message,
38+
)
39+
end
40+
41+
def add_expected_value_to_description(template)
42+
template.add_text " "
43+
template.add_text_in_color(
44+
alpha_color,
45+
"`#{expected_for_description}`",
46+
)
47+
end
48+
49+
private
50+
51+
def private_predicate?
52+
@private_predicate
53+
end
54+
55+
def predicate_accessible?
56+
@predicate_accessible
57+
end
58+
end
59+
end
60+
end
61+
end

lib/super_diff/rspec/monkey_patches.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,35 @@ def respond_to_failure_message_or
487487
end
488488
end
489489

490+
class Has
491+
prepend SuperDiff::RSpec::AugmentedMatcher
492+
493+
prepend(Module.new do
494+
def actual_for_matcher_text
495+
actual
496+
end
497+
498+
def expected_for_matcher_text
499+
"#{predicate}#{failure_message_args_description}"
500+
end
501+
502+
def expected_action_for_matcher_text
503+
"return true for"
504+
end
505+
506+
def matcher_text_builder_class
507+
SuperDiff::RSpec::MatcherTextBuilders::HavePredicate
508+
end
509+
510+
def matcher_text_builder_args
511+
super.merge(
512+
predicate_accessible: predicate_accessible?,
513+
private_predicate: private_predicate?
514+
)
515+
end
516+
end)
517+
end
518+
490519
class Include
491520
prepend SuperDiff::RSpec::AugmentedMatcher
492521

0 commit comments

Comments
 (0)