blob: b8e4eedcb957476161c2fa59266a4ea38be15950 (
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
|
require_relative '../../spec_helper'
describe "IO#set_encoding_by_bom" do
before :each do
@name = tmp('io_set_encoding_by_bom.txt')
touch(@name)
@io = new_io(@name, 'rb')
end
after :each do
@io.close unless @io.closed?
rm_r @name
end
ruby_version_is "2.7" do
it "returns the result encoding if found BOM UTF-8 sequence" do
File.binwrite(@name, "\u{FEFF}abc")
@io.set_encoding_by_bom.should == Encoding::UTF_8
@io.external_encoding.should == Encoding::UTF_8
end
it "returns the result encoding if found BOM UTF_16LE sequence" do
File.binwrite(@name, "\xFF\xFEabc")
@io.set_encoding_by_bom.should == Encoding::UTF_16LE
@io.external_encoding.should == Encoding::UTF_16LE
end
it "returns the result encoding if found BOM UTF_16BE sequence" do
File.binwrite(@name, "\xFE\xFFabc")
@io.set_encoding_by_bom.should == Encoding::UTF_16BE
@io.external_encoding.should == Encoding::UTF_16BE
end
it "returns nil if found BOM sequence not provided" do
File.write(@name, "abc")
@io.set_encoding_by_bom.should == nil
end
it 'returns exception if io not in binary mode' do
not_binary_io = new_io(@name, 'r')
-> { not_binary_io.set_encoding_by_bom }.should raise_error(ArgumentError, 'ASCII incompatible encoding needs binmode')
ensure
not_binary_io.close
end
it 'returns exception if encoding already set' do
@io.set_encoding("utf-8")
-> { @io.set_encoding_by_bom }.should raise_error(ArgumentError, 'encoding is set to UTF-8 already')
end
end
end
|