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 +4 -0
- data/lib/test_benchmarker/core_ext.rb +42 -13
- data/lib/test_benchmarker/test_benchmarks.rb +2 -2
- data/test/test_core_ext.rb +6 -6
- metadata +1 -1
data/CHANGELOG
CHANGED
@@ -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
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
|
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.
|
29
|
-
rescue
|
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)
|
data/test/test_core_ext.rb
CHANGED
@@ -6,14 +6,14 @@ class FooSubSubclass < FooSubclass; end
|
|
6
6
|
class Bar; end
|
7
7
|
|
8
8
|
class TestCoreExt < Test::Unit::TestCase
|
9
|
-
def
|
10
|
-
assert_equal TestCoreExt, 'TestCoreExt'.
|
11
|
-
assert_equal FooSubSubclass, 'FooSubSubclass'.
|
9
|
+
def test_string_constantize
|
10
|
+
assert_equal TestCoreExt, 'TestCoreExt'.constantize
|
11
|
+
assert_equal FooSubSubclass, 'FooSubSubclass'.constantize
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
assert_raise
|
16
|
-
'This it not the name of a 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
|
|