Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Changed to ActiveSupport's implementation of constantize so as not to…

… use ObjectSpace.
  • Loading branch information...
commit b57acfea8d8b4be5da3aa4e55afac3d24ca5098f 1 parent 1869e4b
Myron Marston authored
View
4 CHANGELOG
@@ -1,3 +1,7 @@
== 1.0.0 2009-03-27
* Initial release
+
+== 1.0.1 2009-03-27
+
+* Changed to ActiveSupport's implementation of constantize so as not to use ObjectSpace.
View
55 lib/test_benchmarker/core_ext.rb
@@ -1,18 +1,47 @@
-class TestBenchmarker::ClassNotFoundError < StandardError
- attr_accessor :class_str
- def initialize(class_str)
- @class_str = class_str
- end
-end
-
class String
- # similar to ActiveSupports constantize, but doesn't require the use of active support.
- def to_class
- ObjectSpace.each_object(Class) do |klass|
- return klass if klass.to_s == self
+ # ported from ActiveSupport, so that we don't have to require all of active support for this gem.
+ # http://github.com/rails/rails/blob/dd2eb1ea7c34eb6496feaf7e42100f37a8dae76b/activesupport/lib/active_support/inflector.rb#L335-376
+ # Ruby 1.9 introduces an inherit argument for Module#const_get and
+ # #const_defined? and changes their default behavior.
+ if Module.method(:const_get).arity == 1
+ # Tries to find a constant with the name specified in the argument string:
+ #
+ # "Module".constantize # => Module
+ # "Test::Unit".constantize # => Test::Unit
+ #
+ # The name is assumed to be the one of a top-level constant, no matter whether
+ # it starts with "::" or not. No lexical context is taken into account:
+ #
+ # C = 'outside'
+ # module M
+ # C = 'inside'
+ # C # => 'inside'
+ # "C".constantize # => 'outside', same as ::C
+ # end
+ #
+ # NameError is raised when the name is not in CamelCase or the constant is
+ # unknown.
+ def constantize
+ names = self.split('::')
+ names.shift if names.empty? || names.first.empty?
+
+ constant = Object
+ names.each do |name|
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
+ end
+ constant
+ end
+ else
+ def constantize
+ names = self.split('::')
+ names.shift if names.empty? || names.first.empty?
+
+ constant = Object
+ names.each do |name|
+ constant = constant.const_get(name, false) || constant.const_missing(name)
+ end
+ constant
end
-
- raise TestBenchmarker::ClassNotFoundError.new(self), "A class matching '#{self}' could not be found"
end
end
View
4 lib/test_benchmarker/test_benchmarks.rb
@@ -25,8 +25,8 @@ def self.add(benchmark)
return if test_class =~ /(rake_test_loader|::TestCase|::IntegrationTest)/
begin
- test_class = test_class.to_class
- rescue TestBenchmarker::ClassNotFoundError
+ test_class = test_class.constantize
+ rescue NameError
return
end
return unless test_class.is_subclass_of?(Test::Unit::TestCase)
View
12 test/test_core_ext.rb
@@ -6,14 +6,14 @@ class FooSubSubclass < FooSubclass; end
class Bar; end
class TestCoreExt < Test::Unit::TestCase
- def test_string_to_class
- assert_equal TestCoreExt, 'TestCoreExt'.to_class
- assert_equal FooSubSubclass, 'FooSubSubclass'.to_class
+ def test_string_constantize
+ assert_equal TestCoreExt, 'TestCoreExt'.constantize
+ assert_equal FooSubSubclass, 'FooSubSubclass'.constantize
end
- def test_string_to_class_not_found
- assert_raise TestBenchmarker::ClassNotFoundError do
- 'This it not the name of a class!'.to_class
+ def test_string_constantize_for_bogus_class
+ assert_raise NameError do
+ 'This it not the name of a class!'.constantize
end
end
View
2  test_benchmarker.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "test_benchmarker"
- s.version = "1.0.0"
+ s.version = "1.0.1"
s.date = "2009-03-27"
s.summary = "A tool for benchmarking ruby Test::Unit tests"
s.homepage = "http://github.com/myronmarston/test_benchmarker"
Please sign in to comment.
Something went wrong with that request. Please try again.