Hi all,

I decided to take a stab at creating a minimal test suite for WeakRef 
(based on a post by Charles Oliver Nutter for JRuby). The only problem 
is that I get two unexpected failures. I'm not sure if it's Test::Unit 
fighting with me or what.

Consider the following example:

require 'weakref'
str = 'hello'
ref = WeakRef.new(str)

p ref 'hello'
GC.start
p ref 'hello'
str = nil
p ref # 'hello'
GC.start
p ref # error

I follow the same pattern in the tests, but it doesn't raise the 
expected error. Any ideas?

Thanks,

Dan

# tc_weakref.rb
require 'test/unit'
require 'weakref'

class TC_WeakRef < Test::Unit::TestCase
    def setup
       @ref = nil
       @str = 'hello'
       GC.enable
    end

    def test_weakref_constructor
       assert_respond_to(WeakRef, :new)
       assert_nothing_raised{ @ref = WeakRef.new(@str) }
       assert_kind_of(WeakRef, @ref)
    end

    # TODO: Figure out why last test fails
    def test_weakref
       assert_nothing_raised{ @ref = WeakRef.new(@str) }
       assert_equal('hello', @ref)

       assert_nothing_raised{ GC.start }
       assert_equal('hello', @ref)

       assert_nothing_raised{ @str = nil }
       assert_equal('hello', @ref)

       assert_nothing_raised{ GC.start }
       assert_raise(WeakRef::RefError){ @str = @ref * 3 }
    end

    def test_weakref_is_alive_basic
       assert_nothing_raised{ @ref = WeakRef.new(@str) }
       assert_respond_to(@ref, :weakref_alive?)
    end

    # TODO: Figure out why last test fails
    def test_weakref_is_alive
       assert_nothing_raised{ @ref = WeakRef.new(@str) }
       assert_equal(true, @ref.weakref_alive?)

       assert_nothing_raised{ GC.start }
       assert_equal(true, @ref.weakref_alive?)

       assert_nothing_raised{ @str = nil }
       assert_equal(true, @ref.weakref_alive?)

       assert_nothing_raised{ GC.start }
       assert_equal(false, @ref.weakref_alive?)
    end

    def teardown
       @str = nil
       @ref = nil
    end
end