From 93b4c6b26086e4e29ce06a86a1e388073a469ebc Mon Sep 17 00:00:00 2001 From: Dmitriy Ivliev Date: Mon, 2 Nov 2020 19:10:36 +0200 Subject: [PATCH 1/2] adds IO#set_encoding_by_bom spec --- core/io/set_encoding_by_bom_spec.rb | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 core/io/set_encoding_by_bom_spec.rb diff --git a/core/io/set_encoding_by_bom_spec.rb b/core/io/set_encoding_by_bom_spec.rb new file mode 100644 index 0000000000..a414870276 --- /dev/null +++ b/core/io/set_encoding_by_bom_spec.rb @@ -0,0 +1,42 @@ +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.write(@name, "\u{FEFF}abc") + + @io.set_encoding_by_bom.should == Encoding::UTF_8 + @io.external_encoding.should == Encoding::UTF_8 + 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') + 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 From 213ce9cfe89a5e1c5834dba2ca259dbc447b1f9c Mon Sep 17 00:00:00 2001 From: Dmitriy Ivliev Date: Tue, 3 Nov 2020 18:43:30 +0200 Subject: [PATCH 2/2] fixes according review --- core/io/set_encoding_by_bom_spec.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/core/io/set_encoding_by_bom_spec.rb b/core/io/set_encoding_by_bom_spec.rb index a414870276..b8e4eedcb9 100644 --- a/core/io/set_encoding_by_bom_spec.rb +++ b/core/io/set_encoding_by_bom_spec.rb @@ -14,12 +14,26 @@ ruby_version_is "2.7" do it "returns the result encoding if found BOM UTF-8 sequence" do - File.write(@name, "\u{FEFF}abc") + 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") @@ -30,6 +44,7 @@ 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