rubysl-delegate 1.0.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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +25 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/delegate.rb +1 -0
- data/lib/rubysl/delegate.rb +2 -0
- data/lib/rubysl/delegate/delegate.rb +337 -0
- data/lib/rubysl/delegate/version.rb +5 -0
- data/rubysl-delegate.gemspec +22 -0
- data/spec/delegate_class/instance_method_spec.rb +53 -0
- data/spec/delegate_class/instance_methods_spec.rb +49 -0
- data/spec/delegate_class/private_instance_methods_spec.rb +41 -0
- data/spec/delegate_class/protected_instance_methods_spec.rb +55 -0
- data/spec/delegate_class/public_instance_methods_spec.rb +45 -0
- data/spec/delegator/case_compare_spec.rb +10 -0
- data/spec/delegator/compare_spec.rb +10 -0
- data/spec/delegator/complement_spec.rb +12 -0
- data/spec/delegator/eql_spec.rb +12 -0
- data/spec/delegator/equal_spec.rb +12 -0
- data/spec/delegator/equal_value_spec.rb +31 -0
- data/spec/delegator/frozen_spec.rb +48 -0
- data/spec/delegator/hash_spec.rb +12 -0
- data/spec/delegator/marshal_spec.rb +22 -0
- data/spec/delegator/method_spec.rb +76 -0
- data/spec/delegator/methods_spec.rb +65 -0
- data/spec/delegator/not_equal_spec.rb +25 -0
- data/spec/delegator/not_spec.rb +12 -0
- data/spec/delegator/private_methods_spec.rb +33 -0
- data/spec/delegator/protected_methods_spec.rb +31 -0
- data/spec/delegator/public_methods_spec.rb +29 -0
- data/spec/delegator/send_spec.rb +33 -0
- data/spec/delegator/taint_spec.rb +24 -0
- data/spec/delegator/tap_spec.rb +17 -0
- data/spec/delegator/trust_spec.rb +41 -0
- data/spec/delegator/untaint_spec.rb +25 -0
- data/spec/delegator/untrust_spec.rb +24 -0
- data/spec/fixtures/classes.rb +65 -0
- metadata +153 -0
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require './lib/rubysl/delegate/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "rubysl-delegate"
|
6
|
+
spec.version = RubySL::Delegator::VERSION
|
7
|
+
spec.authors = ["Brian Shirai"]
|
8
|
+
spec.email = ["brixen@gmail.com"]
|
9
|
+
spec.description = %q{Ruby standard library delegate.}
|
10
|
+
spec.summary = %q{Ruby standard library delegate.}
|
11
|
+
spec.homepage = "https://github.com/rubysl/rubysl-delegate"
|
12
|
+
spec.license = "BSD"
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
20
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
spec.add_development_dependency "mspec", "~> 1.5"
|
22
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "DelegateClass.instance_method" do
|
4
|
+
before :all do
|
5
|
+
@klass = DelegateSpecs::DelegateClass
|
6
|
+
@obj = @klass.new(DelegateSpecs::Simple.new)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns a method object for public instance methods of the delegated class" do
|
10
|
+
m = @klass.instance_method(:pub)
|
11
|
+
m.should be_an_instance_of(UnboundMethod)
|
12
|
+
m.bind(@obj).call.should == :foo
|
13
|
+
end
|
14
|
+
|
15
|
+
ruby_bug "#2496", "1.8" do
|
16
|
+
it "returns a method object for protected instance methods of the delegated class" do
|
17
|
+
m = @klass.instance_method(:prot)
|
18
|
+
m.should be_an_instance_of(UnboundMethod)
|
19
|
+
m.bind(@obj).call.should == :protected
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "raises a NameError for a private instance methods of the delegated class" do
|
24
|
+
lambda {
|
25
|
+
@klass.instance_method(:priv)
|
26
|
+
}.should raise_error(NameError)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns a method object for public instance methods of the DelegateClass class" do
|
30
|
+
m = @klass.instance_method(:extra)
|
31
|
+
m.should be_an_instance_of(UnboundMethod)
|
32
|
+
m.bind(@obj).call.should == :cheese
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns a method object for protected instance methods of the DelegateClass class" do
|
36
|
+
m = @klass.instance_method(:extra_protected)
|
37
|
+
m.should be_an_instance_of(UnboundMethod)
|
38
|
+
m.bind(@obj).call.should == :baz
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns a method object for private instance methods of the DelegateClass class" do
|
42
|
+
m = @klass.instance_method(:extra_private)
|
43
|
+
m.should be_an_instance_of(UnboundMethod)
|
44
|
+
m.bind(@obj).call.should == :bar
|
45
|
+
end
|
46
|
+
|
47
|
+
it "raises a NameError for an invalid method name" do
|
48
|
+
lambda {
|
49
|
+
@klass.instance_method(:invalid_and_silly_method_name)
|
50
|
+
}.should raise_error(NameError)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "DelegateClass.instance_methods" do
|
4
|
+
before :all do
|
5
|
+
@methods = DelegateSpecs::DelegateClass.instance_methods
|
6
|
+
end
|
7
|
+
|
8
|
+
ruby_version_is ""..."1.9" do
|
9
|
+
it "includes all public methods of the delegated class" do
|
10
|
+
@methods.should include "pub"
|
11
|
+
end
|
12
|
+
|
13
|
+
ruby_bug "#2496", "1.8" do
|
14
|
+
it "includes all protected methods of the delegated class" do
|
15
|
+
@methods.should include "prot"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "includes instance methods of the DelegateClass class" do
|
20
|
+
@methods.should include "extra"
|
21
|
+
@methods.should include "extra_protected"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "does not include private methods" do
|
25
|
+
@methods.should_not include "priv"
|
26
|
+
@methods.should_not include "extra_private"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
ruby_version_is "1.9" do
|
31
|
+
it "includes all public methods of the delegated class" do
|
32
|
+
@methods.should include :pub
|
33
|
+
end
|
34
|
+
|
35
|
+
it "includes all protected methods of the delegated class" do
|
36
|
+
@methods.should include :prot
|
37
|
+
end
|
38
|
+
|
39
|
+
it "includes instance methods of the DelegateClass class" do
|
40
|
+
@methods.should include :extra
|
41
|
+
@methods.should include :extra_protected
|
42
|
+
end
|
43
|
+
|
44
|
+
it "does not include private methods" do
|
45
|
+
@methods.should_not include :priv
|
46
|
+
@methods.should_not include :extra_private
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "DelegateClass.private_instance_methods" do
|
4
|
+
before :all do
|
5
|
+
@methods = DelegateSpecs::DelegateClass.private_instance_methods
|
6
|
+
end
|
7
|
+
|
8
|
+
ruby_version_is ""..."1.9" do
|
9
|
+
it "does not include any instance methods of the delegated class" do
|
10
|
+
@methods.should_not include "pub"
|
11
|
+
@methods.should_not include "prot"
|
12
|
+
@methods.should_not include "priv" # since these are not forwarded...
|
13
|
+
end
|
14
|
+
|
15
|
+
it "includes private instance methods of the DelegateClass class" do
|
16
|
+
@methods.should include "extra_private"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "does not include public or protected instance methods of the DelegateClass class" do
|
20
|
+
@methods.should_not include "extra"
|
21
|
+
@methods.should_not include "extra_protected"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
ruby_version_is "1.9" do
|
26
|
+
it "does not include any instance methods of the delegated class" do
|
27
|
+
@methods.should_not include :pub
|
28
|
+
@methods.should_not include :prot
|
29
|
+
@methods.should_not include :priv # since these are not forwarded...
|
30
|
+
end
|
31
|
+
|
32
|
+
it "includes private instance methods of the DelegateClass class" do
|
33
|
+
@methods.should include :extra_private
|
34
|
+
end
|
35
|
+
|
36
|
+
it "does not include public or protected instance methods of the DelegateClass class" do
|
37
|
+
@methods.should_not include :extra
|
38
|
+
@methods.should_not include :extra_protected
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "DelegateClass.protected_instance_methods" do
|
4
|
+
before :all do
|
5
|
+
@methods = DelegateSpecs::DelegateClass.protected_instance_methods
|
6
|
+
end
|
7
|
+
|
8
|
+
ruby_version_is ""..."1.9" do
|
9
|
+
it "does not include public methods of the delegated class" do
|
10
|
+
@methods.should_not include "pub"
|
11
|
+
end
|
12
|
+
|
13
|
+
ruby_bug "#2496", "1.8" do
|
14
|
+
it "includes the protected methods of the delegated class" do
|
15
|
+
@methods.should include "prot"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "includes protected instance methods of the DelegateClass class" do
|
20
|
+
@methods.should include "extra_protected"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "does not include public instance methods of the DelegateClass class" do
|
24
|
+
@methods.should_not include "extra"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "does not include private methods" do
|
28
|
+
@methods.should_not include "priv"
|
29
|
+
@methods.should_not include "extra_private"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
ruby_version_is "1.9" do
|
34
|
+
it "does not include public methods of the delegated class" do
|
35
|
+
@methods.should_not include :pub
|
36
|
+
end
|
37
|
+
|
38
|
+
it "includes the protected methods of the delegated class" do
|
39
|
+
@methods.should include :prot
|
40
|
+
end
|
41
|
+
|
42
|
+
it "includes protected instance methods of the DelegateClass class" do
|
43
|
+
@methods.should include :extra_protected
|
44
|
+
end
|
45
|
+
|
46
|
+
it "does not include public instance methods of the DelegateClass class" do
|
47
|
+
@methods.should_not include :extra
|
48
|
+
end
|
49
|
+
|
50
|
+
it "does not include private methods" do
|
51
|
+
@methods.should_not include :priv
|
52
|
+
@methods.should_not include :extra_private
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "DelegateClass.public_instance_methods" do
|
4
|
+
before :all do
|
5
|
+
@methods = DelegateSpecs::DelegateClass.public_instance_methods
|
6
|
+
end
|
7
|
+
|
8
|
+
ruby_version_is ""..."1.9" do
|
9
|
+
it "includes all public methods of the delegated class" do
|
10
|
+
@methods.should include "pub"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "does not include the protected methods of the delegated class" do
|
14
|
+
@methods.should_not include "prot"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "includes public instance methods of the DelegateClass class" do
|
18
|
+
@methods.should include "extra"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "does not include private methods" do
|
22
|
+
@methods.should_not include "priv"
|
23
|
+
@methods.should_not include "extra_private"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
ruby_version_is "1.9" do
|
28
|
+
it "includes all public methods of the delegated class" do
|
29
|
+
@methods.should include :pub
|
30
|
+
end
|
31
|
+
|
32
|
+
it "does not include the protected methods of the delegated class" do
|
33
|
+
@methods.should_not include :prot
|
34
|
+
end
|
35
|
+
|
36
|
+
it "includes public instance methods of the DelegateClass class" do
|
37
|
+
@methods.should include :extra
|
38
|
+
end
|
39
|
+
|
40
|
+
it "does not include private methods" do
|
41
|
+
@methods.should_not include :priv
|
42
|
+
@methods.should_not include :extra_private
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "Delegator#===" do
|
4
|
+
it "is delegated" do
|
5
|
+
base = mock('base')
|
6
|
+
delegator = DelegateSpecs::Delegator.new(base)
|
7
|
+
base.should_receive(:===).with(42).and_return(:foo)
|
8
|
+
(delegator === 42).should == :foo
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "Delegator#<=>" do
|
4
|
+
it "is delegated" do
|
5
|
+
base = mock('base')
|
6
|
+
delegator = DelegateSpecs::Delegator.new(base)
|
7
|
+
base.should_receive(:<=>).with(42).and_return(1)
|
8
|
+
(delegator <=> 42).should == 1
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "Delegator#~" do
|
4
|
+
ruby_bug "redmine:2224", "1.8" do
|
5
|
+
it "is delegated" do
|
6
|
+
base = mock('base')
|
7
|
+
delegator = DelegateSpecs::Delegator.new(base)
|
8
|
+
base.should_receive(:~).and_return(:foo)
|
9
|
+
(~delegator).should == :foo
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "Delegator#eql?" do
|
4
|
+
ruby_bug "redmine:2224", "1.8" do
|
5
|
+
it "is delegated" do
|
6
|
+
base = mock('base')
|
7
|
+
delegator = DelegateSpecs::Delegator.new(base)
|
8
|
+
base.should_receive(:eql?).with(42).and_return(:foo)
|
9
|
+
delegator.eql?(42).should == :foo
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "Delegator#equal?" do
|
4
|
+
it "returns true only when compared with the delegator" do
|
5
|
+
obj = mock('base')
|
6
|
+
delegator = DelegateSpecs::Delegator.new(obj)
|
7
|
+
obj.should_not_receive(:equal?)
|
8
|
+
delegator.equal?(obj).should be_false
|
9
|
+
delegator.equal?(nil).should be_false
|
10
|
+
delegator.equal?(delegator).should be_true
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "Delegator#==" do
|
4
|
+
before :all do
|
5
|
+
@base = mock('base')
|
6
|
+
@delegator = DelegateSpecs::Delegator.new(@base)
|
7
|
+
end
|
8
|
+
|
9
|
+
ruby_version_is ""..."1.9" do
|
10
|
+
it "is delegated when passed self" do
|
11
|
+
@base.should_receive(:==).and_return(false)
|
12
|
+
(@delegator == @delegator).should be_false
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
ruby_version_is "1.9" do
|
17
|
+
it "is not delegated when passed self" do
|
18
|
+
@base.should_not_receive(:==)
|
19
|
+
(@delegator == @delegator).should be_true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
it "is delegated when passed the delegated object" do
|
23
|
+
@base.should_receive(:==).and_return(false)
|
24
|
+
(@delegator == @base).should be_false
|
25
|
+
end
|
26
|
+
|
27
|
+
it "is delegated in general" do
|
28
|
+
@base.should_receive(:==).and_return(true)
|
29
|
+
(@delegator == 42).should be_true
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "Delegator when frozen" do
|
4
|
+
before :all do
|
5
|
+
@array = [42, :hello]
|
6
|
+
@delegate = DelegateSpecs::Delegator.new(@array)
|
7
|
+
@delegate.freeze
|
8
|
+
end
|
9
|
+
|
10
|
+
it "is still readable" do
|
11
|
+
@delegate.should == [42, :hello]
|
12
|
+
@delegate.include?("bar").should be_false
|
13
|
+
end
|
14
|
+
|
15
|
+
it "is frozen" do
|
16
|
+
@delegate.frozen?.should be_true
|
17
|
+
end
|
18
|
+
|
19
|
+
ruby_bug "redmine:2221", "1.8.7" do
|
20
|
+
it "is not writeable" do
|
21
|
+
lambda{ @delegate[0] += 2 }.should raise_error( RuntimeError )
|
22
|
+
end
|
23
|
+
|
24
|
+
it "creates a frozen clone" do
|
25
|
+
@delegate.clone.frozen?.should be_true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "creates an unfrozen dup" do
|
30
|
+
@delegate.dup.frozen?.should be_false
|
31
|
+
end
|
32
|
+
|
33
|
+
ruby_version_is "" ... "1.9" do
|
34
|
+
it "causes mutative calls to raise TypeError" do
|
35
|
+
lambda{ @delegate.__setobj__("hola!") }.should raise_error( TypeError )
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
ruby_version_is "1.9" do
|
40
|
+
it "causes mutative calls to raise RuntimeError" do
|
41
|
+
lambda{ @delegate.__setobj__("hola!") }.should raise_error( RuntimeError )
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it "returns false if only the delegated object is frozen" do
|
46
|
+
DelegateSpecs::Delegator.new([1,2,3].freeze).frozen?.should be_false
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
describe "Delegator#hash" do
|
4
|
+
ruby_bug "redmine:2224", "1.8" do
|
5
|
+
it "is delegated" do
|
6
|
+
base = mock('base')
|
7
|
+
delegator = DelegateSpecs::Delegator.new(base)
|
8
|
+
base.should_receive(:hash).and_return(42)
|
9
|
+
delegator.hash.should == 42
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
describe "SimpleDelegator" do
|
4
|
+
before :all do
|
5
|
+
@obj = "hello"
|
6
|
+
@delegate = SimpleDelegator.new(@obj)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "can be marshalled" do
|
10
|
+
m = Marshal.load(Marshal.dump(@delegate))
|
11
|
+
m.class.should == SimpleDelegator
|
12
|
+
(m == @obj).should be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
ruby_bug "redmine:1744", "1.8.7" do
|
16
|
+
it "can be marshalled with its instance variables intact" do
|
17
|
+
@delegate.instance_variable_set(:@foo, "bar")
|
18
|
+
m = Marshal.load(Marshal.dump(@delegate))
|
19
|
+
m.instance_variable_get(:@foo).should == "bar"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|