File: testStruct.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 (145 lines) | stat: -rw-r--r-- 2,744 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require 'test/minirunit'
test_check "Test Struct"

s = Struct.new("MyStruct", :x, :y)

test_equal(Struct::MyStruct, s)

s1 = s.new(11, 22)
test_equal(11, s1["x"])
test_equal(22, s1["y"])

s1 = s.new
test_equal(nil, s1.x)
test_equal(nil, s1.y)
s1.x = 10
s1.y = 20
test_equal(10, s1.x)
test_equal(20, s1.y)
test_equal(10, s1[0])
test_equal(20, s1[1])
test_equal(20, s1[-1])

test_equal(2, s1.length)
test_equal(2, s1.size)

test_equal([10, 20], s1.values)

test_exception(NameError) { s1["froboz"] }
test_exception(IndexError) { s1[10] }

s2 = s.new(1, 2)
test_equal(1, s2.x)
test_equal(2, s2.y)

test_exception(ArgumentError) { s.new(1,2,3,4,5,6,7) }
test_exception(ArgumentError) { Struct.new }
test_exception(NameError) { Struct.new('foo', 'bar') }

# name should be coerced to string with to_str
foo = Object.new
def foo.to_str() 'Foo' end
s = Struct.new(foo, :x, :y)
test_equal s, Struct::Foo

# Anonymous Struct
a = Struct.new(:x, :y)
a1 = a.new(5, 7)
test_equal(5, a1.x)

# Struct::Tms
tms = Struct::Tms.new(0, 0, 0, 0)
test_ok(tms != nil)

# Struct creation with a block
a = Struct.new(:foo, :bar) {
  def hello
    "hello"
  end
}

test_equal("hello", a.new(0, 0).hello)

require 'stringio'

module Recording
  def self.stderr
    $stderr = recorder = StringIO.new
    begin
      yield
    ensure
      $stderr = STDERR
    end
    recorder.rewind
    recorder
  end
end

# Redefining a named struct should produce a warning, but it should be a new class
P1 = Struct.new("Post", :foo)
P1.class_eval do
  def bar
    true
  end
end
Recording::stderr do
  P2 = Struct.new("Post", :foo)
end

test_exception {
  P2.new.bar
}

Recording::stderr do
  MyStruct = Struct.new("MyStruct", :a, :b)
end
class MySubStruct < MyStruct
  def initialize(v, *args) super(*args); @v = v; end 
end

b = MySubStruct.new(1, 2)
inspect1 = b.inspect
b.instance_eval {"EH"}
# Instance_eval creates a metaclass and our inspect should not print that new metaclass out
test_equal(inspect1, b.inspect)
c = MySubStruct.new(1, 2)

class << b
  def foo
  end
end

# Even though they have different metaclasses they are still equal in the eyes of Ruby
test_equal(b, c)

c = Struct.new(:a)
s1 = c.new(1)
s2 = c.new(1)
test_ok(true, s1.eql?(s2))
test_equal(s1.hash, s2.hash)

test_no_exception { Struct.new(:icandup).new(1).dup }

FiveElementStruct = Struct.new(:a, :b, :c, :d, :e)
fes = FiveElementStruct.new(1, 2, 3, 4, 5)
test_equal([2,4], fes.select {|i| (i % 2).zero?})

# JRUBY-2157
class Foo < Struct.new(:heh)
  def initialize
  end
end

test_equal(nil, Foo.new.heh)


# JRUBY-2490
require 'java'

class JavaComparableStruct < Struct.new(:foo)
  include java.lang.Comparable
  
  def compare_to(other); 0; end
end

test_equal(:a, JavaComparableStruct.new(:a).foo)