summaryrefslogtreecommitdiff
path: root/spec/ruby/core/encoding/compatible_spec.rb
blob: f18d8680a96e1e2ce2df00a54ab8ef7fc830a13b (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# -*- encoding: binary -*-

require_relative '../../spec_helper'

# TODO: add IO

describe "Encoding.compatible? String, String" do
  describe "when the first's Encoding is valid US-ASCII" do
    before :each do
      @str = "abc".dup.force_encoding Encoding::US_ASCII
    end

    it "returns US-ASCII when the second's is US-ASCII" do
      Encoding.compatible?(@str, "def".encode("us-ascii")).should == Encoding::US_ASCII
    end

    it "returns US-ASCII if the second String is BINARY and ASCII only" do
      Encoding.compatible?(@str, "\x7f").should == Encoding::US_ASCII
    end

    it "returns BINARY if the second String is BINARY but not ASCII only" do
      Encoding.compatible?(@str, "\xff").should == Encoding::BINARY
    end

    it "returns US-ASCII if the second String is UTF-8 and ASCII only" do
      Encoding.compatible?(@str, "\x7f".encode("utf-8")).should == Encoding::US_ASCII
    end

    it "returns UTF-8 if the second String is UTF-8 but not ASCII only" do
      Encoding.compatible?(@str, "\u3042".encode("utf-8")).should == Encoding::UTF_8
    end
  end

  describe "when the first's Encoding is ASCII compatible and ASCII only" do
    it "returns the first's Encoding if the second is ASCII compatible and ASCII only" do
      [ [Encoding, "abc".dup.force_encoding("UTF-8"), "123".dup.force_encoding("Shift_JIS"), Encoding::UTF_8],
        [Encoding, "123".dup.force_encoding("Shift_JIS"), "abc".dup.force_encoding("UTF-8"), Encoding::Shift_JIS]
      ].should be_computed_by(:compatible?)
    end

    it "returns the first's Encoding if the second is ASCII compatible and ASCII only" do
      [ [Encoding, "abc".dup.force_encoding("BINARY"), "123".dup.force_encoding("US-ASCII"), Encoding::BINARY],
        [Encoding, "123".dup.force_encoding("US-ASCII"), "abc".dup.force_encoding("BINARY"), Encoding::US_ASCII]
      ].should be_computed_by(:compatible?)
    end

    it "returns the second's Encoding if the second is ASCII compatible but not ASCII only" do
      [ [Encoding, "abc".dup.force_encoding("UTF-8"), "\xff".dup.force_encoding("Shift_JIS"), Encoding::Shift_JIS],
        [Encoding, "123".dup.force_encoding("Shift_JIS"), "\xff".dup.force_encoding("UTF-8"), Encoding::UTF_8],
        [Encoding, "abc".dup.force_encoding("BINARY"), "\xff".dup.force_encoding("US-ASCII"), Encoding::US_ASCII],
        [Encoding, "123".dup.force_encoding("US-ASCII"), "\xff".dup.force_encoding("BINARY"), Encoding::BINARY],
      ].should be_computed_by(:compatible?)
    end

    it "returns nil if the second's Encoding is not ASCII compatible" do
      a = "abc".dup.force_encoding("UTF-8")
      b = "1234".dup.force_encoding("UTF-16LE")
      Encoding.compatible?(a, b).should be_nil
    end
  end

  describe "when the first's Encoding is ASCII compatible but not ASCII only" do
    it "returns the first's Encoding if the second's is valid US-ASCII" do
      Encoding.compatible?("\xff", "def".encode("us-ascii")).should == Encoding::BINARY
    end

    it "returns the first's Encoding if the second's is UTF-8 and ASCII only" do
      Encoding.compatible?("\xff", "\u{7f}".encode("utf-8")).should == Encoding::BINARY
    end

    it "returns nil if the second encoding is ASCII compatible but neither String's encoding is ASCII only" do
      Encoding.compatible?("\xff", "\u3042".encode("utf-8")).should be_nil
    end
  end

  describe "when the first's Encoding is not ASCII compatible" do
    before :each do
      @str = "abc".dup.force_encoding Encoding::UTF_7
    end

    it "returns nil when the second String is US-ASCII" do
      Encoding.compatible?(@str, "def".encode("us-ascii")).should be_nil
    end

    it "returns nil when the second String is BINARY and ASCII only" do
      Encoding.compatible?(@str, "\x7f").should be_nil
    end

    it "returns nil when the second String is BINARY but not ASCII only" do
      Encoding.compatible?(@str, "\xff").should be_nil
    end

    it "returns the Encoding when the second's Encoding is not ASCII compatible but the same as the first's Encoding" do
      encoding = Encoding.compatible?(@str, "def".dup.force_encoding("utf-7"))
      encoding.should == Encoding::UTF_7
    end
  end

  describe "when the first's Encoding is invalid" do
    before :each do
      @str = "\xff".dup.force_encoding Encoding::UTF_8
    end

    it "returns the first's Encoding when the second's Encoding is US-ASCII" do
      Encoding.compatible?(@str, "def".encode("us-ascii")).should == Encoding::UTF_8
    end

    it "returns the first's Encoding when the second String is ASCII only" do
      Encoding.compatible?(@str, "\x7f").should == Encoding::UTF_8
    end

    it "returns nil when the second's Encoding is BINARY but not ASCII only" do
      Encoding.compatible?(@str, "\xff").should be_nil
    end

    it "returns nil when the second's Encoding is invalid and ASCII only" do
      Encoding.compatible?(@str, "\x7f".dup.force_encoding("utf-16be")).should be_nil
    end

    it "returns nil when the second's Encoding is invalid and not ASCII only" do
      Encoding.compatible?(@str, "\xff".dup.force_encoding("utf-16be")).should be_nil
    end

    it "returns the Encoding when the second's Encoding is invalid but the same as the first" do
      Encoding.compatible?(@str, @str).should == Encoding::UTF_8
    end
  end

  describe "when the first String is empty and the second is not" do
    describe "and the first's Encoding is ASCII compatible" do
      before :each do
        @str = "".dup.force_encoding("utf-8")
      end

      it "returns the first's encoding when the second String is ASCII only" do
        Encoding.compatible?(@str, "def".encode("us-ascii")).should == Encoding::UTF_8
      end

      it "returns the second's encoding when the second String is not ASCII only" do
        Encoding.compatible?(@str, "def".encode("utf-32le")).should == Encoding::UTF_32LE
      end
    end

    describe "when the first's Encoding is not ASCII compatible" do
      before :each do
        @str = "".dup.force_encoding Encoding::UTF_7
      end

      it "returns the second string's encoding" do
        Encoding.compatible?(@str, "def".encode("us-ascii")).should == Encoding::US_ASCII
      end
    end
  end

  describe "when the second String is empty" do
    before :each do
      @str = "abc".dup.force_encoding("utf-7")
    end

    it "returns the first Encoding" do
      Encoding.compatible?(@str, "").should == Encoding::UTF_7
    end
  end
end

describe "Encoding.compatible? String, Regexp" do
  it "returns US-ASCII if both are US-ASCII" do
    str = "abc".dup.force_encoding("us-ascii")
    Encoding.compatible?(str, /abc/).should == Encoding::US_ASCII
  end

  it "returns the String's Encoding if it is not US-ASCII but both are ASCII only" do
    [ [Encoding, "abc",                     Encoding::BINARY],
      [Encoding, "abc".encode("utf-8"),     Encoding::UTF_8],
      [Encoding, "abc".encode("euc-jp"),    Encoding::EUC_JP],
      [Encoding, "abc".encode("shift_jis"), Encoding::Shift_JIS],
    ].should be_computed_by(:compatible?, /abc/)
  end

  it "returns the String's Encoding if the String is not ASCII only" do
    [ [Encoding, "\xff",                                  Encoding::BINARY],
      [Encoding, "\u3042".encode("utf-8"),                Encoding::UTF_8],
      [Encoding, "\xa4\xa2".dup.force_encoding("euc-jp"),     Encoding::EUC_JP],
      [Encoding, "\x82\xa0".dup.force_encoding("shift_jis"),  Encoding::Shift_JIS],
    ].should be_computed_by(:compatible?, /abc/)
  end
end

describe "Encoding.compatible? String, Symbol" do
  it "returns US-ASCII if both are ASCII only" do
    str = "abc".dup.force_encoding("us-ascii")
    Encoding.compatible?(str, :abc).should == Encoding::US_ASCII
  end

  it "returns the String's Encoding if it is not US-ASCII but both are ASCII only" do
    [ [Encoding, "abc",                     Encoding::BINARY],
      [Encoding, "abc".encode("utf-8"),     Encoding::UTF_8],
      [Encoding, "abc".encode("euc-jp"),    Encoding::EUC_JP],
      [Encoding, "abc".encode("shift_jis"), Encoding::Shift_JIS],
    ].should be_computed_by(:compatible?, :abc)
  end

  it "returns the String's Encoding if the String is not ASCII only" do
    [ [Encoding, "\xff",                                  Encoding::BINARY],
      [Encoding, "\u3042".encode("utf-8"),                Encoding::UTF_8],
      [Encoding, "\xa4\xa2".dup.force_encoding("euc-jp"),     Encoding::EUC_JP],
      [Encoding, "\x82\xa0".dup.force_encoding("shift_jis"),  Encoding::Shift_JIS],
    ].should be_computed_by(:compatible?, :abc)
  end
end

describe "Encoding.compatible? String, Encoding" do
  it "returns nil if the String's encoding is not ASCII compatible" do
    Encoding.compatible?("abc".encode("utf-32le"), Encoding::US_ASCII).should be_nil
  end

  it "returns nil if the Encoding is not ASCII compatible" do
    Encoding.compatible?("abc".encode("us-ascii"), Encoding::UTF_32LE).should be_nil
  end

  it "returns the String's encoding if the Encoding is US-ASCII" do
    [ [Encoding, "\xff",                                  Encoding::BINARY],
      [Encoding, "\u3042".encode("utf-8"),                Encoding::UTF_8],
      [Encoding, "\xa4\xa2".dup.force_encoding("euc-jp"),     Encoding::EUC_JP],
      [Encoding, "\x82\xa0".dup.force_encoding("shift_jis"),  Encoding::Shift_JIS],
    ].should be_computed_by(:compatible?, Encoding::US_ASCII)
  end

  it "returns the Encoding if the String's encoding is ASCII compatible and the String is ASCII only" do
    str = "abc".encode("utf-8")

    Encoding.compatible?(str, Encoding::BINARY).should == Encoding::BINARY
    Encoding.compatible?(str, Encoding::UTF_8).should == Encoding::UTF_8
    Encoding.compatible?(str, Encoding::EUC_JP).should == Encoding::EUC_JP
    Encoding.compatible?(str, Encoding::Shift_JIS).should == Encoding::Shift_JIS
  end

  it "returns nil if the String's encoding is ASCII compatible but the string is not ASCII only" do
    Encoding.compatible?("\u3042".encode("utf-8"), Encoding::BINARY).should be_nil
  end
end

describe "Encoding.compatible? Regexp, String" do
  it "returns US-ASCII if both are US-ASCII" do
    str = "abc".dup.force_encoding("us-ascii")
    Encoding.compatible?(/abc/, str).should == Encoding::US_ASCII
  end

end

describe "Encoding.compatible? Regexp, Regexp" do
  it "returns US-ASCII if both are US-ASCII" do
    Encoding.compatible?(/abc/, /def/).should == Encoding::US_ASCII
  end

  it "returns the first's Encoding if it is not US-ASCII and not ASCII only" do
    [ [Encoding, Regexp.new("\xff"),                                  Encoding::BINARY],
      [Encoding, Regexp.new("\u3042".encode("utf-8")),                Encoding::UTF_8],
      [Encoding, Regexp.new("\xa4\xa2".dup.force_encoding("euc-jp")),     Encoding::EUC_JP],
      [Encoding, Regexp.new("\x82\xa0".dup.force_encoding("shift_jis")),  Encoding::Shift_JIS],
    ].should be_computed_by(:compatible?, /abc/)
  end
end

describe "Encoding.compatible? Regexp, Symbol" do
  it "returns US-ASCII if both are US-ASCII" do
    Encoding.compatible?(/abc/, :def).should == Encoding::US_ASCII
  end

  it "returns the first's Encoding if it is not US-ASCII and not ASCII only" do
    [ [Encoding, Regexp.new("\xff"),                                  Encoding::BINARY],
      [Encoding, Regexp.new("\u3042".encode("utf-8")),                Encoding::UTF_8],
      [Encoding, Regexp.new("\xa4\xa2".dup.force_encoding("euc-jp")),     Encoding::EUC_JP],
      [Encoding, Regexp.new("\x82\xa0".dup.force_encoding("shift_jis")),  Encoding::Shift_JIS],
    ].should be_computed_by(:compatible?, /abc/)
  end
end

describe "Encoding.compatible? Symbol, String" do
  it "returns US-ASCII if both are ASCII only" do
    str = "abc".dup.force_encoding("us-ascii")
    Encoding.compatible?(str, :abc).should == Encoding::US_ASCII
  end
end

describe "Encoding.compatible? Symbol, Regexp" do
  it "returns US-ASCII if both are US-ASCII" do
    Encoding.compatible?(:abc, /def/).should == Encoding::US_ASCII
  end

  it "returns the Regexp's Encoding if it is not US-ASCII and not ASCII only" do
    a = Regexp.new("\xff")
    b = Regexp.new("\u3042".encode("utf-8"))
    c = Regexp.new("\xa4\xa2".dup.force_encoding("euc-jp"))
    d = Regexp.new("\x82\xa0".dup.force_encoding("shift_jis"))

    [ [Encoding, :abc, a, Encoding::BINARY],
      [Encoding, :abc, b, Encoding::UTF_8],
      [Encoding, :abc, c, Encoding::EUC_JP],
      [Encoding, :abc, d, Encoding::Shift_JIS],
    ].should be_computed_by(:compatible?)
  end
end

describe "Encoding.compatible? Symbol, Symbol" do
  it "returns US-ASCII if both are US-ASCII" do
    Encoding.compatible?(:abc, :def).should == Encoding::US_ASCII
  end

  it "returns the first's Encoding if it is not ASCII only" do
    [ [Encoding, "\xff".to_sym,                                  Encoding::BINARY],
      [Encoding, "\u3042".encode("utf-8").to_sym,                Encoding::UTF_8],
      [Encoding, "\xa4\xa2".dup.force_encoding("euc-jp").to_sym,     Encoding::EUC_JP],
      [Encoding, "\x82\xa0".dup.force_encoding("shift_jis").to_sym,  Encoding::Shift_JIS],
    ].should be_computed_by(:compatible?, :abc)
  end
end

describe "Encoding.compatible? Encoding, Encoding" do
  it "returns nil if one of the encodings is a dummy encoding" do
    [ [Encoding, Encoding::UTF_7, Encoding::US_ASCII,   nil],
      [Encoding, Encoding::US_ASCII, Encoding::UTF_7,   nil],
      [Encoding, Encoding::EUC_JP, Encoding::UTF_7,     nil],
      [Encoding, Encoding::UTF_7, Encoding::EUC_JP,     nil],
      [Encoding, Encoding::UTF_7, Encoding::BINARY, nil],
      [Encoding, Encoding::BINARY, Encoding::UTF_7, nil],
    ].should be_computed_by(:compatible?)
  end

  it "returns nil if one of the encodings is not US-ASCII" do
    [ [Encoding, Encoding::UTF_8, Encoding::BINARY,   nil],
      [Encoding, Encoding::BINARY, Encoding::UTF_8,   nil],
      [Encoding, Encoding::BINARY, Encoding::EUC_JP,  nil],
      [Encoding, Encoding::Shift_JIS, Encoding::EUC_JP,   nil],
    ].should be_computed_by(:compatible?)
  end

  it "returns the first if the second is US-ASCII" do
    [ [Encoding, Encoding::UTF_8, Encoding::US_ASCII,       Encoding::UTF_8],
      [Encoding, Encoding::EUC_JP, Encoding::US_ASCII,      Encoding::EUC_JP],
      [Encoding, Encoding::Shift_JIS, Encoding::US_ASCII,   Encoding::Shift_JIS],
      [Encoding, Encoding::BINARY, Encoding::US_ASCII,  Encoding::BINARY],
    ].should be_computed_by(:compatible?)
  end

  it "returns the Encoding if both are the same" do
    [ [Encoding, Encoding::UTF_8, Encoding::UTF_8,            Encoding::UTF_8],
      [Encoding, Encoding::US_ASCII, Encoding::US_ASCII,      Encoding::US_ASCII],
      [Encoding, Encoding::BINARY, Encoding::BINARY,  Encoding::BINARY],
      [Encoding, Encoding::UTF_7, Encoding::UTF_7,            Encoding::UTF_7],
    ].should be_computed_by(:compatible?)
  end
end

describe "Encoding.compatible? Object, Object" do
  it "returns nil for Object, String" do
    Encoding.compatible?(Object.new, "abc").should be_nil
  end

  it "returns nil for Object, Regexp" do
    Encoding.compatible?(Object.new, /./).should be_nil
  end

  it "returns nil for Object, Symbol" do
    Encoding.compatible?(Object.new, :sym).should be_nil
  end

  it "returns nil for String, Object" do
    Encoding.compatible?("abc", Object.new).should be_nil
  end

  it "returns nil for Regexp, Object" do
    Encoding.compatible?(/./, Object.new).should be_nil
  end

  it "returns nil for Symbol, Object" do
    Encoding.compatible?(:sym, Object.new).should be_nil
  end
end