casting 0.2.0
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.
- data/test/casting_test.rb +113 -0
- data/test/test_helper.rb +9 -0
- metadata +60 -0
@@ -0,0 +1,113 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
require_relative '../lib/casting.rb'
|
3
|
+
|
4
|
+
class TestPerson
|
5
|
+
def name
|
6
|
+
'name from TestPerson'
|
7
|
+
end
|
8
|
+
|
9
|
+
module Greeter
|
10
|
+
def greet
|
11
|
+
'hello'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Verbose
|
16
|
+
def verbose(arg1, arg2)
|
17
|
+
%w{arg1 arg2}.join(',')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class SubTestPerson < TestPerson
|
23
|
+
def sub_method
|
24
|
+
'sub'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Unrelated
|
29
|
+
end
|
30
|
+
|
31
|
+
describe Casting::Delegation do
|
32
|
+
it 'initializes with method name and object' do
|
33
|
+
assert Casting::Delegation.new('some_method', Object.new)
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'raises an error when calling without an attendant object' do
|
37
|
+
delegation = Casting::Delegation.new('some_method', Object.new)
|
38
|
+
assert_raises(Casting::MissingAttendant){
|
39
|
+
delegation.call
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'raises an error when setting an invalid attendant type' do
|
44
|
+
delegation = Casting::Delegation.new('some_method', TestPerson.new)
|
45
|
+
assert_raises(TypeError, 'poo'){
|
46
|
+
delegation.to(Unrelated.new)
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'sets an attendant to an object of an ancestor class of the object class' do
|
51
|
+
attendant = TestPerson.new
|
52
|
+
client = SubTestPerson.new
|
53
|
+
|
54
|
+
delegation = Casting::Delegation.new('name', client)
|
55
|
+
assert delegation.to(attendant)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'calls a method defined on another object of the same type' do
|
59
|
+
client = TestPerson.new
|
60
|
+
attendant = TestPerson.new
|
61
|
+
attendant.extend(TestPerson::Greeter)
|
62
|
+
delegation = Casting::Delegation.new('greet', client).to(attendant)
|
63
|
+
assert_equal 'hello', delegation.call
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'passes arguments to a delegated method' do
|
67
|
+
client = TestPerson.new
|
68
|
+
attendant = TestPerson.new
|
69
|
+
attendant.extend(TestPerson::Verbose)
|
70
|
+
delegation = Casting::Delegation.new('verbose', client).to(attendant).with('arg1','arg2')
|
71
|
+
assert_equal 'arg1,arg2', delegation.call
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'delegates when given a module' do
|
75
|
+
client = TestPerson.new
|
76
|
+
delegation = Casting::Delegation.new('greet', client).to(TestPerson::Greeter)
|
77
|
+
assert_equal 'hello', delegation.call
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe Casting::Client do
|
82
|
+
it 'adds a delegate method to call a method on an attendant' do
|
83
|
+
client = TestPerson.new
|
84
|
+
client.extend(Casting::Client)
|
85
|
+
attendant = TestPerson.new
|
86
|
+
attendant.extend(TestPerson::Greeter)
|
87
|
+
|
88
|
+
assert_equal attendant.greet, client.delegate('greet', attendant)
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'passes additional parameters to the attendant' do
|
92
|
+
client = TestPerson.new
|
93
|
+
client.extend(Casting::Client)
|
94
|
+
attendant = TestPerson.new
|
95
|
+
attendant.extend(TestPerson::Verbose)
|
96
|
+
|
97
|
+
attendant_output = attendant.verbose('hello', 'goodbye')
|
98
|
+
client_output = client.delegate('verbose', attendant, 'hello', 'goodbye')
|
99
|
+
|
100
|
+
assert_equal attendant_output, client_output
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'passes the object as the client for delegation' do
|
104
|
+
client = Object.new
|
105
|
+
client.extend(Casting::Client)
|
106
|
+
|
107
|
+
delegation = client.delegation('id')
|
108
|
+
|
109
|
+
assert_equal client, delegation.client
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
CoverMe.complete!
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: casting
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jim Gay
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: cover_me
|
16
|
+
requirement: &70222871943440 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70222871943440
|
25
|
+
description: Proper method delegation.
|
26
|
+
email:
|
27
|
+
- jim@saturnflyer.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- test/test_helper.rb
|
33
|
+
- test/casting_test.rb
|
34
|
+
homepage: ''
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.11
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Proper method delegation.
|
58
|
+
test_files:
|
59
|
+
- test/test_helper.rb
|
60
|
+
- test/casting_test.rb
|