1
1
# encoding: utf-8
2
2
require 'spec_helper'
3
3
require 'rspec/support/encoded_string'
4
-
5
- # Special matcher for comparing encoded strings so that
6
- # we don't run any expectation failures through the Differ,
7
- # which also relies on EncodedString. Instead, confirm the
8
- # strings have the same encoding and same bytes.
9
- RSpec ::Matchers . define :be_identical_string do |expected |
10
-
11
- if String . method_defined? ( :encoding )
12
- match do
13
- actual . encoding == expected . encoding &&
14
- actual . bytes . to_a == expected . bytes . to_a
15
- end
16
-
17
- failure_message do
18
- "expected\n #{ actual . inspect } (#{ actual . encoding . name } ) to be identical to\n " \
19
- "#{ expected . inspect } (#{ expected . encoding . name } )\n " \
20
- "The exact bytes are printed below for more detail:\n " \
21
- "#{ actual . bytes . to_a } \n " \
22
- "#{ expected . bytes . to_a } \n " \
23
- end
24
- else
25
- match do |actual |
26
- actual . split ( // ) == expected . split ( // )
27
- end
28
- end
29
- end
30
- RSpec ::Matchers . alias_matcher :a_string_identical_to , :be_identical_string
4
+ require 'rspec/support/spec/string_matcher'
31
5
32
6
module RSpec ::Support
33
7
describe EncodedString do
@@ -93,7 +67,7 @@ module RSpec::Support
93
67
resulting_string = build_encoded_string ( string , target_encoding ) . to_s
94
68
replacement = EncodedString ::REPLACE * 3
95
69
expected_string = "I have a bad byt#{ replacement } " . force_encoding ( target_encoding )
96
- expect ( resulting_string ) . to be_identical_string ( expected_string )
70
+ expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
97
71
end
98
72
end
99
73
@@ -115,13 +89,13 @@ module RSpec::Support
115
89
it 'does nothing' do
116
90
resulting_string = build_encoded_string ( string , no_converter_encoding ) . to_s
117
91
expected_string = "\x80 " . force_encoding ( no_converter_encoding )
118
- expect ( resulting_string ) . to be_identical_string ( expected_string )
92
+ expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
119
93
end
120
94
else
121
95
it 'forces the encoding and replaces invalid characters with the REPLACE string' do
122
96
resulting_string = build_encoded_string ( string , no_converter_encoding ) . to_s
123
97
expected_string = EncodedString ::REPLACE . dup . force_encoding ( no_converter_encoding )
124
- expect ( resulting_string ) . to be_identical_string ( expected_string )
98
+ expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
125
99
end
126
100
127
101
it 'does not mutate the input string' do
@@ -150,7 +124,7 @@ module RSpec::Support
150
124
resulting_string = build_encoded_string ( string , incompatible_encoding ) . to_s
151
125
replacement = EncodedString ::REPLACE
152
126
expected_string = "#{ replacement } hi I am not going to work" . force_encoding ( 'EUC-JP' )
153
- expect ( resulting_string ) . to be_identical_string ( expected_string )
127
+ expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
154
128
end
155
129
end
156
130
end
@@ -166,7 +140,7 @@ module RSpec::Support
166
140
it 'encodes and appends the string' do
167
141
resulting_string = build_encoded_string ( valid_unicode_string , utf8_encoding ) << valid_ascii_string
168
142
expected_string = "#{ utf_8_euro_symbol } abcde" . force_encoding ( 'UTF-8' )
169
- expect ( resulting_string ) . to be_identical_string ( expected_string )
143
+ expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
170
144
end
171
145
end
172
146
@@ -184,7 +158,7 @@ module RSpec::Support
184
158
it 'replaces unconvertable characters with the REPLACE string' do
185
159
resulting_string = build_encoded_string ( valid_unicode_string , utf8_encoding ) << ascii_string
186
160
expected_string = "#{ utf_8_euro_symbol } #{ EncodedString ::REPLACE } "
187
- expect ( resulting_string ) . to be_identical_string ( expected_string )
161
+ expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
188
162
end
189
163
end
190
164
end
@@ -196,7 +170,7 @@ module RSpec::Support
196
170
197
171
resulting_string = build_encoded_string ( ascii_string , utf8_encoding ) << other_ascii_string
198
172
expected_string = 'abc123' . force_encoding ( utf8_encoding )
199
- expect ( resulting_string ) . to be_identical_string ( expected_string )
173
+ expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
200
174
end
201
175
end
202
176
end
@@ -217,8 +191,8 @@ module RSpec::Support
217
191
resulting_string = build_encoded_string ( wrapped_string , utf8_encoding ) . split ( delimiter )
218
192
exp1 , exp2 = sprintf ( wrapped_string_template , EncodedString ::REPLACE ) . force_encoding ( utf8_encoding ) . split ( delimiter )
219
193
expect ( resulting_string ) . to match [
220
- a_string_identical_to ( exp1 ) ,
221
- a_string_identical_to ( exp2 )
194
+ a_string_identical_to ( exp1 ) . with_same_encoding ,
195
+ a_string_identical_to ( exp2 ) . with_same_encoding
222
196
]
223
197
end
224
198
end
@@ -239,7 +213,7 @@ module RSpec::Support
239
213
it 'makes no changes to the resulting string' do
240
214
resulting_array = build_encoded_string ( non_ascii_compatible_string ) . split ( "\n " )
241
215
expect ( resulting_array ) . to match [
242
- a_string_identical_to ( non_ascii_compatible_string )
216
+ a_string_identical_to ( non_ascii_compatible_string ) . with_same_encoding
243
217
]
244
218
end
245
219
end
@@ -258,7 +232,7 @@ module RSpec::Support
258
232
resulting_array = build_encoded_string ( message_with_invalid_byte_sequence , utf8_encoding ) . split ( "\n " )
259
233
expected_string = "? ? ? I have bad bytes"
260
234
expect ( resulting_array ) . to match [
261
- a_string_identical_to ( expected_string )
235
+ a_string_identical_to ( expected_string ) . with_same_encoding
262
236
]
263
237
end
264
238
end
0 commit comments