-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsuper_test.rb
87 lines (68 loc) · 2.14 KB
/
super_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
82
83
84
85
86
87
require "test_helper"
module AnyWay
def which_way
"any way"
end
def way_with_args(one, two, &block)
[one, two, block&.call].compact.inspect
end
def way_with_keyword_args(one:, two:, &block)
[one, two, block&.call].compact.inspect
end
end
module ThisWay
include Casting::SuperDelegate
def which_way
"this way or #{super_delegate}"
end
def way_with_args(one, two, &block)
[one, two, block&.call].compact.inspect
end
def way_with_keyword_args(one:, two:, &block)
[one, two, block&.call].compact.inspect
end
def no_super
super_delegate
end
end
module ThatWay
include Casting::SuperDelegate
def which_way
"#{super_delegate(ThatWay)} and that way!"
end
def way_with_args(one, two, &block)
super_delegate(one, two, block&.call).compact
end
def way_with_keyword_args(one:, two:, &block)
[one, two, block&.call].compact.inspect
end
end
describe Casting, "modules using delegate_super" do
it "call the method from the next delegate with the same arguments" do
client = TestPerson.new.extend(Casting::Client)
client.delegate_missing_methods
client.cast_as(AnyWay, ThatWay, ThisWay)
assert_equal "this way or any way and that way!", client.which_way
end
it "passes arguments" do
client = TestPerson.new.extend(Casting::Client)
client.delegate_missing_methods
client.cast_as(ThatWay, ThisWay)
assert_equal %(["first", "second", "block"]), client.way_with_args("first", "second") { "block" }
end
it "passes keyword arguments" do
client = TestPerson.new.extend(Casting::Client)
client.delegate_missing_methods
client.cast_as(ThatWay, ThisWay)
assert_equal %(["first", "second", "block"]), client.way_with_keyword_args(one: "first", two: "second") { "block" }
end
it "raises an error when method is not defined" do
client = TestPerson.new.extend(Casting::Client)
client.delegate_missing_methods
client.cast_as(ThisWay)
err = expect {
client.no_super
}.must_raise(NoMethodError)
expect(err.message).must_match(/super_delegate: no delegate method `no_super' for \#<TestPerson:\dx[a-z0-9]*> from ThisWay/)
end
end