[前][次][番号順一覧][スレッド一覧]

ruby-changes:13051

From: wyhaines <ko1@a...>
Date: Wed, 9 Sep 2009 01:16:41 +0900 (JST)
Subject: [ruby-changes:13051] Ruby:r24797 (ruby_1_8_6): Openstruct fix, and fix to test_file_exhaustive.

wyhaines	2009-09-09 01:13:26 +0900 (Wed, 09 Sep 2009)

  New Revision: 24797

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=24797

  Log:
    Openstruct fix, and fix to test_file_exhaustive.

  Modified files:
    branches/ruby_1_8_6/ChangeLog
    branches/ruby_1_8_6/lib/ostruct.rb
    branches/ruby_1_8_6/test/ostruct/test_ostruct.rb
    branches/ruby_1_8_6/test/ruby/test_file_exhaustive.rb
    branches/ruby_1_8_6/version.h

Index: ruby_1_8_6/ChangeLog
===================================================================
--- ruby_1_8_6/ChangeLog	(revision 24796)
+++ ruby_1_8_6/ChangeLog	(revision 24797)
@@ -1,3 +1,9 @@
+Fri Aug 28 12:54:00 2009 Kirk Haines <khaines@r...>
+
+	* Backport #1524 [ruby-core:23567]; lib/ostrct.rb (OpenStruct@new_ostruct_member): check if it's; also a little code cleanup to do a few things more efficiently. Tests adjusted accordingly.
+
+	* test/ruby/test_file_exhaustive.rb: Fixed a mistake in the monkey patched String#start_with? method required by the test suite.
+
 Tue Aug 25 11:48:00 2009 Kirk Haines <khaines@r...>
 
 	* Backport #1168; Wrong result of File.extname for a path that contains a space before the extension.
Index: ruby_1_8_6/version.h
===================================================================
--- ruby_1_8_6/version.h	(revision 24796)
+++ ruby_1_8_6/version.h	(revision 24797)
@@ -1,15 +1,15 @@
 #define RUBY_VERSION "1.8.6"
-#define RUBY_RELEASE_DATE "2009-08-26"
+#define RUBY_RELEASE_DATE "2009-09-08"
 #define RUBY_VERSION_CODE 186
-#define RUBY_RELEASE_CODE 20090826
-#define RUBY_PATCHLEVEL 387
+#define RUBY_RELEASE_CODE 20090908
+#define RUBY_PATCHLEVEL 388
 
 #define RUBY_VERSION_MAJOR 1
 #define RUBY_VERSION_MINOR 8
 #define RUBY_VERSION_TEENY 6
 #define RUBY_RELEASE_YEAR 2009
-#define RUBY_RELEASE_MONTH 8
-#define RUBY_RELEASE_DAY 26
+#define RUBY_RELEASE_MONTH 9
+#define RUBY_RELEASE_DAY 8
 
 #ifdef RUBY_EXTERN
 RUBY_EXTERN const char ruby_version[];
Index: ruby_1_8_6/lib/ostruct.rb
===================================================================
--- ruby_1_8_6/lib/ostruct.rb	(revision 24796)
+++ ruby_1_8_6/lib/ostruct.rb	(revision 24797)
@@ -67,28 +67,33 @@
     @table.each_key{|key| new_ostruct_member(key)}
   end
 
+  def modifiable
+    if self.frozen?
+      raise TypeError, "can't modify frozen #{self.class}", caller(2)
+    end
+    @table
+  end
+  protected :modifiable
+
   def new_ostruct_member(name)
     name = name.to_sym
     unless self.respond_to?(name)
-      meta = class << self; self; end
-      meta.send(:define_method, name) { @table[name] }
-      meta.send(:define_method, :"#{name}=") { |x| @table[name] = x }
+      class << self; self; end.class_eval do
+        define_method(name) { @table[name] }
+        define_method("#{name}=") { |x| modifiable[name] = x }
+      end
     end
+    name
   end
 
   def method_missing(mid, *args) # :nodoc:
     mname = mid.id2name
     len = args.length
-    if mname =~ /=$/
+    if mname.chomp!('=')
       if len != 1
         raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
       end
-      if self.frozen?
-        raise TypeError, "can't modify frozen #{self.class}", caller(1)
-      end
-      mname.chop!
-      self.new_ostruct_member(mname)
-      @table[mname.intern] = args[0]
+      modifiable[new_ostruct_member(mname)] = args[0]
     elsif len == 0
       @table[mid]
     else
Index: ruby_1_8_6/test/ruby/test_file_exhaustive.rb
===================================================================
--- ruby_1_8_6/test/ruby/test_file_exhaustive.rb	(revision 24796)
+++ ruby_1_8_6/test/ruby/test_file_exhaustive.rb	(revision 24797)
@@ -3,8 +3,8 @@
 require "tmpdir"
 
 class String
-  def start_with(prefix)
-    self =~ /^#{prefix}/
+  def start_with?(prefix)
+    self[0..(prefix.length - 1)] == prefix
   end
 end
 
Index: ruby_1_8_6/test/ostruct/test_ostruct.rb
===================================================================
--- ruby_1_8_6/test/ostruct/test_ostruct.rb	(revision 24796)
+++ ruby_1_8_6/test/ostruct/test_ostruct.rb	(revision 24797)
@@ -2,6 +2,21 @@
 require 'ostruct'
 
 class TC_OpenStruct < Test::Unit::TestCase
+  def assert_not_respond_to(object, method, message="")
+    _wrap_assertion do
+      full_message = build_message(message, <<EOT, object, object.class, method)
+<?>
+of type <?>
+expected not to respond_to\\?<?>.
+EOT
+      _wrap_assertion do
+        if object.respond_to?(method)
+          raise Test::Unit::AssertionFailedError, full_message, caller(5)
+        end
+      end
+    end
+  end
+
   def test_equality
     o1 = OpenStruct.new
     o2 = OpenStruct.new
@@ -23,9 +38,23 @@
   
   def test_inspect
     foo = OpenStruct.new
+    assert_equal("#<OpenStruct>", foo.inspect)
+    foo.bar = 1
+    foo.baz = 2
+    assert_equal("#<OpenStruct bar=1, baz=2>", foo.inspect)
+
+    foo = OpenStruct.new
     foo.bar = OpenStruct.new
     assert_equal('#<OpenStruct bar=#<OpenStruct>>', foo.inspect)
     foo.bar.foo = foo
     assert_equal('#<OpenStruct bar=#<OpenStruct foo=#<OpenStruct ...>>>', foo.inspect)
   end
+
+  def test_frozen
+    o = OpenStruct.new
+    o.a = 'a'
+    o.freeze
+    assert_raise(TypeError) {o.b = 'b'}
+    assert_not_respond_to(o, :b)
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]