Skip to content

Commit ce5aceb

Browse files
committed
Check rb_str_set_len() on both binary and UTF-8 strings
1 parent 8010fff commit ce5aceb

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

spec/ruby/optional/capi/string_spec.rb

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,45 @@ def to_str
3232
end
3333
end
3434

35-
describe "rb_str_set_len" do
36-
before :each do
37-
# Make a completely new copy of the string
38-
# for every example (#dup doesn't cut it).
39-
@str = "abcdefghij".b[0..-1]
40-
end
35+
[Encoding::BINARY, Encoding::UTF_8].each do |enc|
36+
describe "rb_str_set_len on a #{enc.name} String" do
37+
before :each do
38+
# Make a completely new copy of the string
39+
# for every example (#dup doesn't cut it).
40+
@str = "abcdefghij".force_encoding(enc)[0..-1]
41+
end
4142

42-
it "reduces the size of the string" do
43-
@s.rb_str_set_len(@str, 5).should == "abcde"
44-
end
43+
it "reduces the size of the string" do
44+
@s.rb_str_set_len(@str, 5).should == "abcde"
45+
end
4546

46-
it "inserts a NULL byte at the length" do
47-
@s.rb_str_set_len(@str, 5).should == "abcde"
48-
@s.rb_str_set_len(@str, 8).should == "abcde\x00gh"
49-
end
47+
it "inserts a NULL byte at the length" do
48+
@s.rb_str_set_len(@str, 5).should == "abcde"
49+
@s.rb_str_set_len(@str, 8).should == "abcde\x00gh"
50+
end
5051

51-
it "updates the byte size and character size" do
52-
@s.rb_str_set_len(@str, 4)
53-
@str.bytesize.should == 4
54-
@str.size.should == 4
55-
@str.should == "abcd"
56-
end
52+
it "updates the byte size and character size" do
53+
@s.rb_str_set_len(@str, 4)
54+
@str.bytesize.should == 4
55+
@str.size.should == 4
56+
@str.should == "abcd"
57+
end
5758

58-
it "updates the string's attributes visible in C code" do
59-
@s.rb_str_set_len_RSTRING_LEN(@str, 4).should == 4
60-
end
59+
it "updates the string's attributes visible in C code" do
60+
@s.rb_str_set_len_RSTRING_LEN(@str, 4).should == 4
61+
end
6162

62-
it "can reveal characters written from C with RSTRING_PTR" do
63-
@s.rb_str_set_len(@str, 1)
64-
@str.should == "a"
63+
it "can reveal characters written from C with RSTRING_PTR" do
64+
@s.rb_str_set_len(@str, 1)
65+
@str.should == "a"
6566

66-
@s.RSTRING_PTR_set(@str, 1, 'B'.ord)
67-
@s.RSTRING_PTR_set(@str, 2, 'C'.ord)
68-
@s.rb_str_set_len(@str, 3)
67+
@s.RSTRING_PTR_set(@str, 1, 'B'.ord)
68+
@s.RSTRING_PTR_set(@str, 2, 'C'.ord)
69+
@s.rb_str_set_len(@str, 3)
6970

70-
@str.bytesize.should == 3
71-
@str.should == "aBC"
71+
@str.bytesize.should == 3
72+
@str.should == "aBC"
73+
end
7274
end
7375
end
7476

0 commit comments

Comments
 (0)