-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflection_utils_spec.rb
194 lines (161 loc) · 6.12 KB
/
reflection_utils_spec.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require "spec_helper"
RSpec.describe ReflectionUtils do
it "has a version number" do
expect(ReflectionUtils::VERSION).not_to be nil
end
before :each do
@test_value = 5
class Example
def initialize(value = nil)
@value = value
end
def method(msg)
@value
end
def instance_method_1;end
def instance_method_2;end
def self.class_method_1;end
def self.class_method_2;end
end
@test_instance = Example.new(@test_value)
end
describe ".get_bound_instance_method" do
context "Given an instance and method name" do
it "Will return a method" do
method = ReflectionUtils.get_bound_instance_method(instance: @test_instance, method_name: :method)
expect(method).to be_a(Method)
end
it "Will return the method bound to the instance" do
method = ReflectionUtils.get_bound_instance_method(instance: @test_instance, method_name: :method)
expect(method.receiver).to eq(@test_instance)
end
end
end
describe ".get_class_constant" do
context "Given an instance" do
it "Will return instance's class object" do
clazz = ReflectionUtils.get_class_constant(instance: @test_instance)
expect(clazz).to be_a(Example.class)
end
end
end
describe ".call" do
context "Given a Method" do
it "Will call the method" do
method = ReflectionUtils.get_bound_instance_method(instance: @test_instance, method_name: :method)
expect(method).to receive(:call)
ReflectionUtils.call(method)
end
end
context "Given a Method with parameters" do
it "Will call the Method with parameters" do
method = ReflectionUtils.get_bound_instance_method(instance: @test_instance, method_name: :method)
params = 'hi'
expect(method).to receive(:call).with(params)
ReflectionUtils.call(method, params)
end
end
context "Given a lambda" do
it "Will call the method" do
callback = lambda {}
expect(callback).to receive(:call)
ReflectionUtils.call(callback)
end
end
context "Given a lambda with parameters" do
it "Will call the lambda with parameters" do
params = 'hi'
callback = lambda { |msg| }
expect(callback).to receive(:call).with(params)
ReflectionUtils.call(callback, params)
end
end
context "Given a Proc" do
it "Will call the Proc" do
callback = Proc.new {}
expect(callback).to receive(:call)
ReflectionUtils.call(callback)
end
end
context "Given a Proc with parameters" do
it "Will call the Proc with parameters" do
params = 'hi'
callback = Proc.new { |msg| }
expect(callback).to receive(:call).with(params)
ReflectionUtils.call(callback, params)
end
end
end
describe ".has_parameter?" do
context "Given a lambda / Proc / Method, parameter_index and parameter" do
it "Will return true if lambda has given parameter on given index" do
expect(ReflectionUtils.has_parameter?(lambda { |msg| }, 0, [:req, :msg])).to be true
end
it "Will return false if Proc has given parameter on different index" do
expect(ReflectionUtils.has_parameter?(Proc.new { |msg| }, 1, [:req, :msg])).to be false
end
it "Will return false if Method doesn't have given parameter" do
method = Example.instance_method(:method)
expect(ReflectionUtils.has_parameter?(method, 0, [:req, :something_else])).to be false
end
end
end
describe ".has_parameters?" do
context "Given a lambda / Proc / Method and parameters" do
it "Will return if Method has given parameter" do
method = Example.instance_method(:method)
expect(ReflectionUtils.has_parameters?(method, [[:req, :msg]])).to be true
end
it "Will return if lambda has given parameters" do
expect(ReflectionUtils.has_parameters?(lambda { |a, b| }, [[:req, :a], [:req, :b]])).to be true
end
it "Will return if Proc has given parameters in given order" do
expect(ReflectionUtils.has_parameters?(Proc.new { |a, b| }, [[:opt, :b], [:opt, :a]])).to be false
end
end
end
describe ".assert_parameters" do
context "Given a lambda / Proc / Method and parameters" do
it "Will raise ArgumentError if lambda doesn't have given parameter" do
method = Example.instance_method(:method)
expect{ ReflectionUtils.assert_parameters(method, [[:req, :something_else]]) }.to raise_error ArgumentError
end
it "Will return true if lambda has given parameters" do
expect(ReflectionUtils.assert_parameters(lambda { |a, b| }, [[:req, :a], [:req, :b]])).to be true
end
it "Will raise ArgumentError if Proc doesn't have parameters in given order" do
expect{ ReflectionUtils.assert_parameters(Proc.new { |a, b| }, [[:opt, :b], [:opt, :a]]) }.to raise_error ArgumentError
end
end
end
describe ".non_default_methods" do
context "Given a class" do
context "With no class methods defined" do
it "Will return empty array" do
class Empty;end
expect(ReflectionUtils.non_default_methods(Empty)).to be_empty
end
end
context "With (non overwritten) class methods defined" do
it "Will return array of class method name symbols" do
expect(ReflectionUtils.non_default_methods(Example)).to include(:class_method_1, :class_method_2)
end
end
end
context "Given an instance of a class" do
context "With no instance methods defined" do
it "Will return empty array" do
class Empty;end
expect(ReflectionUtils.non_default_methods(Empty.new)).to be_empty
end
end
context "With (non overwritten) instance methods defined" do
it "Will return array of instance method name symbols" do
methods = ReflectionUtils.non_default_methods(Example.new)
expect(methods).to include(:instance_method_1, :instance_method_2)
expect(methods).not_to include(:initialize, :method)
end
end
end
end
end