/
class_test.rb
81 lines (65 loc) · 2.87 KB
/
class_test.rb
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'test_helper'
require 'lotus/utils/class'
describe Lotus::Utils::Class do
before do
module App
module Layer
class Step
end
end
module Service
class Point
end
end
class ServicePoint
end
end
end
describe '.load!' do
it 'loads the class from the given static string' do
Lotus::Utils::Class.load!('App::Layer::Step').must_equal(App::Layer::Step)
end
it 'raises an error in case of missing class' do
-> { Lotus::Utils::Class.load!('Missing') }.must_raise(NameError)
end
it 'prints a deprecation warning if used with a pattern' do
_, err = capture_io do
Lotus::Utils::Class.load!('(Layer|Layer::)Step', App).must_equal(App::Layer::Step)
end
err.must_include "Using Lotus::Utils::Class.load! with a pattern is deprecated, please use Lotus::Utils::Class.load_from_pattern!: (Layer|Layer::)Step, App"
end
end
describe '.load_from_pattern!' do
it 'loads the class from the given static string' do
Lotus::Utils::Class.load_from_pattern!('App::Layer::Step').must_equal(App::Layer::Step)
end
it 'raises error for missing constant' do
error = -> { Lotus::Utils::Class.load_from_pattern!('MissingConstant') }.must_raise(NameError)
error.message.must_equal "uninitialized constant MissingConstant"
end
it 'raises error for missing constant with multiple alternatives' do
error = -> { Lotus::Utils::Class.load_from_pattern!('Missing(Constant|Class)') }.must_raise(NameError)
error.message.must_equal "uninitialized constant Missing(Constant|Class)"
end
it 'raises error with full constant name' do
error = -> { Lotus::Utils::Class.load_from_pattern!('Step', App) }.must_raise(NameError)
error.message.must_equal "uninitialized constant App::Step"
end
it 'raises error with full constant name and multiple alternatives' do
error = -> { Lotus::Utils::Class.load_from_pattern!('(Step|Point)', App) }.must_raise(NameError)
error.message.must_equal "uninitialized constant App::(Step|Point)"
end
it 'loads the class from given string, by interpolating tokens' do
Lotus::Utils::Class.load_from_pattern!('App::Service(::Point|Point)').must_equal(App::Service::Point)
end
it 'loads the class from given string, by interpolating string tokens and respecting their order' do
Lotus::Utils::Class.load_from_pattern!('App::Service(Point|::Point)').must_equal(App::ServicePoint)
end
it 'loads the class from given string, by interpolating tokens and not stopping after first fail' do
Lotus::Utils::Class.load_from_pattern!('App::(Layer|Layer::)Step').must_equal(App::Layer::Step)
end
it 'loads class from given string and namespace' do
Lotus::Utils::Class.load_from_pattern!('(Layer|Layer::)Step', App).must_equal(App::Layer::Step)
end
end
end