casting 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +41 -0
- data/Rakefile +4 -4
- data/lib/casting/client.rb +12 -13
- data/lib/casting/context.rb +9 -15
- data/lib/casting/delegation.rb +33 -35
- data/lib/casting/enum.rb +13 -0
- data/lib/casting/method_consolidator.rb +5 -5
- data/lib/casting/missing_method_client.rb +16 -17
- data/lib/casting/missing_method_client_class.rb +3 -4
- data/lib/casting/null.rb +8 -2
- data/lib/casting/super_delegate.rb +11 -12
- data/lib/casting/version.rb +1 -1
- data/lib/casting.rb +6 -8
- metadata +5 -26
- data/test/casting_test.rb +0 -89
- data/test/class_refinement_test.rb +0 -119
- data/test/client_test.rb +0 -81
- data/test/context_test.rb +0 -80
- data/test/delegation_test.rb +0 -198
- data/test/method_consolidator_test.rb +0 -81
- data/test/missing_method_client_test.rb +0 -203
- data/test/module_cleanup_test.rb +0 -43
- data/test/null_module_test.rb +0 -43
- data/test/super_test.rb +0 -80
- data/test/test_helper.rb +0 -97
data/test/casting_test.rb
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
describe Casting, '.delegating' do
|
4
|
-
it 'delegates missing methods to object delegates' do
|
5
|
-
client = test_person
|
6
|
-
client.extend(Casting::Client)
|
7
|
-
client.delegate_missing_methods
|
8
|
-
|
9
|
-
attendant = test_person
|
10
|
-
attendant.extend(TestPerson::Greeter)
|
11
|
-
|
12
|
-
assert_raises(NoMethodError){
|
13
|
-
client.greet
|
14
|
-
}
|
15
|
-
Casting.delegating(client => attendant) do
|
16
|
-
assert_equal 'hello', client.greet
|
17
|
-
end
|
18
|
-
assert_raises(NoMethodError){
|
19
|
-
client.greet
|
20
|
-
}
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'delegates missing methods for the objects inside the block' do
|
24
|
-
client = BlockTestPerson.new('Jim')
|
25
|
-
verbose_client = BlockTestPerson.new('Amy')
|
26
|
-
|
27
|
-
assert_raises(NoMethodError){
|
28
|
-
client.greet
|
29
|
-
}
|
30
|
-
Casting.delegating(client => TestPerson::Greeter, verbose_client => TestPerson::Verbose) do
|
31
|
-
assert_equal 'hello', client.greet
|
32
|
-
assert_equal 'this,that', verbose_client.verbose('this','that')
|
33
|
-
end
|
34
|
-
assert_raises(NoMethodError){
|
35
|
-
client.greet
|
36
|
-
}
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'delegates missing methods on altered objects inside the block' do
|
40
|
-
client = test_person.extend(Casting::Client)
|
41
|
-
client.delegate_missing_methods
|
42
|
-
|
43
|
-
assert_raises(NoMethodError){
|
44
|
-
client.greet
|
45
|
-
}
|
46
|
-
Casting.delegating(client => TestPerson::Greeter) do
|
47
|
-
assert_equal 'hello', client.greet
|
48
|
-
end
|
49
|
-
assert_raises(NoMethodError){
|
50
|
-
client.greet
|
51
|
-
}
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'responds to added methods inside the block' do
|
55
|
-
client = test_person.extend(Casting::Client)
|
56
|
-
client.delegate_missing_methods
|
57
|
-
|
58
|
-
assert !client.respond_to?(:greet)
|
59
|
-
|
60
|
-
Casting.delegating(client => TestPerson::Greeter) do
|
61
|
-
assert client.respond_to?(:greet)
|
62
|
-
end
|
63
|
-
|
64
|
-
assert !client.respond_to?(:greet)
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'raises an error if the given object is not an object that delegates missing methods' do
|
68
|
-
client = test_person.extend(Casting::Client)
|
69
|
-
|
70
|
-
assert_raises(Casting::InvalidClientError){
|
71
|
-
Casting.delegating(client => TestPerson::Greeter){ }
|
72
|
-
}
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'allows for nested delegating' do
|
76
|
-
client = test_person.extend(Casting::Client)
|
77
|
-
client.delegate_missing_methods
|
78
|
-
|
79
|
-
Casting.delegating(client => TestPerson::Greeter) do
|
80
|
-
assert client.respond_to?(:greet)
|
81
|
-
Casting.delegating(client => TestPerson::Verbose) do
|
82
|
-
assert client.respond_to?(:greet)
|
83
|
-
assert client.respond_to?(:verbose)
|
84
|
-
end
|
85
|
-
assert !client.respond_to?(:verbose)
|
86
|
-
end
|
87
|
-
assert !client.respond_to?(:greet)
|
88
|
-
end
|
89
|
-
end
|
@@ -1,119 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
ClassDelegatingPerson = Struct.new(:name)
|
4
|
-
ClassDelegatingPerson.send(:include, Casting::Client)
|
5
|
-
ClassDelegatingPerson.delegate_missing_methods :class
|
6
|
-
|
7
|
-
InstanceDelegatingPerson = Struct.new(:name)
|
8
|
-
InstanceDelegatingPerson.send(:include, Casting::Client)
|
9
|
-
InstanceDelegatingPerson.delegate_missing_methods :instance
|
10
|
-
|
11
|
-
ClassThenInstanceDelegatingPerson = Struct.new(:name)
|
12
|
-
ClassThenInstanceDelegatingPerson.send(:include, Casting::Client)
|
13
|
-
ClassThenInstanceDelegatingPerson.delegate_missing_methods :class, :instance
|
14
|
-
|
15
|
-
InstanceThenClassDelegatingPerson = Struct.new(:name)
|
16
|
-
InstanceThenClassDelegatingPerson.send(:include, Casting::Client)
|
17
|
-
InstanceThenClassDelegatingPerson.delegate_missing_methods :instance, :class
|
18
|
-
|
19
|
-
module ClassGreeter
|
20
|
-
def greet
|
21
|
-
'hello from the class delegate'
|
22
|
-
end
|
23
|
-
|
24
|
-
def class_greeting
|
25
|
-
'Hi!'
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
module InstanceGreeter
|
30
|
-
def greet
|
31
|
-
'hello from the instance delegate'
|
32
|
-
end
|
33
|
-
|
34
|
-
def instance_greeting
|
35
|
-
'hi!'
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
describe Casting, '.delegating' do
|
40
|
-
it 'delegates methods for all instances to a class delegate inside a block' do
|
41
|
-
jim = ClassDelegatingPerson.new('Jim')
|
42
|
-
amy = ClassDelegatingPerson.new('Amy')
|
43
|
-
|
44
|
-
assert_raises(NoMethodError){
|
45
|
-
jim.greet
|
46
|
-
}
|
47
|
-
Casting.delegating(ClassDelegatingPerson => TestPerson::Greeter) do
|
48
|
-
assert_equal 'hello', jim.greet
|
49
|
-
assert_equal 'hello', amy.greet
|
50
|
-
end
|
51
|
-
assert_raises(NoMethodError){
|
52
|
-
jim.greet
|
53
|
-
}
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'delegates methods for given instances to an instance delegate inside a block' do
|
57
|
-
jim = InstanceDelegatingPerson.new('Jim')
|
58
|
-
amy = InstanceDelegatingPerson.new('Amy')
|
59
|
-
|
60
|
-
assert_raises(NoMethodError){
|
61
|
-
jim.greet
|
62
|
-
}
|
63
|
-
Casting.delegating(jim => TestPerson::Greeter) do
|
64
|
-
assert_equal 'hello', jim.greet
|
65
|
-
assert_raises(NoMethodError){ amy.greet }
|
66
|
-
end
|
67
|
-
assert_raises(NoMethodError){
|
68
|
-
jim.greet
|
69
|
-
}
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'delegates first to class delegates, then to instance delegates inside a block' do
|
73
|
-
jim = ClassThenInstanceDelegatingPerson.new('Jim')
|
74
|
-
amy = ClassThenInstanceDelegatingPerson.new('Amy')
|
75
|
-
|
76
|
-
assert_raises(NoMethodError){
|
77
|
-
jim.greet
|
78
|
-
}
|
79
|
-
Casting.delegating(ClassThenInstanceDelegatingPerson => ClassGreeter, jim => InstanceGreeter) do
|
80
|
-
assert_equal 'hello from the class delegate', jim.greet
|
81
|
-
assert_equal 'hi!', jim.instance_greeting
|
82
|
-
assert_equal 'hello from the class delegate', amy.greet
|
83
|
-
assert(NoMethodError){ amy.instance_greeting }
|
84
|
-
end
|
85
|
-
assert_raises(NoMethodError){
|
86
|
-
jim.greet
|
87
|
-
}
|
88
|
-
end
|
89
|
-
|
90
|
-
it 'delegates first to instance delegates, then to class delegates inside a block' do
|
91
|
-
jim = InstanceThenClassDelegatingPerson.new('Jim')
|
92
|
-
amy = InstanceThenClassDelegatingPerson.new('Amy')
|
93
|
-
|
94
|
-
assert_raises(NoMethodError){
|
95
|
-
jim.greet
|
96
|
-
}
|
97
|
-
Casting.delegating(InstanceThenClassDelegatingPerson => ClassGreeter, jim => InstanceGreeter) do
|
98
|
-
assert_equal 'hello from the instance delegate', jim.greet
|
99
|
-
assert_equal 'hi!', jim.instance_greeting
|
100
|
-
assert_equal 'hello from the class delegate', amy.greet
|
101
|
-
assert(NoMethodError){ amy.instance_greeting }
|
102
|
-
end
|
103
|
-
assert_raises(NoMethodError){
|
104
|
-
jim.greet
|
105
|
-
}
|
106
|
-
end
|
107
|
-
|
108
|
-
it 'sets instances to respond_to? class delegate methods' do
|
109
|
-
jim = ClassDelegatingPerson.new('Jim')
|
110
|
-
|
111
|
-
refute jim.respond_to?(:greet)
|
112
|
-
|
113
|
-
Casting.delegating(ClassDelegatingPerson => ClassGreeter) do
|
114
|
-
assert jim.respond_to?(:greet)
|
115
|
-
end
|
116
|
-
|
117
|
-
refute jim.respond_to?(:greet)
|
118
|
-
end
|
119
|
-
end
|
data/test/client_test.rb
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
describe Casting::Client do
|
4
|
-
it 'will not override an existing `delegate` method' do
|
5
|
-
client = TestPerson.new
|
6
|
-
def client.delegate
|
7
|
-
'existing delegate method'
|
8
|
-
end
|
9
|
-
client.extend(Casting::Client)
|
10
|
-
|
11
|
-
attendant = TestPerson::Greeter
|
12
|
-
|
13
|
-
assert_equal 'existing delegate method', client.delegate
|
14
|
-
|
15
|
-
assert_equal 'hello', client.cast('greet', attendant)
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'adds a delegate method to call a method on an attendant' do
|
19
|
-
client = TestPerson.new
|
20
|
-
client.extend(Casting::Client)
|
21
|
-
attendant = TestPerson::Greeter
|
22
|
-
|
23
|
-
assert_equal 'hello', client.delegate('greet', attendant)
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'passes additional parameters to the attendant' do
|
27
|
-
client = TestPerson.new
|
28
|
-
client.extend(Casting::Client)
|
29
|
-
attendant = TestPerson::Verbose
|
30
|
-
|
31
|
-
assert_equal 'hello,goodbye', client.delegate('verbose', attendant, 'hello', 'goodbye')
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'executes delegated methods with a block' do
|
35
|
-
client = TestPerson.new
|
36
|
-
client.extend(Casting::Client)
|
37
|
-
mod = Module.new
|
38
|
-
mod.module_eval do
|
39
|
-
def blocky(arg, &block)
|
40
|
-
block.call(arg, self)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
output = client.delegate('blocky', mod, 'argument') do |arg, me|
|
45
|
-
%{#{arg} from #{me.name}}
|
46
|
-
end
|
47
|
-
|
48
|
-
assert_equal 'argument from name from TestPerson', output
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'passes the object as the client for delegation' do
|
52
|
-
client = Object.new
|
53
|
-
client.extend(Casting::Client)
|
54
|
-
|
55
|
-
delegation = client.delegation('id')
|
56
|
-
|
57
|
-
assert_equal client, delegation.client
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'refuses to delegate to itself' do
|
61
|
-
client = TestPerson.new
|
62
|
-
client.extend(Casting::Client)
|
63
|
-
|
64
|
-
assert_raises(Casting::InvalidAttendant){
|
65
|
-
client.delegate('to_s', client)
|
66
|
-
}
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'does not delegate singleton methods' do
|
70
|
-
client = test_person.extend(Casting::Client)
|
71
|
-
client.delegate_missing_methods
|
72
|
-
attendant = test_person
|
73
|
-
|
74
|
-
def attendant.hello
|
75
|
-
'hello'
|
76
|
-
end
|
77
|
-
assert_raises(TypeError){
|
78
|
-
client.delegate('hello', attendant)
|
79
|
-
}
|
80
|
-
end
|
81
|
-
end
|
data/test/context_test.rb
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TestContext
|
4
|
-
using Casting::Context
|
5
|
-
extend Casting::Context
|
6
|
-
|
7
|
-
initialize :admin, :user
|
8
|
-
|
9
|
-
def approve
|
10
|
-
tell :admin, :say, 'I approve'
|
11
|
-
end
|
12
|
-
|
13
|
-
def approve_with_keyword
|
14
|
-
tell :admin, :keyword_say, what: 'I approve'
|
15
|
-
end
|
16
|
-
|
17
|
-
def user_approve
|
18
|
-
tell :user, :approve
|
19
|
-
end
|
20
|
-
|
21
|
-
module Admin
|
22
|
-
def say(what)
|
23
|
-
what
|
24
|
-
end
|
25
|
-
|
26
|
-
def keyword_say(what:)
|
27
|
-
what
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
module User
|
32
|
-
def approve
|
33
|
-
'Yay!'
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
class MissingModuleContext
|
39
|
-
using Casting::Context
|
40
|
-
extend Casting::Context
|
41
|
-
|
42
|
-
initialize :admin, :user
|
43
|
-
|
44
|
-
def run
|
45
|
-
tell :admin, :go
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe Casting::Context do
|
50
|
-
it 'applies module methods to Casting::Client objects' do
|
51
|
-
admin = casting_person
|
52
|
-
user = casting_person
|
53
|
-
|
54
|
-
context = TestContext.new admin: admin, user: user
|
55
|
-
|
56
|
-
expect(context.approve).must_equal ('I approve')
|
57
|
-
expect(context.approve_with_keyword).must_equal ('I approve')
|
58
|
-
expect(context.user_approve).must_equal ('Yay!')
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'applies module methods to any object' do
|
62
|
-
admin = Object.new
|
63
|
-
user = 1
|
64
|
-
|
65
|
-
context = TestContext.new admin: admin, user: user
|
66
|
-
|
67
|
-
expect(context.approve).must_equal ('I approve')
|
68
|
-
expect(context.user_approve).must_equal ('Yay!')
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'handles missing modules and raises missing method error' do
|
72
|
-
admin = TestPerson.new
|
73
|
-
user = TestPerson.new
|
74
|
-
|
75
|
-
context = MissingModuleContext.new admin: admin, user: user
|
76
|
-
|
77
|
-
err = expect{ context.run }.must_raise(NoMethodError)
|
78
|
-
expect(err.message).must_match(/unknown method 'go'/)
|
79
|
-
end
|
80
|
-
end
|
data/test/delegation_test.rb
DELETED
@@ -1,198 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
describe Casting::Delegation do
|
4
|
-
|
5
|
-
it 'initializes with method name and object' do
|
6
|
-
assert Casting::Delegation.prepare('some_method', Object.new)
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'raises an error when calling without an attendant object' do
|
10
|
-
delegation = Casting::Delegation.prepare('some_method', Object.new)
|
11
|
-
begin
|
12
|
-
delegation.call
|
13
|
-
rescue StandardError => e
|
14
|
-
end
|
15
|
-
assert_kind_of Casting::MissingAttendant, e
|
16
|
-
assert_equal "You must set your attendant object using `to'.", e.message
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'raises an error when setting an invalid attendant type' do
|
20
|
-
delegation = Casting::Delegation.prepare('some_method', TestPerson.new)
|
21
|
-
assert_raises(Casting::InvalidAttendant){
|
22
|
-
delegation.to(Unrelated.new)
|
23
|
-
}
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'raises an error when setting a class as the attendant' do
|
27
|
-
delegation = Casting::Delegation.prepare('some_method', TestPerson)
|
28
|
-
assert_raises(Casting::InvalidAttendant){
|
29
|
-
delegation.to(Unrelated.new)
|
30
|
-
}
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'sets an attendant to an object of an ancestor class of the object class' do
|
34
|
-
attendant = test_person
|
35
|
-
client = SubTestPerson.new
|
36
|
-
|
37
|
-
delegation = Casting::Delegation.prepare('name', client)
|
38
|
-
assert delegation.to(attendant)
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'delegates when given a module' do
|
42
|
-
client = test_person
|
43
|
-
delegation = Casting::Delegation.prepare('greet', client).to(TestPerson::Greeter)
|
44
|
-
assert_equal 'hello', delegation.call
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'does not delegate when given a class' do
|
48
|
-
client = test_person
|
49
|
-
err = expect{
|
50
|
-
Casting::Delegation.prepare('class_defined', client).to(Unrelated)
|
51
|
-
}.must_raise(TypeError)
|
52
|
-
expect(err.message).must_match(/ argument must be a module or an object with/)
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'finds the module defining a method and uses it to delegate' do
|
56
|
-
client = test_person
|
57
|
-
attendant = Unrelated.new
|
58
|
-
delegation = Casting::Delegation.prepare('unrelated', client).to(attendant)
|
59
|
-
assert_equal attendant.unrelated, delegation.call
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'does not delegate to methods defined in classes' do
|
63
|
-
client = test_person
|
64
|
-
attendant = Unrelated.new
|
65
|
-
assert_raises(TypeError){
|
66
|
-
Casting::Delegation.prepare('class_defined', client).to(attendant)
|
67
|
-
}
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'assigns arguments to the delegated method using with' do
|
71
|
-
client = test_person
|
72
|
-
attendant = TestPerson::Verbose
|
73
|
-
|
74
|
-
delegation = Casting::Delegation.prepare('verbose', client).to(attendant)
|
75
|
-
|
76
|
-
assert_equal 'hello,goodbye', delegation.with('hello', 'goodbye').call
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'assigns keyword arguments to the delegated method using with' do
|
80
|
-
client = test_person
|
81
|
-
attendant = TestPerson::Verbose
|
82
|
-
|
83
|
-
delegation = Casting::Delegation.prepare('verbose_keywords', client).to(attendant)
|
84
|
-
|
85
|
-
assert_equal 'hello,goodbye', delegation.with(key: 'hello', word: 'goodbye').call
|
86
|
-
end
|
87
|
-
|
88
|
-
it 'assigns regular and keyword arguments to the delegated method using with' do
|
89
|
-
client = test_person
|
90
|
-
attendant = TestPerson::Verbose
|
91
|
-
|
92
|
-
delegation = Casting::Delegation.prepare('verbose_multi_args', client).to(attendant)
|
93
|
-
|
94
|
-
assert_equal('hello,goodbye,keys,words,block!', delegation.with('hello', 'goodbye', key: 'keys', word: 'words') do
|
95
|
-
"block!"
|
96
|
-
end.call)
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'handles flexible arguments to the delegated method using with' do
|
100
|
-
client = test_person
|
101
|
-
attendant = TestPerson::Verbose
|
102
|
-
|
103
|
-
delegation = Casting::Delegation.prepare('verbose_flex', client).to(attendant)
|
104
|
-
|
105
|
-
assert_equal('hello,key:keys,word:words,block!', delegation.with('hello', key: 'keys', word: 'words') do
|
106
|
-
"block!"
|
107
|
-
end.call)
|
108
|
-
end
|
109
|
-
|
110
|
-
it 'prefers `call` arguments over `with`' do
|
111
|
-
client = test_person
|
112
|
-
attendant = TestPerson::Verbose
|
113
|
-
|
114
|
-
delegation = Casting::Delegation.prepare('verbose', client).to(attendant)
|
115
|
-
|
116
|
-
assert_equal 'call,args', delegation.with('hello', 'goodbye').call('call','args')
|
117
|
-
end
|
118
|
-
|
119
|
-
it 'prefers "call" keyword arguments over "with"' do
|
120
|
-
client = test_person
|
121
|
-
attendant = TestPerson::Verbose
|
122
|
-
|
123
|
-
delegation = Casting::Delegation.prepare('verbose_keywords', client).to(attendant)
|
124
|
-
|
125
|
-
assert_equal 'call,args', delegation.with(key: 'hello', word: 'goodbye').call(key: 'call', word: 'args')
|
126
|
-
end
|
127
|
-
|
128
|
-
it 'prefers "call" regular and keyword arguments over "with"' do
|
129
|
-
client = test_person
|
130
|
-
attendant = TestPerson::Verbose
|
131
|
-
|
132
|
-
delegation = Casting::Delegation.prepare('verbose_multi_args', client).to(attendant)
|
133
|
-
|
134
|
-
assert_equal 'hello,goodbye,call,args', delegation.with('this', 'that', key: 'something', word: 'else').call('hello', 'goodbye', key: 'call', word: 'args')
|
135
|
-
end
|
136
|
-
|
137
|
-
it 'prefers "call" block arguments over "with"' do
|
138
|
-
client = test_person
|
139
|
-
attendant = TestPerson::Verbose
|
140
|
-
|
141
|
-
delegation = Casting::Delegation.prepare('verbose_multi_args', client).to(attendant)
|
142
|
-
|
143
|
-
prepared = delegation.with('this', 'that', key: 'something', word: 'else') { "prepared block!" }
|
144
|
-
|
145
|
-
assert_equal('this,that,something,else,call block!', prepared.call{ "call block!" })
|
146
|
-
end
|
147
|
-
|
148
|
-
it 'prefers "call" keyword arguments and block over "with"' do
|
149
|
-
client = test_person
|
150
|
-
attendant = TestPerson::Verbose
|
151
|
-
|
152
|
-
delegation = Casting::Delegation.prepare('verbose_flex', client).to(attendant)
|
153
|
-
|
154
|
-
prepared = delegation.with(key: 'something', word: 'else') { "prepared block!" }
|
155
|
-
|
156
|
-
assert_equal('key:call_key,word:call_word,call block!', prepared.call(key: 'call_key', word: 'call_word'){ "call block!" })
|
157
|
-
end
|
158
|
-
|
159
|
-
it 'prefers "call" and block over "with"' do
|
160
|
-
client = test_person
|
161
|
-
attendant = TestPerson::Verbose
|
162
|
-
|
163
|
-
delegation = Casting::Delegation.prepare('verbose_flex', client).to(attendant)
|
164
|
-
|
165
|
-
prepared = delegation.with { "prepared block!" }
|
166
|
-
|
167
|
-
assert_equal('call block!', prepared.call { "call block!" })
|
168
|
-
end
|
169
|
-
|
170
|
-
it 'calls a method defined on another object of the same type' do
|
171
|
-
client = test_person
|
172
|
-
attendant = test_person
|
173
|
-
attendant.extend(TestPerson::Greeter)
|
174
|
-
delegation = Casting::Delegation.prepare('greet', client).to(attendant)
|
175
|
-
assert_equal 'hello', delegation.call
|
176
|
-
end
|
177
|
-
|
178
|
-
it 'passes arguments to a delegated method' do
|
179
|
-
client = test_person
|
180
|
-
attendant = test_person
|
181
|
-
attendant.extend(TestPerson::Verbose)
|
182
|
-
delegation = Casting::Delegation.prepare('verbose', client).to(attendant).with('arg1','arg2')
|
183
|
-
assert_equal 'arg1,arg2', delegation.call
|
184
|
-
end
|
185
|
-
|
186
|
-
it 'delegates when given a module' do
|
187
|
-
client = test_person
|
188
|
-
delegation = Casting::Delegation.prepare('greet', client).to(TestPerson::Greeter)
|
189
|
-
assert_equal 'hello', delegation.call
|
190
|
-
end
|
191
|
-
|
192
|
-
it 'does not delegate when given a class' do
|
193
|
-
client = test_person
|
194
|
-
assert_raises(TypeError){
|
195
|
-
Casting::Delegation.prepare('class_defined', client).to(Unrelated)
|
196
|
-
}
|
197
|
-
end
|
198
|
-
end
|
@@ -1,81 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
describe Casting::MethodConsolidator, '#methods' do
|
4
|
-
let(:client){
|
5
|
-
object = test_person.extend(Casting::Client, Casting::MissingMethodClient, Casting::MethodConsolidator)
|
6
|
-
object.cast_as(TestPerson::Greeter)
|
7
|
-
object
|
8
|
-
}
|
9
|
-
|
10
|
-
it "returns all instance methods including private from the object and it's delegates" do
|
11
|
-
assert_includes(client.methods(true), :psst)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "returns all public instance methods from the object and it's delegates" do
|
15
|
-
refute_includes(client.methods(false), :psst)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "returns all protected instance methods from the object and it's delegates" do
|
19
|
-
assert_includes(client.methods(true), :hey)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
describe Casting::MethodConsolidator, '#public_methods' do
|
24
|
-
let(:client){
|
25
|
-
object = test_person.extend(Casting::Client, Casting::MissingMethodClient, Casting::MethodConsolidator)
|
26
|
-
object.cast_as(TestPerson::Greeter)
|
27
|
-
object
|
28
|
-
}
|
29
|
-
|
30
|
-
it "returns all public_methods and those from it's delegates" do
|
31
|
-
assert_includes(client.public_methods, :greet)
|
32
|
-
end
|
33
|
-
|
34
|
-
it "excludes all protected_methods and those from it's delegates" do
|
35
|
-
refute_includes(client.public_methods, :hey)
|
36
|
-
end
|
37
|
-
|
38
|
-
it "excludes all private_methods from the object and it's delegates" do
|
39
|
-
refute_includes(client.public_methods, :psst)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe Casting::MethodConsolidator, '#protected_methods' do
|
44
|
-
let(:client){
|
45
|
-
object = test_person.extend(Casting::Client, Casting::MissingMethodClient, Casting::MethodConsolidator)
|
46
|
-
object.cast_as(TestPerson::Greeter)
|
47
|
-
object
|
48
|
-
}
|
49
|
-
|
50
|
-
it "excludes all public_methods and those from it's delegates" do
|
51
|
-
refute_includes(client.protected_methods, :greet)
|
52
|
-
end
|
53
|
-
|
54
|
-
it "returns all protected_methods and those from it's delegates" do
|
55
|
-
assert_includes(client.protected_methods, :hey)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "excludes all private_methods from the object and it's delegates" do
|
59
|
-
refute_includes(client.protected_methods, :psst)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe Casting::MethodConsolidator, '#private_methods' do
|
64
|
-
let(:client){
|
65
|
-
object = test_person.extend(Casting::Client, Casting::MissingMethodClient, Casting::MethodConsolidator)
|
66
|
-
object.cast_as(TestPerson::Greeter)
|
67
|
-
object
|
68
|
-
}
|
69
|
-
|
70
|
-
it "excludes all public_methods and those from it's delegates" do
|
71
|
-
refute_includes(client.private_methods, :greet)
|
72
|
-
end
|
73
|
-
|
74
|
-
it "excludes all protected_methods and those from it's delegates" do
|
75
|
-
refute_includes(client.private_methods, :hey)
|
76
|
-
end
|
77
|
-
|
78
|
-
it "excludes all private_methods from the object and it's delegates" do
|
79
|
-
assert_includes(client.private_methods, :psst)
|
80
|
-
end
|
81
|
-
end
|