File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change @@ -127,6 +127,13 @@ def fail(reason = StandardError.new)
127
127
set { raise reason }
128
128
end
129
129
130
+ def set? ( value = NO_VALUE , &block )
131
+ set ( value , &block )
132
+ true
133
+ rescue MultipleAssignmentError
134
+ false
135
+ end
136
+
130
137
protected
131
138
132
139
# @!visibility private
Original file line number Diff line number Diff line change 93
93
expect ( subject . fail ) . to eq subject
94
94
end
95
95
end
96
+
97
+ describe '#set?' do
98
+
99
+ context 'when unset' do
100
+
101
+ it 'assigns the value' do
102
+ subject . set? ( 32 )
103
+ expect ( subject . value ) . to eq 32
104
+ end
105
+
106
+ it 'assigns the block result' do
107
+ subject . set? { 32 }
108
+ expect ( subject . value ) . to eq 32
109
+ end
110
+
111
+ it 'returns true' do
112
+ expect ( subject . set? ( 'hi' ) ) . to eq true
113
+ end
114
+ end
115
+
116
+ context 'when fulfilled' do
117
+
118
+ before ( :each ) { subject . set ( 27 ) }
119
+
120
+ it 'does not assign the value' do
121
+ subject . set? ( 88 )
122
+ expect ( subject . value ) . to eq 27
123
+ end
124
+
125
+ it 'does not assign the block result' do
126
+ subject . set? { 88 }
127
+ expect ( subject . value ) . to eq 27
128
+ end
129
+
130
+ it 'returns false' do
131
+ expect ( subject . set? ( 'hello' ) ) . to eq false
132
+ end
133
+ end
134
+
135
+ context 'when rejected' do
136
+
137
+ before ( :each ) { subject . fail }
138
+
139
+ it 'does not assign the value' do
140
+ subject . set? ( 88 )
141
+ expect ( subject ) . to be_rejected
142
+ end
143
+
144
+ it 'does not assign the block result' do
145
+ subject . set? { 88 }
146
+ expect ( subject ) . to be_rejected
147
+ end
148
+
149
+ it 'has a nil value' do
150
+ expect ( subject . value ) . to be_nil
151
+ end
152
+
153
+ it 'returns false' do
154
+ expect ( subject . set? ( 'hello' ) ) . to eq false
155
+ end
156
+ end
157
+ end
96
158
end
You can’t perform that action at this time.
0 commit comments