blob: 52bfeb7179aeb8e32bf21652d1fa6159788dd2fd (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
require "active_support/concern"
require "active_support/inflector"
module ActiveSupport
module Testing
# Resolves a constant from a minitest spec name.
#
# Given the following spec-style test:
#
# describe WidgetsController, :index do
# describe "authenticated user" do
# describe "returns widgets" do
# it "has a controller that exists" do
# assert_kind_of WidgetsController, @controller
# end
# end
# end
# end
#
# The test will have the following name:
#
# "WidgetsController::index::authenticated user::returns widgets"
#
# The constant WidgetsController can be resolved from the name.
# The following code will resolve the constant:
#
# controller = determine_constant_from_test_name(name) do |constant|
# Class === constant && constant < ::ActionController::Metal
# end
module ConstantLookup
extend ::ActiveSupport::Concern
module ClassMethods # :nodoc:
def determine_constant_from_test_name(test_name)
names = test_name.split "::"
while names.size > 0 do
names.last.sub!(/Test$/, "")
begin
constant = names.join("::").constantize
break(constant) if yield(constant)
rescue NameError
# Constant wasn't found, move on
ensure
names.pop
end
end
end
end
end
end
end
|