# Ruby Treasures 0.1
# Copyright (C) 2001 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.rb'
require 'enum'

class EnumTest < DS_Test_Case

  E = Enum.new(:A, :B, :C, 10, :D)
  F = Enum[:B, :C, :D]

  def initialize(*args)
    super(*args)
    DS_Test_Case.class_method_checked(:[], Enum)
  end

  def test_each
    expected_values = [
      [ EnumTest::E, 'A', 0 ],
      [ EnumTest::E, 'B', 1 ],
      [ EnumTest::E, 'C', 2 ],
      [ EnumTest::E, 'D', 10 ]
    ]
    j = 0
    E.each do |value|
      assert_equal value.type, expected_values[j][0]
      assert_equal value.to_s, expected_values[j][1]
      assert_equal value.to_i, expected_values[j][2]
      j = j.succ
    end
  end

  def test_type
    e = E::A
    f = F::B
    assert e.type != f.type
  end

  def test_enum
    a = E::A
    b = E.new(:A)
    c = E.new(0)
    assert_exception(NameError) do
      d = E.new(:Q)
    end
    assert_equal a, b
    assert_equal a, c
  end

  def test_constants
    E.each do |value|
      assert E.constants.index(value.to_s)
    end
    F.each do |value|
      assert F.constants.index(value.to_s)
    end
  end
  
  def test_lookup
    lookup1 = E.lookup_table()
    lookup2 = E.lookup_table()
    assert_equal(lookup1, lookup2)
    assert_containers_equal(lookup1, lookup2)
  end

  def test_misc
    assert E::A === E::A
    assert E::A == E::A
    assert !(E::A < E::A)
    assert !(E::A === F::B)
    assert !(E::A == F::B)
  end
end

exit run_test(EnumTest, Enum)

