|
100 | 100 | if RSpec::Support::RubyFeatures.distincts_kw_args_from_positional_hash?
|
101 | 101 | eval <<-'RUBY', nil, __FILE__, __LINE__ + 1
|
102 | 102 | it "prints a diff when keyword argument were expected but got an option hash (using splat)" do
|
| 103 | + message = |
| 104 | + "#<Double \"double\"> received :foo with unexpected arguments\n" \ |
| 105 | + " expected: ({:baz=>:quz, :foo=>:bar}) (keyword arguments)\n" \ |
| 106 | + " got: ({:baz=>:quz, :foo=>:bar}) (options hash)" |
| 107 | +
|
| 108 | + message = message.gsub('=>', ' => ') if RUBY_VERSION.to_f > 3.3 |
| 109 | +
|
103 | 110 | with_unfulfilled_double do |d|
|
104 | 111 | expect(d).to receive(:foo).with(**expected_hash)
|
105 | 112 | expect {
|
106 | 113 | d.foo(expected_hash)
|
107 |
| - }.to fail_with( |
108 |
| - "#<Double \"double\"> received :foo with unexpected arguments\n" \ |
109 |
| - " expected: ({:baz=>:quz, :foo=>:bar}) (keyword arguments)\n" \ |
110 |
| - " got: ({:baz=>:quz, :foo=>:bar}) (options hash)" |
111 |
| - ) |
| 114 | + }.to fail_with(message) |
112 | 115 | end
|
113 | 116 | end
|
114 | 117 | RUBY
|
|
117 | 120 | it "prints a diff when keyword argument were expected but got an option hash (literal)" do
|
118 | 121 | with_unfulfilled_double do |d|
|
119 | 122 | expect(d).to receive(:foo).with(:positional, keyword: 1)
|
120 |
| - expect { |
121 |
| - options = { keyword: 1 } |
122 |
| - d.foo(:positional, options) |
123 |
| - }.to fail_with( |
| 123 | +
|
| 124 | + message = |
124 | 125 | "#<Double \"double\"> received :foo with unexpected arguments\n" \
|
125 | 126 | " expected: (:positional, {:keyword=>1}) (keyword arguments)\n" \
|
126 | 127 | " got: (:positional, {:keyword=>1}) (options hash)"
|
127 |
| - ) |
| 128 | +
|
| 129 | + message = message.gsub('=>',' => ') if RUBY_VERSION.to_f > 3.3 |
| 130 | +
|
| 131 | + expect { |
| 132 | + options = { keyword: 1 } |
| 133 | + d.foo(:positional, options) |
| 134 | + }.to fail_with(message) |
128 | 135 | end
|
129 | 136 | end
|
130 | 137 | RUBY
|
|
139 | 146 |
|
140 | 147 | expect(d).to receive(:foo).with(expected_input, one: 1)
|
141 | 148 |
|
142 |
| - expect { |
143 |
| - options = { one: 1 } |
144 |
| - d.foo(actual_input, options) |
145 |
| - }.to fail_with( |
| 149 | + message = |
146 | 150 | "#<Double \"double\"> received :foo with unexpected arguments\n" \
|
147 | 151 | " expected: (#{expected_input.inspect}, {:one=>1}) (keyword arguments)\n" \
|
148 | 152 | " got: (#{actual_input.inspect}, {:one=>1}) (options hash)\n" \
|
149 | 153 | "Diff:\n" \
|
150 | 154 | "@@ -1 +1 @@\n" \
|
151 | 155 | "-[#{expected_input.inspect}, {:one=>1}]\n" \
|
152 | 156 | "+[#{actual_input.inspect}, {:one=>1}]\n"
|
153 |
| - ) |
| 157 | +
|
| 158 | + message = message.gsub('=>',' => ') if RUBY_VERSION.to_f > 3.3 |
| 159 | +
|
| 160 | + expect { |
| 161 | + options = { one: 1 } |
| 162 | + d.foo(actual_input, options) |
| 163 | + }.to fail_with(message) |
154 | 164 | end
|
155 | 165 | end
|
156 | 166 | RUBY
|
|
172 | 182 | if RSpec::Support::RubyFeatures.distincts_kw_args_from_positional_hash?
|
173 | 183 | eval <<-'RUBY', nil, __FILE__, __LINE__ + 1
|
174 | 184 | it "prints a diff when keyword argument were expected but got an option hash (using splat)" do
|
175 |
| - expect(d).to receive(:foo).with(:positional, **expected_hash) |
176 |
| - expect { |
177 |
| - d.foo(:positional, expected_hash) |
178 |
| - }.to fail_with( |
| 185 | + message = |
179 | 186 | "#{d.inspect} received :foo with unexpected arguments\n" \
|
180 | 187 | " expected: (:positional, {:baz=>:quz, :foo=>:bar}) (keyword arguments)\n" \
|
181 | 188 | " got: (:positional, {:baz=>:quz, :foo=>:bar}) (options hash)"
|
182 |
| - ) |
| 189 | +
|
| 190 | + message.gsub!('=>',' => ') if RUBY_VERSION.to_f > 3.3 |
| 191 | +
|
| 192 | + expect(d).to receive(:foo).with(:positional, **expected_hash) |
| 193 | + expect { |
| 194 | + d.foo(:positional, expected_hash) |
| 195 | + }.to fail_with(message) |
183 | 196 | end
|
184 | 197 | RUBY
|
185 | 198 |
|
186 | 199 | eval <<-'RUBY', nil, __FILE__, __LINE__ + 1
|
187 | 200 | it "prints a diff when keyword argument were expected but got an option hash (literal)" do
|
| 201 | + message = |
| 202 | + "#{d.inspect} received :foo with unexpected arguments\n" \ |
| 203 | + " expected: (:positional, {:keyword=>1}) (keyword arguments)\n" \ |
| 204 | + " got: (:positional, {:keyword=>1}) (options hash)" |
| 205 | +
|
| 206 | + message.gsub!('=>',' => ') if RUBY_VERSION.to_f > 3.3 |
| 207 | +
|
188 | 208 | expect(d).to receive(:foo).with(:positional, keyword: 1)
|
189 | 209 | expect {
|
190 | 210 | options = { keyword: 1 }
|
191 | 211 | d.foo(:positional, options)
|
192 |
| - }.to fail_with( |
193 |
| - "#{d.inspect} received :foo with unexpected arguments\n" \ |
194 |
| - " expected: (:positional, {:keyword=>1}) (keyword arguments)\n" \ |
195 |
| - " got: (:positional, {:keyword=>1}) (options hash)" |
196 |
| - ) |
| 212 | + }.to fail_with(message) |
197 | 213 | end
|
198 | 214 | RUBY
|
199 | 215 |
|
|
206 | 222 |
|
207 | 223 | expect(d).to receive(:foo).with(expected_input, one: 1)
|
208 | 224 |
|
209 |
| - expect { |
210 |
| - options = { one: 1 } |
211 |
| - d.foo(actual_input, options) |
212 |
| - }.to fail_with( |
| 225 | + message = |
213 | 226 | "#{d.inspect} received :foo with unexpected arguments\n" \
|
214 | 227 | " expected: (#{expected_input.inspect}, {:one=>1}) (keyword arguments)\n" \
|
215 | 228 | " got: (#{actual_input.inspect}, {:one=>1}) (options hash)\n" \
|
216 | 229 | "Diff:\n" \
|
217 | 230 | "@@ -1 +1 @@\n" \
|
218 | 231 | "-[#{expected_input.inspect}, {:one=>1}]\n" \
|
219 | 232 | "+[#{actual_input.inspect}, {:one=>1}]\n"
|
220 |
| - ) |
| 233 | +
|
| 234 | + message = message.gsub('=>', ' => ') if RUBY_VERSION.to_f > 3.3 |
| 235 | +
|
| 236 | + expect { |
| 237 | + options = { one: 1 } |
| 238 | + d.foo(actual_input, options) |
| 239 | + }.to fail_with(message) |
221 | 240 | end
|
222 | 241 | RUBY
|
223 | 242 | end
|
|
232 | 251 | def hash_regex_inspect(hash)
|
233 | 252 | "\\{(#{hash.map { |key, value| "#{key.inspect}=>#{value.inspect}.*" }.join "|"}){#{hash.size}}\\}"
|
234 | 253 | end
|
| 254 | + elsif RUBY_VERSION.to_f > 3.3 |
| 255 | + # Ruby head / 3.4 is changing the hash syntax inspect, but we use PP when diffing which just spaces out hashrockets |
| 256 | + def hash_regex_inspect(hash) |
| 257 | + Regexp.escape("{#{hash.map { |key, value| "#{key.inspect} => #{value.inspect}"}.join(", ")}}") |
| 258 | + end |
235 | 259 | else
|
236 | 260 | def hash_regex_inspect(hash)
|
237 | 261 | Regexp.escape(hash.inspect)
|
|
0 commit comments