Skip to content

Commit 7266f1b

Browse files
committed
Update specs for rb_interned_str(_cstr)
Add full checks for the behaviour around US_ASCII/BINARY encoding choice. Remove some of the checks that depend on the encoding of the source string, since this function uses C char* without any encoding information.
1 parent f737668 commit 7266f1b

1 file changed

Lines changed: 34 additions & 25 deletions

File tree

optional/capi/string_spec.rb

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,21 +1396,26 @@ def inspect
13961396
result.should == str
13971397
end
13981398

1399-
ruby_bug "21842", ""..."4.1" do
1400-
it "uses BINARY encoding for strings that are not valid US-ASCII" do
1401-
str = "foo\x81bar\x82baz".b
1402-
result = @s.rb_interned_str(str, str.bytesize)
1403-
result.encoding.should == Encoding::BINARY
1404-
result.should == str
1399+
it "return US_ASCII encoding for an empty string" do
1400+
result = @s.rb_interned_str("", 0)
1401+
result.should == ""
1402+
result.encoding.should == Encoding::US_ASCII
1403+
end
1404+
1405+
it "returns US_ASCII encoding for strings of only 7 bit ASCII" do
1406+
0x00.upto(0x7f).each do |char|
1407+
result = @s.rb_interned_str(char.chr, 1)
1408+
result.encoding.should == Encoding::US_ASCII
14051409
end
14061410
end
14071411

1408-
it "returns the same frozen strings for different encodings" do
1409-
str1 = "hello".dup.force_encoding(Encoding::US_ASCII)
1410-
str2 = "hello".dup.force_encoding(Encoding::UTF_8)
1411-
result1 = @s.rb_interned_str(str1, str1.bytesize)
1412-
result2 = @s.rb_interned_str(str2, str2.bytesize)
1413-
result1.should.equal?(result2)
1412+
ruby_bug "21842", ""..."4.1" do
1413+
it "returns BINARY encoding for strings that use the 8th bit" do
1414+
0x80.upto(0xff) do |char|
1415+
result = @s.rb_interned_str(char.chr, 1)
1416+
result.encoding.should == Encoding::BINARY
1417+
end
1418+
end
14141419
end
14151420

14161421
it 'returns the same string when using non-ascii characters' do
@@ -1450,22 +1455,26 @@ def inspect
14501455
result.should == "foo"
14511456
end
14521457

1453-
ruby_bug "21842", ""..."4.1" do
1454-
it "uses BINARY encoding for strings that are not valid US-ASCII" do
1455-
str = "foo\x81bar\x82baz".b
1456-
result = @s.rb_interned_str_cstr(str)
1457-
result.encoding.should == Encoding::BINARY
1458-
result.should == str
1459-
result.should.valid_encoding?
1458+
it "return US_ASCII encoding for an empty string" do
1459+
result = @s.rb_interned_str_cstr("")
1460+
result.should == ""
1461+
result.encoding.should == Encoding::US_ASCII
1462+
end
1463+
1464+
it "returns US_ASCII encoding for strings of only 7 bit ASCII" do
1465+
0x01.upto(0x7f).each do |char|
1466+
result = @s.rb_interned_str_cstr(char.chr)
1467+
result.encoding.should == Encoding::US_ASCII
14601468
end
14611469
end
14621470

1463-
it "returns the same frozen strings for different encodings" do
1464-
str1 = "hello".dup.force_encoding(Encoding::US_ASCII)
1465-
str2 = "hello".dup.force_encoding(Encoding::UTF_8)
1466-
result1 = @s.rb_interned_str_cstr(str1)
1467-
result2 = @s.rb_interned_str_cstr(str2)
1468-
result1.should.equal?(result2)
1471+
ruby_bug "21842", ""..."4.1" do
1472+
it "returns BINARY encoding for strings that use the 8th bit" do
1473+
0x80.upto(0xff) do |char|
1474+
result = @s.rb_interned_str_cstr(char.chr)
1475+
result.encoding.should == Encoding::BINARY
1476+
end
1477+
end
14691478
end
14701479

14711480
it 'returns the same string when using non-ascii characters' do

0 commit comments

Comments
 (0)