Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added some rb_str_encode C-API specs.

  • Loading branch information...
commit d09f636ba05952126ee4c65240600cbc89a3e5b0 1 parent 88402f1
@brixen brixen authored
View
1  spec/ruby/optional/capi/ext/rubyspec.h
@@ -201,6 +201,7 @@
#define HAVE_RB_ENC_TO_INDEX 1
#define HAVE_RB_OBJ_ENCODING 1
+#define HAVE_RB_STR_ENCODE 1
#define HAVE_RB_STR_NEW_CSTR 1
#define HAVE_RB_USASCII_STR_NEW 1
#define HAVE_RB_USASCII_STR_NEW_CSTR 1
View
10 spec/ruby/optional/capi/ext/string_spec.c
@@ -171,6 +171,12 @@ VALUE string_spec_rb_str_new2(VALUE self, VALUE str) {
}
#endif
+#ifdef HAVE_RB_STR_ENCODE
+VALUE string_spec_rb_str_encode(VALUE self, VALUE str, VALUE enc, VALUE flags, VALUE opts) {
+ return rb_str_encode(str, enc, FIX2INT(flags), opts);
+}
+#endif
+
#ifdef HAVE_RB_STR_NEW_CSTR
VALUE string_spec_rb_str_new_cstr(VALUE self, VALUE str) {
if(NIL_P(str)) {
@@ -559,6 +565,10 @@ void Init_string_spec() {
rb_define_method(cls, "rb_str_new2", string_spec_rb_str_new2, 1);
#endif
+#ifdef HAVE_RB_STR_ENCODE
+ rb_define_method(cls, "rb_str_encode", string_spec_rb_str_encode, 4);
+#endif
+
#ifdef HAVE_RB_STR_NEW_CSTR
rb_define_method(cls, "rb_str_new_cstr", string_spec_rb_str_new_cstr, 1);
#endif
View
36 spec/ruby/optional/capi/string_spec.rb
@@ -155,6 +155,42 @@ def to_str
result.encoding.should == Encoding::US_ASCII
end
end
+
+ describe "rb_str_encode" do
+ it "returns a String in the destination encoding" do
+ result = @s.rb_str_encode("abc", Encoding::ISO_8859_1, 0, nil)
+ result.encoding.should == Encoding::ISO_8859_1
+ end
+
+ it "transcodes the String" do
+ result = @s.rb_str_encode("ありがとう", "euc-jp", 0, nil)
+ result.should == "\xa4\xa2\xa4\xea\xa4\xac\xa4\xc8\xa4\xa6".force_encoding("euc-jp")
+ result.encoding.should == Encoding::EUC_JP
+ end
+
+ it "returns a dup of the original String" do
+ a = "abc"
+ b = @s.rb_str_encode("abc", "us-ascii", 0, nil)
+ a.should_not equal(b)
+ end
+
+ it "accepts encoding flags" do
+ result = @s.rb_str_encode("a\xffc", "us-ascii",
+ Encoding::Converter::INVALID_REPLACE, nil)
+ result.should == "a?c"
+ result.encoding.should == Encoding::US_ASCII
+ end
+
+ it "accepts an encoding options Hash specifying replacement String" do
+ # Yeah, MRI aborts with rb_bug() if the options Hash is not frozen
+ options = { :replace => "b" }.freeze
+ result = @s.rb_str_encode("a\xffc", "us-ascii",
+ Encoding::Converter::INVALID_REPLACE,
+ options)
+ result.should == "abc"
+ result.encoding.should == Encoding::US_ASCII
+ end
+ end
end
describe "rb_str_new3" do
Please sign in to comment.
Something went wrong with that request. Please try again.