aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/optional/capi/string_spec.rb
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-03-28 14:22:29 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-03-28 14:22:29 +0000
commita28aa80c739a1d169649a4da833ef48cfb3465b3 (patch)
treec2f6bb79c268bd60116b54319ea96f01bb7dda79 /spec/ruby/optional/capi/string_spec.rb
parent0f64776745ef31e626dec0d42b7fb2a5988397ec (diff)
downloadruby-a28aa80c739a1d169649a4da833ef48cfb3465b3.tar.gz
Update to ruby/spec@e81b3cd
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67361 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/optional/capi/string_spec.rb')
-rw-r--r--spec/ruby/optional/capi/string_spec.rb64
1 files changed, 48 insertions, 16 deletions
diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb
index 5ad8169e8e..ac23198662 100644
--- a/spec/ruby/optional/capi/string_spec.rb
+++ b/spec/ruby/optional/capi/string_spec.rb
@@ -4,6 +4,30 @@ require_relative '../../shared/string/times'
load_extension('string')
+class CApiStringSpecs
+ class ValidTostrTest
+ def to_str
+ "ruby"
+ end
+ end
+
+ class InvalidTostrTest
+ def to_str
+ []
+ end
+ end
+
+ class ToSOrInspect
+ def to_s
+ 'A string'
+ end
+
+ def inspect
+ 'A different string'
+ end
+ end
+end
+
describe :rb_str_new2, shared: true do
it "returns a new string object calling strlen on the passed C string" do
# Hardcoded to pass const char * = "hello\0invisible"
@@ -20,18 +44,6 @@ describe "C-API String function" do
@s = CApiStringSpecs.new
end
- class ValidTostrTest
- def to_str
- "ruby"
- end
- end
-
- class InvalidTostrTest
- def to_str
- []
- end
- end
-
[Encoding::BINARY, Encoding::UTF_8].each do |enc|
describe "rb_str_set_len on a #{enc.name} String" do
before :each do
@@ -438,12 +450,12 @@ describe "C-API String function" do
describe "rb_str_to_str" do
it "calls #to_str to coerce the value to a String" do
@s.rb_str_to_str("foo").should == "foo"
- @s.rb_str_to_str(ValidTostrTest.new).should == "ruby"
+ @s.rb_str_to_str(CApiStringSpecs::ValidTostrTest.new).should == "ruby"
end
it "raises a TypeError if coercion fails" do
lambda { @s.rb_str_to_str(0) }.should raise_error(TypeError)
- lambda { @s.rb_str_to_str(InvalidTostrTest.new) }.should raise_error(TypeError)
+ lambda { @s.rb_str_to_str(CApiStringSpecs::InvalidTostrTest.new) }.should raise_error(TypeError)
end
end
@@ -875,6 +887,26 @@ describe "C-API String function" do
s = "Awesome %s is here with %s"
@s.rb_sprintf2(s, "string", "content").should == "Awesome string is here with content"
end
+
+ it "formats a string VALUE using to_s if sign not specified in format" do
+ s = 'Result: A string.'
+ @s.rb_sprintf3(CApiStringSpecs::ToSOrInspect.new).should == s
+ end
+
+ it "formats a string VALUE using inspect if sign specified in format" do
+ s = 'Result: A different string.'
+ @s.rb_sprintf4(CApiStringSpecs::ToSOrInspect.new).should == s
+ end
+
+ it "formats a TrueClass VALUE as `TrueClass` if sign not specified in format" do
+ s = 'Result: TrueClass.'
+ @s.rb_sprintf3(true.class).should == s
+ end
+
+ it "formats a TrueClass VALUE as 'true' if sign specified in format" do
+ s = 'Result: true.'
+ @s.rb_sprintf4(true.class).should == s
+ end
end
describe "rb_vsprintf" do
@@ -890,11 +922,11 @@ describe "C-API String function" do
end
it "tries to convert the passed argument to a string by calling #to_str first" do
- @s.rb_String(ValidTostrTest.new).should == "ruby"
+ @s.rb_String(CApiStringSpecs::ValidTostrTest.new).should == "ruby"
end
it "raises a TypeError if #to_str does not return a string" do
- lambda { @s.rb_String(InvalidTostrTest.new) }.should raise_error(TypeError)
+ lambda { @s.rb_String(CApiStringSpecs::InvalidTostrTest.new) }.should raise_error(TypeError)
end
it "tries to convert the passed argument to a string by calling #to_s" do