Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add specs for rb_sprintf

  • Loading branch information...
commit a8f5d6fb6781247e3d4dd430f0595adcf6d4b5e3 1 parent 2bec5be
@dbussink dbussink authored brixen committed
View
1  optional/capi/ext/rubyspec.h
@@ -490,6 +490,7 @@
#define HAVE_STRINGVALUE 1
#ifdef RUBY_VERSION_IS_1_9
+#define HAVE_RB_SPRINTF 1
#define HAVE_RB_LOCALE_STR_NEW 1
#define HAVE_RB_LOCALE_STR_NEW_CSTR 1
#define HAVE_RB_STR_CONV_ENC 1
View
14 optional/capi/ext/string_spec.c
@@ -543,6 +543,15 @@ static VALUE string_spec_rb_str_hash(VALUE self, VALUE str) {
}
#endif
+#ifdef HAVE_RB_SPRINTF
+static VALUE string_spec_rb_sprintf1(VALUE self, VALUE str, VALUE repl) {
+ return rb_sprintf(RSTRING_PTR(str), RSTRING_PTR(repl));
+}
+static VALUE string_spec_rb_sprintf2(VALUE self, VALUE str, VALUE repl1, VALUE repl2) {
+ return rb_sprintf(RSTRING_PTR(str), RSTRING_PTR(repl1), RSTRING_PTR(repl2));
+}
+#endif
+
#ifdef HAVE_RB_STR_EQUAL
VALUE string_spec_rb_str_equal(VALUE self, VALUE str1, VALUE str2) {
return rb_str_equal(str1, str2);
@@ -785,6 +794,11 @@ void Init_string_spec() {
rb_define_method(cls, "rb_str_hash", string_spec_rb_str_hash, 1);
#endif
+#ifdef HAVE_RB_SPRINTF
+ rb_define_method(cls, "rb_sprintf1", string_spec_rb_sprintf1, 2);
+ rb_define_method(cls, "rb_sprintf2", string_spec_rb_sprintf2, 3);
+#endif
+
#ifdef HAVE_RB_STR_EQUAL
rb_define_method(cls, "rb_str_equal", string_spec_rb_str_equal, 2);
#endif
View
13 optional/capi/string_spec.rb
@@ -805,5 +805,18 @@ def to_str
s.encoding.should equal(Encoding.find("locale"))
end
end
+
+ describe "rb_sprintf" do
+ it "replaces the parts like sprintf" do
+ s = "Awesome %s is replaced"
+ @s.rb_sprintf1(s, "string").should == "Awesome string is replaced"
+ end
+
+ it "accepts multiple arguments" do
+ s = "Awesome %s is here with %s"
+ @s.rb_sprintf2(s, "string", "content").should == "Awesome string is here with content"
+ end
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.