Revision
807
Author
eloy.de.enige@gmail.com
Date
2009-01-22 08:30:50 -0800 (Thu, 22 Jan 2009)

Log Message

Added tests for hotcocoa/object_ext.rb from upstream and missing ones. Also added poper copyright and license attribution to Sam Smoot.

Modified Paths

Added Paths

Diff

Modified: MacRuby/trunk/lib/hotcocoa/object_ext.rb (806 => 807)


--- MacRuby/trunk/lib/hotcocoa/object_ext.rb	2009-01-22 16:30:41 UTC (rev 806)
+++ MacRuby/trunk/lib/hotcocoa/object_ext.rb	2009-01-22 16:30:50 UTC (rev 807)
@@ -1,5 +1,7 @@
+# Object.full_const_get was taken from the ‘extlib’ project:
+# http://github.com/sam/extlib which is released under a MIT License and
+# copyrighted by Sam Smoot (2008).
 
-# This is ripped from sam/extlib and changed a little bit.
 class Object
 
   # @param name<String> The name of the constant to get, e.g. "Merb::Router".
@@ -7,13 +9,12 @@
   # @return <Object> The constant corresponding to the name.
   def full_const_get(name)
     list = name.split("::")
-    list.shift if list.first.strip.empty?
+    list.shift if list.first.empty?
     obj = self
     list.each do |x|
       # This is required because const_get tries to look for constants in the
       # ancestor chain, but we only want constants that are HERE
-      raise NameError, "uninitialized constant #{self.name}::#{name}" unless obj.const_defined?(x)
-      obj = obj.const_get(x)
+      obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
     end
     obj
   end

Added: MacRuby/trunk/test-macruby/cases/hotcocoa/object_ext_test.rb (0 => 807)


--- MacRuby/trunk/test-macruby/cases/hotcocoa/object_ext_test.rb	                        (rev 0)
+++ MacRuby/trunk/test-macruby/cases/hotcocoa/object_ext_test.rb	2009-01-22 16:30:50 UTC (rev 807)
@@ -0,0 +1,32 @@
+#!/usr/bin/env macruby
+
+require File.expand_path('../../../test_helper', __FILE__)
+require 'hotcocoa'
+
+module TestNamespaceForConstLookup
+  def self.const_missing(const)
+    @missing_const = const
+  end
+  
+  def self.missing_const
+    @missing_const
+  end
+end
+
+class TestObjectExt < Test::Unit::TestCase
+  it 'should return a constant by FQ name _in_ receiver namespace' do
+    assert_equal HotCocoa,           Object.full_const_get("HotCocoa")
+    assert_equal HotCocoa::Mappings, Object.full_const_get("HotCocoa::Mappings")
+  end
+  
+  it "should call ::const_missing on the namespace which _does_ exist" do
+    Object.full_const_get('TestNamespaceForConstLookup::DoesNotExist')
+    assert_equal 'DoesNotExist', TestNamespaceForConstLookup.missing_const
+  end
+  
+  it "should normally raise a NameError if a const cannot be found" do
+    assert_raise(NameError) do
+      Object.full_const_get('DoesNotExist::ForSure')
+    end
+  end
+end
\ No newline at end of file