myronmarston-test_benchmarker 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
1
  == 1.0.0 2009-03-27
2
2
 
3
3
  * Initial release
4
+
5
+ == 1.0.1 2009-03-27
6
+
7
+ * Changed to ActiveSupport's implementation of constantize so as not to use ObjectSpace.
@@ -1,18 +1,47 @@
1
- class TestBenchmarker::ClassNotFoundError < StandardError
2
- attr_accessor :class_str
3
- def initialize(class_str)
4
- @class_str = class_str
5
- end
6
- end
7
-
8
1
  class String
9
- # similar to ActiveSupports constantize, but doesn't require the use of active support.
10
- def to_class
11
- ObjectSpace.each_object(Class) do |klass|
12
- return klass if klass.to_s == self
2
+ # ported from ActiveSupport, so that we don't have to require all of active support for this gem.
3
+ # http://github.com/rails/rails/blob/dd2eb1ea7c34eb6496feaf7e42100f37a8dae76b/activesupport/lib/active_support/inflector.rb#L335-376
4
+ # Ruby 1.9 introduces an inherit argument for Module#const_get and
5
+ # #const_defined? and changes their default behavior.
6
+ if Module.method(:const_get).arity == 1
7
+ # Tries to find a constant with the name specified in the argument string:
8
+ #
9
+ # "Module".constantize # => Module
10
+ # "Test::Unit".constantize # => Test::Unit
11
+ #
12
+ # The name is assumed to be the one of a top-level constant, no matter whether
13
+ # it starts with "::" or not. No lexical context is taken into account:
14
+ #
15
+ # C = 'outside'
16
+ # module M
17
+ # C = 'inside'
18
+ # C # => 'inside'
19
+ # "C".constantize # => 'outside', same as ::C
20
+ # end
21
+ #
22
+ # NameError is raised when the name is not in CamelCase or the constant is
23
+ # unknown.
24
+ def constantize
25
+ names = self.split('::')
26
+ names.shift if names.empty? || names.first.empty?
27
+
28
+ constant = Object
29
+ names.each do |name|
30
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
31
+ end
32
+ constant
33
+ end
34
+ else
35
+ def constantize
36
+ names = self.split('::')
37
+ names.shift if names.empty? || names.first.empty?
38
+
39
+ constant = Object
40
+ names.each do |name|
41
+ constant = constant.const_get(name, false) || constant.const_missing(name)
42
+ end
43
+ constant
13
44
  end
14
-
15
- raise TestBenchmarker::ClassNotFoundError.new(self), "A class matching '#{self}' could not be found"
16
45
  end
17
46
  end
18
47
 
@@ -25,8 +25,8 @@ module TestBenchmarker
25
25
  return if test_class =~ /(rake_test_loader|::TestCase|::IntegrationTest)/
26
26
 
27
27
  begin
28
- test_class = test_class.to_class
29
- rescue TestBenchmarker::ClassNotFoundError
28
+ test_class = test_class.constantize
29
+ rescue NameError
30
30
  return
31
31
  end
32
32
  return unless test_class.is_subclass_of?(Test::Unit::TestCase)
@@ -6,14 +6,14 @@ class FooSubSubclass < FooSubclass; end
6
6
  class Bar; end
7
7
 
8
8
  class TestCoreExt < Test::Unit::TestCase
9
- def test_string_to_class
10
- assert_equal TestCoreExt, 'TestCoreExt'.to_class
11
- assert_equal FooSubSubclass, 'FooSubSubclass'.to_class
9
+ def test_string_constantize
10
+ assert_equal TestCoreExt, 'TestCoreExt'.constantize
11
+ assert_equal FooSubSubclass, 'FooSubSubclass'.constantize
12
12
  end
13
13
 
14
- def test_string_to_class_not_found
15
- assert_raise TestBenchmarker::ClassNotFoundError do
16
- 'This it not the name of a class!'.to_class
14
+ def test_string_constantize_for_bogus_class
15
+ assert_raise NameError do
16
+ 'This it not the name of a class!'.constantize
17
17
  end
18
18
  end
19
19
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myronmarston-test_benchmarker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Myron Marston