@@ -6,33 +6,39 @@ module RepresentMatcher
6
6
def represent ( representable )
7
7
RepresentMatcher . new ( representable )
8
8
end
9
-
9
+
10
10
class RepresentMatcher
11
+ include ::RSpec ::Mocks ::ExampleMethods
11
12
def initialize ( representable )
12
13
@expected_representable = representable
13
14
RSpec ::Mocks ::setup
14
15
end
15
-
16
+
16
17
def matches? ( subject )
17
18
@subject = subject
18
19
19
20
check_methods && verify_exposure && check_value
20
21
end
21
-
22
+
22
23
def as ( representation )
23
24
@actual_representation = representation
24
25
self
25
26
end
26
-
27
+
28
+ def format_with ( formatter )
29
+ @formatter = formatter
30
+ self
31
+ end
32
+
27
33
def when ( conditions )
28
34
@conditions = conditions
29
35
self
30
36
end
31
-
37
+
32
38
def using ( other_entity )
33
39
@other_entity = other_entity
34
- @represented_attribute = double ( "RepresentedAttribute" )
35
- @other_entity . exposures . keys . each do |key |
40
+ @represented_attribute = double ( "RepresentedAttribute" )
41
+ @other_entity . exposures . keys . each do |key |
36
42
allow ( @represented_attribute ) . to receive ( key ) . and_return ( @other_entity . exposures [ key ] . nil? ? :value : nil )
37
43
end
38
44
self
@@ -54,7 +60,7 @@ def failure_message
54
60
message << "#{ @subject } didn't return the correct value for #{ @expected_representable } . (#{ @serialized_hash [ @actual_representation || @expected_representable ] } != #{ @represented_attribute || :value } )" unless check_value
55
61
message
56
62
end
57
-
63
+
58
64
def failure_message_when_negated
59
65
message = ""
60
66
message << "Didn't expect #{ @subject } to expose #{ @expected_representable } correctly: #{ @subject . exposures [ @expected_representable ] } \n " if verify_exposure
@@ -74,28 +80,28 @@ def negative_failure_message
74
80
def description
75
81
"Ensures that #{ @subject } properly obtains the value of #{ @expected_representable } from a mock class."
76
82
end
77
-
83
+
78
84
private
79
85
80
86
def limit_exposure_to_method ( entity , method )
81
- allow ( entity ) . to receive ( :valid_exposures ) . and_return (
87
+ allow ( entity ) . to receive ( :valid_exposures ) . and_return (
82
88
entity . exposures . slice ( method )
83
89
)
84
90
end
85
-
91
+
86
92
def check_methods
87
93
@representee = double ( "RepresentedObject" )
88
94
@represented_attribute ||= :value
89
95
90
- allow ( @representee ) . to receive ( @expected_representable ) . and_return ( @represented_attribute )
96
+ allow ( @representee ) . to receive ( @expected_representable ) . and_return ( @represented_attribute )
91
97
expect ( @representee ) . to receive ( @conditions . keys . first ) . and_return ( @conditions . values . first ) unless @conditions . nil?
92
-
98
+
93
99
representation = @subject . represent ( @representee )
94
100
95
101
@serialized_hash = if @root
96
- limit_exposure_to_method ( representation [ @root . to_s ] , @expected_representable )
102
+ limit_exposure_to_method ( representation [ @root . to_s ] , @expected_representable )
97
103
representation [ @root . to_s ] . serializable_hash
98
- else
104
+ else
99
105
limit_exposure_to_method ( representation , @expected_representable )
100
106
representation . serializable_hash
101
107
end
@@ -107,14 +113,15 @@ def check_methods
107
113
# here one can use #{e} to construct an error message
108
114
methods_called = false
109
115
end
110
-
116
+
111
117
methods_called
112
118
end
113
-
119
+
114
120
def verify_exposure
115
121
hash = { }
116
122
hash [ :using ] = @other_entity unless @other_entity . nil?
117
123
hash [ :as ] = @actual_representation unless @actual_representation . nil?
124
+ hash [ :format_with ] = @formatter if @formatter
118
125
119
126
if @conditions . nil?
120
127
@subject . exposures [ @expected_representable ] == hash
@@ -124,12 +131,12 @@ def verify_exposure
124
131
exposures . delete ( :if ) != nil && exposures == hash
125
132
end
126
133
end
127
-
134
+
128
135
def check_value
129
136
if @other_entity
130
137
other_representation = @other_entity . represent ( @represented_attribute )
131
138
132
- other_representation . exposures . keys . each do |key |
139
+ other_representation . exposures . keys . each do |key |
133
140
allow ( other_representation ) . to receive ( key ) . and_return ( other_representation . exposures [ key ] . nil? ? :value : nil )
134
141
end
135
142
0 commit comments