This repository has been archived by the owner on Mar 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtables_spec.rb
165 lines (120 loc) · 3.59 KB
/
tables_spec.rb
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#
# Specifying rufus-lua
#
# Fri Mar 13 23:42:29 JST 2009
#
require 'spec_base'
describe Rufus::Lua::State do
context 'tables' do
before do
@s = Rufus::Lua::State.new
end
after do
@s.close
end
it 'finds a hash' do
@s.eval('h = { a = "b", c = 2, 4 }')
expect(@s['h'].to_h).to eq({ 'a' => 'b', 'c' => 2.0, 1.0 => 4.0 })
end
it 'turns a hash into an array' do
@s.eval('a = { "a", "b", "c" }')
expect(@s['a'].to_h).to eq({ 1.0 => 'a', 2.0 => 'b', 3.0 => 'c' })
expect(@s['a'].to_a).to eq %w{ a b c }
end
it 'does nested lookups (2)' do
@s.eval('a = { b = { c = 0 } }')
expect(@s['a.b.c']).to eq 0
end
it 'returns Lua tables' do
expect(@s.eval('return {}').class).to eq Rufus::Lua::Table
end
it 'turns Lua tables into Ruby hashes' do
expect(@s.eval('return {}').to_h).to eq({})
end
it 'can free Lua tables' do
t = @s.eval('t = {}; return t')
t.free
expect(t.ref).to eq nil
expect(lambda { t.to_h }).to raise_error(RuntimeError)
end
it 'indexes tables' do
t = @s.eval("return { a = 'A' }")
expect(t['a']).to eq 'A'
expect(t['b']).to eq nil
end
it 'iterates over Lua tables' do
#t = @s.eval("return { a = 'A', b = 'B', c = 3, d = 3.1 }")
t = @s.eval("return { a = 'A', b = 'B', c = 3 }")
expect(t.values.sort_by { |v| v.to_s }).to eq [ 3.0, 'A', 'B' ]
expect(t.keys.sort).to eq [ 'a', 'b', 'c' ]
end
it 'provides keys and values for tables' do
t = @s.eval("return { a = 'A', b = 'B', c = 3 }")
expect(t.collect { |k, v| v }.size).to eq 3
end
it 'provides the size of a table' do
expect(@s.eval("return { a = 'A', b = 'B', c = 3 }").objlen).to eq 0.0
expect(@s.eval("return { 1, 2 }").objlen).to eq 2
expect(@s.eval("return { a = 'A', b = 'B', c = 3 }").size).to eq 3
expect(@s.eval("return { a = 'A', b = 'B', c = 3 }").length).to eq 3
expect(@s.eval("return { 1, 2 }").size).to eq 2
expect(@s.eval("return { 1, 2 }").length).to eq 2
end
it 'lets setting values in Lua tables from Ruby' do
t = @s.eval("return { a = 'A', b = 'B', c = 3 }")
t['b'] = 4
expect(t['b']).to eq 4.0
end
it 'indexes tables properly' do
@s.eval("t = { 'a', 'b', 'c' }")
expect(@s.eval("return t[0]")).to eq nil
expect(@s.eval("return t[1]")).to eq 'a'
expect(@s.eval("return t[3]")).to eq 'c'
expect(@s.eval("return t[4]")).to eq nil
end
it 'replies to to_a(false) (pure = false)' do
expect(@s.eval(
"return { a = 'A', b = 'B', c = 3 }"
).to_a(false).sort).to eq(
[ [ "a", "A" ], [ "b", "B" ], [ "c", 3.0 ] ]
)
expect(@s.eval(
"return { 1, 2 }"
).to_a(false)).to eq(
[ 1.0, 2.0 ]
)
expect(@s.eval(
"return {}"
).to_a(false)).to eq(
[]
)
expect(@s.eval(
"return { 1, 2, car = 'benz' }"
).to_a(false)).to eq(
[ 1.0, 2.0, ["car", "benz"] ]
)
end
it 'tries hard to honour #to_ruby' do
expect(@s.eval(
"return { a = 'A', b = 'B', c = 3 }"
).to_ruby).to eq(
{ "a" => "A", "b" => "B", "c" => 3.0 }
)
expect(@s.eval(
"return { 1, 2 }"
).to_ruby).to eq(
[ 1.0, 2.0 ]
)
expect(@s.eval(
"return {}"
).to_ruby).to eq(
[]
)
expect(@s.eval(
"return { 1, 2, car = 'benz' }"
).to_ruby).to eq(
{ 1.0 => 1.0, "car" => "benz", 2.0 => 2.0 }
)
end
end
end