rubysl-delegate 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +25 -0
  6. data/README.md +29 -0
  7. data/Rakefile +1 -0
  8. data/lib/delegate.rb +1 -0
  9. data/lib/rubysl/delegate.rb +2 -0
  10. data/lib/rubysl/delegate/delegate.rb +337 -0
  11. data/lib/rubysl/delegate/version.rb +5 -0
  12. data/rubysl-delegate.gemspec +22 -0
  13. data/spec/delegate_class/instance_method_spec.rb +53 -0
  14. data/spec/delegate_class/instance_methods_spec.rb +49 -0
  15. data/spec/delegate_class/private_instance_methods_spec.rb +41 -0
  16. data/spec/delegate_class/protected_instance_methods_spec.rb +55 -0
  17. data/spec/delegate_class/public_instance_methods_spec.rb +45 -0
  18. data/spec/delegator/case_compare_spec.rb +10 -0
  19. data/spec/delegator/compare_spec.rb +10 -0
  20. data/spec/delegator/complement_spec.rb +12 -0
  21. data/spec/delegator/eql_spec.rb +12 -0
  22. data/spec/delegator/equal_spec.rb +12 -0
  23. data/spec/delegator/equal_value_spec.rb +31 -0
  24. data/spec/delegator/frozen_spec.rb +48 -0
  25. data/spec/delegator/hash_spec.rb +12 -0
  26. data/spec/delegator/marshal_spec.rb +22 -0
  27. data/spec/delegator/method_spec.rb +76 -0
  28. data/spec/delegator/methods_spec.rb +65 -0
  29. data/spec/delegator/not_equal_spec.rb +25 -0
  30. data/spec/delegator/not_spec.rb +12 -0
  31. data/spec/delegator/private_methods_spec.rb +33 -0
  32. data/spec/delegator/protected_methods_spec.rb +31 -0
  33. data/spec/delegator/public_methods_spec.rb +29 -0
  34. data/spec/delegator/send_spec.rb +33 -0
  35. data/spec/delegator/taint_spec.rb +24 -0
  36. data/spec/delegator/tap_spec.rb +17 -0
  37. data/spec/delegator/trust_spec.rb +41 -0
  38. data/spec/delegator/untaint_spec.rb +25 -0
  39. data/spec/delegator/untrust_spec.rb +24 -0
  40. data/spec/fixtures/classes.rb +65 -0
  41. metadata +153 -0
