delegation 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/test/delegation_test.rb +69 -0
- data/test/test_helper.rb +2 -0
- metadata +49 -0
@@ -0,0 +1,69 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
require_relative '../lib/delegation.rb'
|
3
|
+
|
4
|
+
class TestPerson
|
5
|
+
module Greeter
|
6
|
+
def greet
|
7
|
+
'hello'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Verbose
|
12
|
+
def verbose(arg1, arg2)
|
13
|
+
%w{arg1 arg2}.join(',')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Unrelated
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Delegation do
|
22
|
+
it 'initializes with method name and object' do
|
23
|
+
assert Delegation.new('some_method', Object.new)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'raises an error when calling without an attendant object' do
|
27
|
+
delegation = Delegation.new('some_method', Object.new)
|
28
|
+
assert_raises(Delegation::MissingAttendant){
|
29
|
+
delegation.call
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'raises an error when setting an invalid attendant type' do
|
34
|
+
delegation = Delegation.new('some_method', TestPerson.new)
|
35
|
+
assert_raises(ArgumentError){
|
36
|
+
delegation.to(Unrelated.new)
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'calls a method defined on another object of the same type' do
|
41
|
+
client = TestPerson.new
|
42
|
+
attendant = TestPerson.new
|
43
|
+
attendant.extend(TestPerson::Greeter)
|
44
|
+
delegation = Delegation.new('greet', client).to(attendant)
|
45
|
+
assert_equal 'hello', delegation.call
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'passes arguments to a delegated method' do
|
49
|
+
client = TestPerson.new
|
50
|
+
attendant = TestPerson.new
|
51
|
+
attendant.extend(TestPerson::Verbose)
|
52
|
+
delegation = Delegation.new('verbose', client).to(attendant).with('arg1','arg2')
|
53
|
+
assert_equal 'arg1,arg2', delegation.call
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'delegates when given a module' do
|
57
|
+
client = TestPerson.new
|
58
|
+
delegation = Delegation.new('greet', client).to(TestPerson::Greeter)
|
59
|
+
assert_equal 'hello', delegation.call
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe Delegation::Client do
|
64
|
+
it 'adds a delegate method to return a Delegation' do
|
65
|
+
client = Object.new
|
66
|
+
client.extend(Delegation::Client)
|
67
|
+
assert_instance_of Delegation, client.delegate('id')
|
68
|
+
end
|
69
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delegation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jim Gay
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Proper method delegation.
|
15
|
+
email:
|
16
|
+
- jim@saturnflyer.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- test/test_helper.rb
|
22
|
+
- test/delegation_test.rb
|
23
|
+
homepage: ''
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.11
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Proper method delegation.
|
47
|
+
test_files:
|
48
|
+
- test/test_helper.rb
|
49
|
+
- test/delegation_test.rb
|