diff --git a/ChangeLog b/ChangeLog index a8326a3419..667a7d0949 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +Sun Oct 14 17:31:48 2007 Nobuyoshi Nakada + + * encoding.c (rb_obj_encoding): rdoc update. a patch from David + Flanagan . [ruby-core:12664] + + * encoding.c (enc_dump, enc_load): marshaling feature. a patch from + David Flanagan. [ruby-core:12665] + + * encoding.c (Init_Encoding): undefine allocator of Encoding. + [ruby-core:12665], [ruby-core:12666] + + * test/ruby/test_encoding.rb: tests for Encoding from David Flanagan + [ruby-core:12665] + Sun Oct 14 11:09:09 2007 Tanaka Akira * lib/pp.rb (PP::PPMethods#pp_hash): don't sort keys because hash is diff --git a/encoding.c b/encoding.c index eecb808083..ab8a020a01 100644 --- a/encoding.c +++ b/encoding.c @@ -2,8 +2,8 @@ encoding.c - - $Author: matz $ - $Date: 2007-05-24 17:22:33 +0900 (Thu, 24 May 2007) $ + $Author$ + $Date$ created at: Thu May 24 17:23:27 JST 2007 Copyright (C) 2007 Yukihiro Matsumoto @@ -372,9 +372,9 @@ rb_enc_copy(VALUE obj1, VALUE obj2) /* * call-seq: - * obj.encoding => str + * obj.encoding => encoding * - * Retruns the encoding name. + * Returns the Encoding object that represents the encoding of obj. */ VALUE @@ -498,12 +498,31 @@ enc_find(VALUE klass, VALUE enc) return enc_from_encoding(rb_enc_from_index(idx)); } +/* :nodoc: */ +static VALUE +enc_dump(int argc, VALUE *argv, VALUE self) +{ + rb_scan_args(argc, argv, "01", 0); + return enc_name(self); +} + +/* :nodoc: */ +static VALUE +enc_load(VALUE klass, VALUE str) +{ + return enc_find(klass, str); +} + void Init_Encoding(void) { rb_cEncoding = rb_define_class("Encoding", rb_cObject); + rb_undef_alloc_func(rb_cEncoding); rb_define_method(rb_cEncoding, "inspect", enc_inspect, 0); rb_define_method(rb_cEncoding, "name", enc_name, 0); rb_define_singleton_method(rb_cEncoding, "list", enc_list, 0); rb_define_singleton_method(rb_cEncoding, "find", enc_find, 1); + + rb_define_method(rb_cEncoding, "_dump", enc_dump, -1); + rb_define_singleton_method(rb_cEncoding, "_load", enc_load, 1); } diff --git a/test/ruby/test_encoding.rb b/test/ruby/test_encoding.rb new file mode 100644 index 0000000000..a9bcd8eb97 --- /dev/null +++ b/test/ruby/test_encoding.rb @@ -0,0 +1,28 @@ +require 'test/unit' + +class TestEncoding < Test::Unit::TestCase + + # Test basic encoding methods: list, find, name + def test_encoding + encodings = Encoding.list + assert_equal(encodings.empty?, false) + + encodings.each do |e| + assert_equal(e, Encoding.find(e.name)) + assert_equal(e, Encoding.find(e.name.upcase)) + assert_equal(e, Encoding.find(e.name.capitalize)) + assert_equal(e, Encoding.find(e.name.downcase)) + end + end + + # Test that Encoding objects can't be copied + # And that they can be compared by object_id + def test_singleton + encodings = Encoding.list + encodings.each do |e| + assert_raise(TypeError) { e.dup } + assert_raise(TypeError) { e.clone } + assert_equal(e.object_id, Marshal.load(Marshal.dump(e)).object_id) + end + end +end