summaryrefslogtreecommitdiff
path: root/test/ruby/test_comparable.rb
blob: e558b190f0ce9c8a92a6868235c61666bf08ec4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'test/unit'

class TestComparable < Test::Unit::TestCase
  def setup
    @o = Object.new
    @o.extend(Comparable)
  end

  def test_equal
    def @o.<=>(x); 0; end
    assert_equal(true, @o == nil)
    def @o.<=>(x); 1; end
    assert_equal(false, @o == nil)
    def @o.<=>(x); raise; end
    assert_equal(false, @o == nil)
  end

  def test_gt
    def @o.<=>(x); 1; end
    assert_equal(true, @o > nil)
    def @o.<=>(x); 0; end
    assert_equal(false, @o > nil)
    def @o.<=>(x); -1; end
    assert_equal(false, @o > nil)
  end

  def test_ge
    def @o.<=>(x); 1; end
    assert_equal(true, @o >= nil)
    def @o.<=>(x); 0; end
    assert_equal(true, @o >= nil)
    def @o.<=>(x); -1; end
    assert_equal(false, @o >= nil)
  end

  def test_lt
    def @o.<=>(x); 1; end
    assert_equal(false, @o < nil)
    def @o.<=>(x); 0; end
    assert_equal(false, @o < nil)
    def @o.<=>(x); -1; end
    assert_equal(true, @o < nil)
  end

  def test_le
    def @o.<=>(x); 1; end
    assert_equal(false, @o <= nil)
    def @o.<=>(x); 0; end
    assert_equal(true, @o <= nil)
    def @o.<=>(x); -1; end
    assert_equal(true, @o <= nil)
  end

  def test_between
    def @o.<=>(x); 0 <=> x end
    assert_equal(false, @o.between?(1, 2))
    assert_equal(false, @o.between?(-2, -1))
    assert_equal(true, @o.between?(-1, 1))
    assert_equal(true, @o.between?(0, 0))
  end

  def test_err
    assert_raise(ArgumentError) { 1.0 < nil }
    assert_raise(ArgumentError) { 1.0 < Object.new }
  end
end