-
-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathencode_spec.rb
240 lines (202 loc) · 8.16 KB
/
encode_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -*- encoding: utf-8 -*-
require_relative '../../spec_helper'
require_relative 'shared/encode'
describe "String#encode" do
before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal
end
after :each do
Encoding.default_external = @external
Encoding.default_internal = @internal
end
it_behaves_like :string_encode, :encode
describe "when passed no options" do
it "returns a copy when Encoding.default_internal is nil" do
Encoding.default_internal = nil
str = "あ"
encoded = str.encode
encoded.should_not equal(str)
encoded.should == str
end
it "returns a copy for a ASCII-only String when Encoding.default_internal is nil" do
Encoding.default_internal = nil
str = "abc"
encoded = str.encode
encoded.should_not equal(str)
encoded.should == str
end
it "encodes an ascii substring of a binary string to UTF-8" do
x82 = [0x82].pack('C')
str = "#{x82}foo".dup.force_encoding("binary")[1..-1].encode("utf-8")
str.should == "foo".dup.force_encoding("utf-8")
str.encoding.should equal(Encoding::UTF_8)
end
end
describe "when passed to encoding" do
it "returns a copy when passed the same encoding as the String" do
str = "あ"
encoded = str.encode(Encoding::UTF_8)
encoded.should_not equal(str)
encoded.should == str
end
it "round trips a String" do
str = "abc def".dup.force_encoding Encoding::US_ASCII
str.encode("utf-32be").encode("ascii").should == "abc def"
end
end
describe "when passed options" do
it "returns a copy when Encoding.default_internal is nil" do
Encoding.default_internal = nil
str = "あ"
str.encode(invalid: :replace).should_not equal(str)
end
it "normalizes newlines with cr_newline option" do
"\r\nfoo".encode(cr_newline: true).should == "\r\rfoo"
"\rfoo".encode(cr_newline: true).should == "\rfoo"
"\nfoo".encode(cr_newline: true).should == "\rfoo"
end
it "normalizes newlines with crlf_newline option" do
"\r\nfoo".encode(crlf_newline: true).should == "\r\r\nfoo"
"\rfoo".encode(crlf_newline: true).should == "\rfoo"
"\nfoo".encode(crlf_newline: true).should == "\r\nfoo"
end
it "normalizes newlines with universal_newline option" do
"\r\nfoo".encode(universal_newline: true).should == "\nfoo"
"\rfoo".encode(universal_newline: true).should == "\nfoo"
"\nfoo".encode(universal_newline: true).should == "\nfoo"
end
it "replaces invalid encoding in source with default replacement" do
encoded = "ち\xE3\x81\xFF".encode("UTF-16LE", invalid: :replace)
encoded.should == "\u3061\ufffd\ufffd".encode("UTF-16LE")
encoded.encode("UTF-8").should == "ち\ufffd\ufffd"
end
it "replaces invalid encoding in source with a specified replacement" do
encoded = "ち\xE3\x81\xFF".encode("UTF-16LE", invalid: :replace, replace: "foo")
encoded.should == "\u3061foofoo".encode("UTF-16LE")
encoded.encode("UTF-8").should == "ちfoofoo"
end
it "replace multiple invalid bytes at the end with a single replacement character" do
"\xE3\x81\x93\xE3\x81".encode("UTF-8", invalid: :replace).should == "\u3053\ufffd"
end
it "replaces invalid encoding in source using a specified replacement even when a fallback is given" do
encoded = "ち\xE3\x81\xFF".encode("UTF-16LE", invalid: :replace, replace: "foo", fallback: -> c { "bar" })
encoded.should == "\u3061foofoo".encode("UTF-16LE")
encoded.encode("UTF-8").should == "ちfoofoo"
end
it "replaces undefined encoding in destination with default replacement" do
encoded = "B\ufffd".encode(Encoding::US_ASCII, undef: :replace)
encoded.should == "B?".encode(Encoding::US_ASCII)
encoded.encode("UTF-8").should == "B?"
end
it "replaces undefined encoding in destination with a specified replacement" do
encoded = "B\ufffd".encode(Encoding::US_ASCII, undef: :replace, replace: "foo")
encoded.should == "Bfoo".encode(Encoding::US_ASCII)
encoded.encode("UTF-8").should == "Bfoo"
end
it "replaces undefined encoding in destination with a specified replacement even if a fallback is given" do
encoded = "B\ufffd".encode(Encoding::US_ASCII, undef: :replace, replace: "foo", fallback: proc {|x| "bar"})
encoded.should == "Bfoo".encode(Encoding::US_ASCII)
encoded.encode("UTF-8").should == "Bfoo"
end
it "replaces undefined encoding in destination using a fallback proc" do
encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: proc {|x| "bar"})
encoded.should == "Bbar".encode(Encoding::US_ASCII)
encoded.encode("UTF-8").should == "Bbar"
end
it "replaces invalid encoding in source using replace even when fallback is given as proc" do
encoded = "ち\xE3\x81\xFF".encode("UTF-16LE", invalid: :replace, replace: "foo", fallback: proc {|x| "bar"})
encoded.should == "\u3061foofoo".encode("UTF-16LE")
encoded.encode("UTF-8").should == "ちfoofoo"
end
end
describe "when passed to, from" do
it "returns a copy in the destination encoding when both encodings are the same" do
str = "あ".dup.force_encoding("binary")
encoded = str.encode("utf-8", "utf-8")
encoded.should_not equal(str)
encoded.should == str.force_encoding("utf-8")
encoded.encoding.should == Encoding::UTF_8
end
it "returns the transcoded string" do
str = "\x00\x00\x00\x1F"
str.encode(Encoding::UTF_8, Encoding::UTF_16BE).should == "\u0000\u001f"
end
end
describe "when passed to, options" do
it "returns a copy when the destination encoding is the same as the String encoding" do
str = "あ"
encoded = str.encode(Encoding::UTF_8, undef: :replace)
encoded.should_not equal(str)
encoded.should == str
end
end
describe "when passed to, from, options" do
it "returns a copy when both encodings are the same" do
str = "あ"
encoded = str.encode("utf-8", "utf-8", invalid: :replace)
encoded.should_not equal(str)
encoded.should == str
end
it "returns a copy in the destination encoding when both encodings are the same" do
str = "あ".dup.force_encoding("binary")
encoded = str.encode("utf-8", "utf-8", invalid: :replace)
encoded.should_not equal(str)
encoded.should == str.force_encoding("utf-8")
encoded.encoding.should == Encoding::UTF_8
end
end
end
describe "String#encode!" do
before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal
end
after :each do
Encoding.default_external = @external
Encoding.default_internal = @internal
end
it_behaves_like :string_encode, :encode!
it "raises a FrozenError when called on a frozen String" do
-> { "foo".freeze.encode!("euc-jp") }.should raise_error(FrozenError)
end
# http://redmine.ruby-lang.org/issues/show/1836
it "raises a FrozenError when called on a frozen String when it's a no-op" do
-> { "foo".freeze.encode!("utf-8") }.should raise_error(FrozenError)
end
describe "when passed no options" do
it "returns self when Encoding.default_internal is nil" do
Encoding.default_internal = nil
str = +"あ"
str.encode!.should equal(str)
end
it "returns self for a ASCII-only String when Encoding.default_internal is nil" do
Encoding.default_internal = nil
str = +"abc"
str.encode!.should equal(str)
end
end
describe "when passed options" do
it "returns self for ASCII-only String when Encoding.default_internal is nil" do
Encoding.default_internal = nil
str = +"abc"
str.encode!(invalid: :replace).should equal(str)
end
end
describe "when passed to encoding" do
it "returns self" do
str = +"abc"
result = str.encode!(Encoding::BINARY)
result.encoding.should equal(Encoding::BINARY)
result.should equal(str)
end
end
describe "when passed to, from" do
it "returns self" do
str = +"ああ"
result = str.encode!("euc-jp", "utf-8")
result.encoding.should equal(Encoding::EUC_JP)
result.should equal(str)
end
end
end