@@ -0,0 +1,76 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ describe "Delegator#method" do
4
+ before :all do
5
+ @simple = DelegateSpecs::Simple.new
6
+ @delegate = DelegateSpecs::Delegator.new(@simple)
7
+ end
8
+
9
+ it "returns a method object for public methods of the delegate object" do
10
+ m = @delegate.method(:pub)
11
+ m.should be_an_instance_of(Method)
12
+ m.call.should == :foo
13
+ end
14
+
15
+ ruby_version_is ""..."2.0" do
16
+ it "returns a method object for protected methods of the delegate object" do
17
+ m = @delegate.method(:prot)
18
+ m.should be_an_instance_of(Method)
19
+ m.call.should == :protected
20
+ end
21
+ end
22
+
23
+ ruby_version_is "2.0" do
24
+ it "returns a method object for protected methods of the delegate object" do
25
+ lambda {
26
+ @delegate.method(:prot)
27
+ }.should raise_error(NameError)
28
+ end
29
+ end
30
+
31
+ it "raises a NameError for a private methods of the delegate object" do
32
+ lambda {
33
+ @delegate.method(:priv)
34
+ }.should raise_error(NameError)
35
+ end
36
+
37
+ it "returns a method object for public methods of the Delegator class" do
38
+ m = @delegate.method(:extra)
39
+ m.should be_an_instance_of(Method)
40
+ m.call.should == :cheese
41
+ end
42
+
43
+ it "returns a method object for protected methods of the Delegator class" do
44
+ m = @delegate.method(:extra_protected)
45
+ m.should be_an_instance_of(Method)
46
+ m.call.should == :baz
47
+ end
48
+
49
+ it "returns a method object for private methods of the Delegator class" do
50
+ m = @delegate.method(:extra_private)
51
+ m.should be_an_instance_of(Method)
52
+ m.call.should == :bar
53
+ end
54
+
55
+ it "raises a NameError for an invalid method name" do
56
+ lambda {
57
+ @delegate.method(:invalid_and_silly_method_name)
58
+ }.should raise_error(NameError)
59
+ end
60
+
61
+ ruby_version_is "1.9" do
62
+ it "returns a method that respond_to_missing?" do
63
+ m = @delegate.method(:pub_too)
64
+ m.should be_an_instance_of(Method)
65
+ m.call.should == :pub_too
66
+ end
67
+ end
68
+
69
+ it "raises a NameError if method is no longer valid because object has changed" do
70
+ m = @delegate.method(:pub)
71
+ @delegate.__setobj__([1,2,3])
72
+ lambda {
73
+ m.call
74
+ }.should raise_error(NameError)
75
+ end
76
+ end
@@ -0,0 +1,65 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ describe "Delegator#methods" do
4
+ before :all do
5
+ @simple = DelegateSpecs::Simple.new
6
+ class << @simple
7
+ def singleton_method
8
+ end
9
+ end
10
+
11
+ @delegate = DelegateSpecs::Delegator.new(@simple)
12
+ @methods = @delegate.methods
13
+ end
14
+
15
+ ruby_version_is ""..."1.9" do
16
+ # See ruby_bug guarded spec below
17
+ it "returns singleton methods when passed false" do
18
+ @delegate.methods(false).should include("singleton_method")
19
+ end
20
+
21
+ it "includes all public methods of the delegate object" do
22
+ @methods.should include "pub"
23
+ end
24
+
25
+ it "includes all protected methods of the delegate object" do
26
+ @methods.should include "prot"
27
+ end
28
+
29
+ it "includes instance methods of the Delegator class" do
30
+ @methods.should include "extra"
31
+ @methods.should include "extra_protected"
32
+ end
33
+
34
+ it "does not include private methods" do
35
+ @methods.should_not include "priv"
36
+ @methods.should_not include "extra_private"
37
+ end
38
+ end
39
+
40
+ ruby_version_is "1.9" do
41
+ ruby_bug "4882", "1.9.3" do
42
+ it "returns singleton methods when passed false" do
43
+ @delegate.methods(false).should include(:singleton_method)
44
+ end
45
+ end
46
+
47
+ it "includes all public methods of the delegate object" do
48
+ @methods.should include :pub
49
+ end
50
+
51
+ it "includes all protected methods of the delegate object" do
52
+ @methods.should include :prot
53
+ end
54
+
55
+ it "includes instance methods of the Delegator class" do
56
+ @methods.should include :extra
57
+ @methods.should include :extra_protected
58
+ end
59
+
60
+ it "does not include private methods" do
61
+ @methods.should_not include :priv
62
+ @methods.should_not include :extra_private
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ ruby_version_is "1.9" do
4
+ describe "Delegator#!=" do
5
+ before :all do
6
+ @base = mock('base')
7
+ @delegator = DelegateSpecs::Delegator.new(@base)
8
+ end
9
+
10
+ it "is not delegated when passed self" do
11
+ @base.should_not_receive(:"!=")
12
+ (@delegator != @delegator).should be_false
13
+ end
14
+
15
+ it "is delegated when passed the delegated object" do
16
+ @base.should_receive(:"!=").and_return(true)
17
+ (@delegator != @base).should be_true
18
+ end
19
+
20
+ it "is delegated in general" do
21
+ @base.should_receive(:"!=").and_return(false)
22
+ (@delegator != 42).should be_false
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ ruby_version_is "1.9" do
4
+ describe "Delegator#!" 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,33 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ describe "Delegator#private_methods" do
4
+ before :all do
5
+ @simple = DelegateSpecs::Simple.new
6
+ @delegate = DelegateSpecs::Delegator.new(@simple)
7
+ @methods = @delegate.private_methods
8
+ end
9
+
10
+ ruby_version_is ""..."1.9" do
11
+ it "does not include any method of the delegate object" do # since delegates does not forward private calls
12
+ @methods.should_not include "priv"
13
+ @methods.should_not include "prot"
14
+ @methods.should_not include "pub"
15
+ end
16
+
17
+ it "includes all private instance methods of the Delegate class" do
18
+ @methods.should include "extra_private"
19
+ end
20
+ end
21
+
22
+ ruby_version_is "1.9" do
23
+ it "does not include any method of the delegate object" do # since delegates does not forward private calls
24
+ @methods.should_not include :priv
25
+ @methods.should_not include :prot
26
+ @methods.should_not include :pub
27
+ end
28
+
29
+ it "includes all private instance methods of the Delegate class" do
30
+ @methods.should include :extra_private
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ describe "Delegator#protected_methods" do
4
+ before :all do
5
+ @simple = DelegateSpecs::Simple.new
6
+ @delegate = DelegateSpecs::Delegator.new(@simple)
7
+ @methods = @delegate.protected_methods
8
+ end
9
+
10
+ ruby_version_is ""..."1.9" do
11
+ ruby_bug "#2496", "1.8" do
12
+ it "includes protected methods of the delegate object" do
13
+ @methods.should include "prot"
14
+ end
15
+ end
16
+
17
+ it "includes protected instance methods of the Delegator class" do
18
+ @methods.should include "extra_protected"
19
+ end
20
+ end
21
+
22
+ ruby_version_is "1.9" do
23
+ it "includes protected methods of the delegate object" do
24
+ @methods.should include :prot
25
+ end
26
+
27
+ it "includes protected instance methods of the Delegator class" do
28
+ @methods.should include :extra_protected
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ describe "Delegator#public_methods" do
4
+ before :all do
5
+ @simple = DelegateSpecs::Simple.new
6
+ @delegate = DelegateSpecs::Delegator.new(@simple)
7
+ @methods = @delegate.public_methods
8
+ end
9
+
10
+ ruby_version_is ""..."1.9" do
11
+ it "includes public methods of the delegate object" do
12
+ @methods.should include "pub"
13
+ end
14
+
15
+ it "includes public instance methods of the Delegator class" do
16
+ @methods.should include "extra"
17
+ end
18
+ end
19
+
20
+ ruby_version_is "1.9" do
21
+ it "includes public methods of the delegate object" do
22
+ @methods.should include :pub
23
+ end
24
+
25
+ it "includes public instance methods of the Delegator class" do
26
+ @methods.should include :extra
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ describe "SimpleDelegator.new" do
4
+ before :all do
5
+ @simple = DelegateSpecs::Simple.new
6
+ @delegate = SimpleDelegator.new(@simple)
7
+ end
8
+
9
+ it "forwards public method calls" do
10
+ @delegate.pub.should == :foo
11
+ end
12
+
13
+ ruby_version_is ""..."2.0" do
14
+ it "forwards protected method calls" do
15
+ @delegate.prot.should == :protected
16
+ end
17
+ end
18
+
19
+ ruby_version_is "2.0" do
20
+ it "forwards protected method calls" do
21
+ lambda{ @delegate.prot }.should raise_error( NoMethodError )
22
+ end
23
+ end
24
+
25
+ it "doesn't forward private method calls" do
26
+ lambda{ @delegate.priv }.should raise_error( NoMethodError )
27
+ end
28
+
29
+ it "doesn't forward private method calls even via send or __send__" do
30
+ lambda{ @delegate.send(:priv, 42) }.should raise_error( NoMethodError )
31
+ lambda{ @delegate.__send__(:priv, 42) }.should raise_error( NoMethodError )
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ describe "Delegator#taint" do
4
+ before :each do
5
+ @delegate = DelegateSpecs::Delegator.new("")
6
+ end
7
+
8
+ it "returns self" do
9
+ @delegate.taint.equal?(@delegate).should be_true
10
+ end
11
+
12
+ it "taints the delegator" do
13
+ @delegate.__setobj__(nil)
14
+ @delegate.taint
15
+ @delegate.tainted?.should be_true
16
+ end
17
+
18
+ ruby_bug "redmine:2223", "1.8" do
19
+ it "taints the delegated object" do
20
+ @delegate.taint
21
+ @delegate.__getobj__.tainted?.should be_true
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ ruby_version_is "1.9" do
4
+ describe "Delegator#tap" do
5
+ it "yield the delegator object" do
6
+ obj = mock('base')
7
+ delegator = DelegateSpecs::Delegator.new(obj)
8
+ obj.should_not_receive(:tap)
9
+ yielded = []
10
+ delegator.tap do |x|
11
+ yielded << x
12
+ end
13
+ yielded.size.should == 1
14
+ yielded[0].equal?(delegator).should be_true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ ruby_version_is "1.9" do
4
+ describe "Delegator#trust" do
5
+ before :each do
6
+ @delegate = DelegateSpecs::Delegator.new([])
7
+ end
8
+
9
+ it "returns self" do
10
+ @delegate.trust.equal?(@delegate).should be_true
11
+ end
12
+
13
+ it "trusts the delegator" do
14
+ @delegate.trust
15
+ @delegate.untrusted?.should be_false
16
+ end
17
+
18
+ it "trusts the delegated object" do
19
+ @delegate.trust
20
+ @delegate.__getobj__.untrusted?.should be_false
21
+ end
22
+ end
23
+
24
+ not_compliant_on :rubinius do
25
+ describe "Delegator#trust" do
26
+ before :each do
27
+ @delegate = lambda { $SAFE=4; DelegateSpecs::Delegator.new([]) }.call
28
+ end
29
+
30
+ it "raises a SecurityError when modifying a trusted delegator" do
31
+ @delegate.trust
32
+ lambda { $SAFE=4; @delegate.data = :foo }.should raise_error(SecurityError)
33
+ end
34
+
35
+ it "raises a SecurityError when modifying a trusted delegate" do
36
+ @delegate.trust
37
+ lambda { $SAFE=4; @delegate << 42 }.should raise_error(SecurityError)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ describe "Delegator#untaint" do
4
+ before :each do
5
+ @delegate = lambda { DelegateSpecs::Delegator.new("") }.call
6
+ end
7
+
8
+ it "returns self" do
9
+ @delegate.untaint.equal?(@delegate).should be_true
10
+ end
11
+
12
+ it "untaints the delegator" do
13
+ @delegate.untaint
14
+ @delegate.tainted?.should be_false
15
+ # No additional meaningful test; that it does or not taint
16
+ # "for real" the delegator has no consequence
17
+ end
18
+
19
+ ruby_bug "redmine:2223", "1.8" do
20
+ it "untaints the delegated object" do
21
+ @delegate.untaint
22
+ @delegate.__getobj__.tainted?.should be_false
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../../fixtures/classes', __FILE__)
2
+
3
+ ruby_version_is "1.9" do
4
+ describe "Delegator#untrust" do
5
+ before :each do
6
+ @delegate = DelegateSpecs::Delegator.new("")
7
+ end
8
+
9
+ it "returns self" do
10
+ @delegate.untrust.equal?(@delegate).should be_true
11
+ end
12
+
13
+ it "untrusts the delegator" do
14
+ @delegate.__setobj__(nil)
15
+ @delegate.untrust
16
+ @delegate.untrusted?.should be_true
17
+ end
18
+
19
+ it "untrusts the delegated object" do
20
+ @delegate.untrust
21
+ @delegate.__getobj__.untrusted?.should be_true
22
+ end
23
+ end
24
+ end