diff options
Diffstat (limited to 'spec/ruby/library/weakref')
| -rw-r--r-- | spec/ruby/library/weakref/__getobj___spec.rb | 17 | ||||
| -rw-r--r-- | spec/ruby/library/weakref/allocate_spec.rb | 8 | ||||
| -rw-r--r-- | spec/ruby/library/weakref/fixtures/classes.rb | 26 | ||||
| -rw-r--r-- | spec/ruby/library/weakref/new_spec.rb | 13 | ||||
| -rw-r--r-- | spec/ruby/library/weakref/send_spec.rb | 37 | ||||
| -rw-r--r-- | spec/ruby/library/weakref/weakref_alive_spec.rb | 15 |
6 files changed, 116 insertions, 0 deletions
diff --git a/spec/ruby/library/weakref/__getobj___spec.rb b/spec/ruby/library/weakref/__getobj___spec.rb new file mode 100644 index 0000000000..fa507384c2 --- /dev/null +++ b/spec/ruby/library/weakref/__getobj___spec.rb @@ -0,0 +1,17 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "WeakRef#__getobj__" do + it "returns the object if it is reachable" do + obj = Object.new + ref = WeakRef.new(obj) + ref.__getobj__.should.equal?(obj) + end + + it "raises WeakRef::RefError if the object is no longer reachable" do + ref = WeakRefSpec.make_dead_weakref + -> { + ref.__getobj__ + }.should.raise(WeakRef::RefError) + end +end diff --git a/spec/ruby/library/weakref/allocate_spec.rb b/spec/ruby/library/weakref/allocate_spec.rb new file mode 100644 index 0000000000..0438d093c4 --- /dev/null +++ b/spec/ruby/library/weakref/allocate_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'weakref' + +describe "WeakRef#allocate" do + it "assigns nil as the reference" do + -> { WeakRef.allocate.__getobj__ }.should.raise(WeakRef::RefError) + end +end diff --git a/spec/ruby/library/weakref/fixtures/classes.rb b/spec/ruby/library/weakref/fixtures/classes.rb new file mode 100644 index 0000000000..041afab14d --- /dev/null +++ b/spec/ruby/library/weakref/fixtures/classes.rb @@ -0,0 +1,26 @@ +require 'weakref' + +# From MRI test_weakref.rb +class WeakRefSpec + def self.make_weakref(level = 10) + if level > 0 + make_weakref(level - 1) + else + WeakRef.new(Object.new) + end + end + + def self.make_dead_weakref + weaks = [] + weak = nil + 1000.times do + weaks << make_weakref + end + + 1000.times do + GC.start + break if weak = weaks.find { |w| !w.weakref_alive? } + end + weak + end +end diff --git a/spec/ruby/library/weakref/new_spec.rb b/spec/ruby/library/weakref/new_spec.rb new file mode 100644 index 0000000000..6290e61fe3 --- /dev/null +++ b/spec/ruby/library/weakref/new_spec.rb @@ -0,0 +1,13 @@ +require_relative '../../spec_helper' +require 'weakref' + +describe "WeakRef#new" do + it "creates a subclass correctly" do + wr2 = Class.new(WeakRef) { + def __getobj__ + :dummy + end + } + wr2.new(Object.new).__getobj__.should == :dummy + end +end diff --git a/spec/ruby/library/weakref/send_spec.rb b/spec/ruby/library/weakref/send_spec.rb new file mode 100644 index 0000000000..da8660066f --- /dev/null +++ b/spec/ruby/library/weakref/send_spec.rb @@ -0,0 +1,37 @@ +require_relative '../../spec_helper' +require 'weakref' + +describe "WeakRef#__send__" do + module WeakRefSpecs + class << self + def delegated_method + :result + end + + def protected_method + :result + end + protected :protected_method + + def private_method + :result + end + private :private_method + end + end + + it "delegates to public methods of the weakly-referenced object" do + wr = WeakRef.new(WeakRefSpecs) + wr.delegated_method.should == :result + end + + it "delegates to protected methods of the weakly-referenced object" do + wr = WeakRef.new(WeakRefSpecs) + -> { wr.protected_method }.should.raise(NameError) + end + + it "does not delegate to private methods of the weakly-referenced object" do + wr = WeakRef.new(WeakRefSpecs) + -> { wr.private_method }.should.raise(NameError) + end +end diff --git a/spec/ruby/library/weakref/weakref_alive_spec.rb b/spec/ruby/library/weakref/weakref_alive_spec.rb new file mode 100644 index 0000000000..1b12ffbbec --- /dev/null +++ b/spec/ruby/library/weakref/weakref_alive_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "WeakRef#weakref_alive?" do + it "returns true if the object is reachable" do + obj = Object.new + ref = WeakRef.new(obj) + ref.weakref_alive?.should == true + end + + it "returns a falsy value if the object is no longer reachable" do + ref = WeakRefSpec.make_dead_weakref + [false, nil].should.include?(ref.weakref_alive?) + end +end |
