# Ruby Treasures 0.4
# Copyright (C) 2002 Paul Brannan <paul@atdesk.com>
# 
# You may distribute this software under the same terms as Ruby (see the file
# COPYING that was distributed with this library).
# 
require 'ds_test_helpers'
require 'hacks/automatic_object'

class Foo
  include AutomaticObject

  def initialize
    $initialized = true
  end

  def finalize
    $finalized = true
  end
end

class MyException < RuntimeError
  attr_reader :x

  def initialize(x)
    @x = x
  end
end

class AutomaticObjectTest < RUNIT::TestCase
  def test_simple
    begin
      $initialized = false
      $finalized = false

      Foo.create do |f|
        # This keeps ruby from giving me "unknown node type 0"...
        assert_equal true, $initialized
        assert_equal false, $finalized
        assert_equal WeakRef, f.class
        assert_equal Foo, f.__getobj__.class
        assert_equal true, f.weakref_alive?
        raise MyException.new(f)
      end
    rescue MyException
      assert_equal true, $finalized
      f = $!.x
      # TODO: It's not possbile to force this object to be
      # garbage-collected.
      # assert_equal false, f.weakref_alive?
    end
  end
end

exit run_test(AutomaticObjectTest)

