File: test_struct.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (127 lines) | stat: -rw-r--r-- 2,909 bytes parent folder | download | duplicates (4)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require 'test/unit'

class TestStruct < Test::Unit::TestCase

  def setup
    if !defined? Struct::StructForTesting
      @@myclass = Struct.new("StructForTesting", :alpha, :bravo)
    end
  end

  def test_00s_new
    assert_equal(Struct::StructForTesting, @@myclass)
    t1 = @@myclass.new
    t1.alpha = 1
    t1.bravo = 2
    assert_equal(1,t1.alpha)
    assert_equal(2,t1.bravo)
    t2 = Struct::StructForTesting.new
    assert_equal(t1.class, t2.class)
    t2.alpha = 3
    t2.bravo = 4
    assert_equal(3,t2.alpha)
    assert_equal(4,t2.bravo)
    assert_raise(ArgumentError) { Struct::StructForTesting.new(1,2,3) }

    t3 = @@myclass.new(5,6)
    assert_equal(5,t3.alpha)
    assert_equal(6,t3.bravo)
  end

  def test_AREF # '[]'
    t = Struct::StructForTesting.new
    t.alpha = 64
    t.bravo = 112
    assert_equal(64,  t["alpha"])
    assert_equal(64,  t[0])
    assert_equal(112, t[1])
    assert_equal(112, t[-1])
    assert_equal(t["alpha"], t[:alpha])
  
    assert_raise(NameError)  { p t["gamma"] }
    assert_raise(IndexError) { p t[2] }
  end

  def test_ASET # '[]='
    t = Struct::StructForTesting.new
    t.alpha = 64
    assert_equal(64,t["alpha"])
    assert_equal(t["alpha"],t[:alpha])
    assert_raise(NameError) { t["gamma"]=1 }
    assert_raise(IndexError) { t[2]=1 }
  end

  def test_EQUAL # '=='
    t1 = Struct::StructForTesting.new
    t1.alpha = 64
    t1.bravo = 42
    t2 = Struct::StructForTesting.new
    t2.alpha = 64
    t2.bravo = 42
    assert_equal(t1,t2)
  end

  def test_clone
    for taint in [ false, true ]
      for frozen in [ false, true ]
        a = Struct::StructForTesting.new
        a.alpha = 112
        a.taint  if taint
        a.freeze if frozen
        b = a.clone

        assert_equal(a, b)
        assert(a.__id__ != b.__id__)
        assert_equal(a.frozen?,  b.frozen?)
        assert_equal(a.tainted?, b.tainted?)
        assert_equal(a.alpha,    b.alpha)
      end
    end
  end

  def test_each
    a=[]
    Struct::StructForTesting.new('a', 'b').each {|x|
      a << x
    }
    assert_equal(['a','b'], a)
  end

  def test_length
    t = Struct::StructForTesting.new
    assert_equal(2,t.length)
  end

  def test_members
    assert_equal(Struct::StructForTesting.members, [ "alpha", "bravo" ])
  end

  def test_size
    t = Struct::StructForTesting.new
    assert_equal(2, t.length)
  end

  def test_to_a
    t = Struct::StructForTesting.new('a','b')
    assert_equal(['a','b'], t.to_a)
  end

  def test_values
    t = Struct::StructForTesting.new('a','b')
    assert_equal(['a','b'], t.values)
  end

  def test_anonymous_struct
    t = Struct.new(:foo)
    u = Struct.new(nil, :foo)

    assert_equal("", t.name)
    assert_equal("", u.name)
    assert_equal('foo', t.new('foo')[:foo])
    assert_equal('foo', u.new('foo')[:foo])
    assert_equal('foo', t.new('foo').foo)
    assert_equal('foo', u.new('foo').foo)
  end


end