Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable specs using GB2312 encoding on musl (fixes #3976) #5867

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions spec/std/io/buffered_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,18 @@ describe "IO::Buffered" do
end
end

it "gets big GB2312 string" do
str = ("你好我是人\n" * 1000).encode("GB2312")
base_io = IO::Memory.new(str)
io = BufferedWrapper.new(base_io)
io.set_encoding("GB2312")
1000.times do
io.gets(chomp: false).should eq("你好我是人\n")
# Musl does not support GB2312 encoding.
{% unless flag?(:musl) %}
it "gets big GB2312 string" do
str = ("你好我是人\n" * 1000).encode("GB2312")
base_io = IO::Memory.new(str)
io = BufferedWrapper.new(base_io)
io.set_encoding("GB2312")
1000.times do
io.gets(chomp: false).should eq("你好我是人\n")
end
end
end
{% end %}

it "reads char" do
str = "x\nHello world" + ("1234567890" * 1000)
Expand Down
133 changes: 71 additions & 62 deletions spec/std/io/io_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,19 @@ describe IO do
end
end

it "gets big GB2312 string" do
2.times do
str = ("你好我是人\n" * 1000).encode("GB2312")
io = SimpleIOMemory.new(str)
io.set_encoding("GB2312")
1000.times do
io.gets.should eq("你好我是人")
# Musl does not support GB2312 encoding.
{% unless flag?(:musl) %}
it "gets big GB2312 string" do
2.times do
str = ("你好我是人\n" * 1000).encode("GB2312")
io = SimpleIOMemory.new(str)
io.set_encoding("GB2312")
1000.times do
io.gets.should eq("你好我是人")
end
end
end
end
{% end %}

it "does gets on unicode with char and limit without off-by-one" do
io = SimpleIOMemory.new("test\nabc".encode("UCS-2LE"))
Expand Down Expand Up @@ -635,51 +638,54 @@ describe IO do
io.read_utf8_byte.should be_nil
end

it "reads utf8" do
io = IO::Memory.new("你".encode("GB2312"))
io.set_encoding("GB2312")
# Musl does not support GB2312 encoding.
{% unless flag?(:musl) %}
it "reads utf8" do
io = IO::Memory.new("你".encode("GB2312"))
io.set_encoding("GB2312")

buffer = uninitialized UInt8[1024]
bytes_read = io.read_utf8(buffer.to_slice) # => 3
bytes_read.should eq(3)
buffer.to_slice[0, bytes_read].to_a.should eq("你".bytes)
end
buffer = uninitialized UInt8[1024]
bytes_read = io.read_utf8(buffer.to_slice) # => 3
bytes_read.should eq(3)
buffer.to_slice[0, bytes_read].to_a.should eq("你".bytes)
end

it "raises on incomplete byte sequence" do
io = SimpleIOMemory.new("好".byte_slice(0, 1))
io.set_encoding("GB2312")
expect_raises ArgumentError, "Incomplete multibyte sequence" do
io.read_char
it "raises on incomplete byte sequence" do
io = SimpleIOMemory.new("好".byte_slice(0, 1))
io.set_encoding("GB2312")
expect_raises ArgumentError, "Incomplete multibyte sequence" do
io.read_char
end
end
end

it "says invalid byte sequence" do
io = SimpleIOMemory.new(Slice.new(1, 140_u8))
io.set_encoding("GB2312")
expect_raises ArgumentError, "Invalid multibyte sequence" do
io.read_char
it "says invalid byte sequence" do
io = SimpleIOMemory.new(Slice.new(1, 140_u8))
io.set_encoding("GB2312")
expect_raises ArgumentError, "Invalid multibyte sequence" do
io.read_char
end
end
end

it "skips invalid byte sequences" do
string = String.build do |str|
str.write "好".encode("GB2312")
str.write_byte 140_u8
str.write "是".encode("GB2312")
it "skips invalid byte sequences" do
string = String.build do |str|
str.write "好".encode("GB2312")
str.write_byte 140_u8
str.write "是".encode("GB2312")
end
io = SimpleIOMemory.new(string)
io.set_encoding("GB2312", invalid: :skip)
io.read_char.should eq('好')
io.read_char.should eq('是')
io.read_char.should be_nil
end
io = SimpleIOMemory.new(string)
io.set_encoding("GB2312", invalid: :skip)
io.read_char.should eq('好')
io.read_char.should eq('是')
io.read_char.should be_nil
end

it "says invalid 'invalid' option" do
io = SimpleIOMemory.new
expect_raises ArgumentError, "Valid values for `invalid` option are `nil` and `:skip`, not :foo" do
io.set_encoding("GB2312", invalid: :foo)
it "says invalid 'invalid' option" do
io = SimpleIOMemory.new
expect_raises ArgumentError, "Valid values for `invalid` option are `nil` and `:skip`, not :foo" do
io.set_encoding("GB2312", invalid: :foo)
end
end
end
{% end %}

it "says invalid encoding" do
io = SimpleIOMemory.new("foo")
Expand Down Expand Up @@ -803,28 +809,31 @@ describe IO do
slice.should eq("hi-123-45.67".encode("UCS-2LE"))
end

it "raises on invalid byte sequence" do
io = SimpleIOMemory.new
io.set_encoding("GB2312")
expect_raises ArgumentError, "Invalid multibyte sequence" do
io.print "ñ"
# Musl does not support GB2312 encoding.
{% unless flag?(:musl) %}
it "raises on invalid byte sequence" do
io = SimpleIOMemory.new
io.set_encoding("GB2312")
expect_raises ArgumentError, "Invalid multibyte sequence" do
io.print "ñ"
end
end
end

it "skips on invalid byte sequence" do
io = SimpleIOMemory.new
io.set_encoding("GB2312", invalid: :skip)
io.print "ñ"
io.print "foo"
end
it "skips on invalid byte sequence" do
io = SimpleIOMemory.new
io.set_encoding("GB2312", invalid: :skip)
io.print "ñ"
io.print "foo"
end

it "raises on incomplete byte sequence" do
io = SimpleIOMemory.new
io.set_encoding("GB2312")
expect_raises ArgumentError, "Incomplete multibyte sequence" do
io.print "好".byte_slice(0, 1)
it "raises on incomplete byte sequence" do
io = SimpleIOMemory.new
io.set_encoding("GB2312")
expect_raises ArgumentError, "Incomplete multibyte sequence" do
io.print "好".byte_slice(0, 1)
end
end
end
{% end %}

it "says invalid encoding" do
io = SimpleIOMemory.new
Expand Down
41 changes: 22 additions & 19 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2285,35 +2285,38 @@ describe "String" do
end
end

it "raises if illegal byte sequence" do
expect_raises ArgumentError, "Invalid multibyte sequence" do
"ñ".encode("GB2312")
# Musl does not support GB2312 encoding.
{% unless flag?(:musl) %}
it "raises if illegal byte sequence" do
expect_raises ArgumentError, "Invalid multibyte sequence" do
"ñ".encode("GB2312")
end
end
end

it "doesn't raise on invalid byte sequence" do
"好ñ是".encode("GB2312", invalid: :skip).to_a.should eq([186, 195, 202, 199])
end
it "doesn't raise on invalid byte sequence" do
"好ñ是".encode("GB2312", invalid: :skip).to_a.should eq([186, 195, 202, 199])
end

it "raises if incomplete byte sequence" do
expect_raises ArgumentError, "Incomplete multibyte sequence" do
"好".byte_slice(0, 1).encode("GB2312")
it "raises if incomplete byte sequence" do
expect_raises ArgumentError, "Incomplete multibyte sequence" do
"好".byte_slice(0, 1).encode("GB2312")
end
end
end

it "doesn't raise if incomplete byte sequence" do
("好".byte_slice(0, 1) + "是").encode("GB2312", invalid: :skip).to_a.should eq([202, 199])
end
it "doesn't raise if incomplete byte sequence" do
("好".byte_slice(0, 1) + "是").encode("GB2312", invalid: :skip).to_a.should eq([202, 199])
end

it "decodes with skip" do
bytes = Bytes[186, 195, 140, 202, 199]
String.new(bytes, "GB2312", invalid: :skip).should eq("好是")
end
{% end %}

it "decodes" do
bytes = "Hello".encode("UTF-16LE")
String.new(bytes, "UTF-16LE").should eq("Hello")
end

it "decodes with skip" do
bytes = Bytes[186, 195, 140, 202, 199]
String.new(bytes, "GB2312", invalid: :skip).should eq("好是")
end
end

it "inserts" do
Expand Down