1
--[[--------------------------------------------------------------------------
3
LGI testsuite, GLib.Variant test suite.
5
Copyright (c) 2010, 2011 Pavel Holejsovsky
6
Licensed under the MIT license:
7
http://www.opensource.org/licenses/mit-license.php
9
--]]--------------------------------------------------------------------------
11
local lgi = require 'lgi'
13
local GObject = lgi.GObject
15
local check = testsuite.check
18
local variant = testsuite.group.new('variant')
20
function variant.gvalue()
21
local var1, var2 = GLib.Variant.new_string('foo'),
22
GLib.Variant.new_string('bar')
23
local val = GObject.Value(GObject.Type.VARIANT, var1)
24
check(val.gtype == GObject.Type.VARIANT)
25
check(val.value == var1)
27
check(val.value == var2)
29
check(val.value == nil)
30
check(val.gtype == GObject.Type.VARIANT)
33
function variant.newv_basic()
34
local V, v = GLib.Variant
36
check(v.type == 'b' and v:get_boolean() == true)
38
check(v.type == 'y' and v:get_byte() == 32)
40
check(v.type == 'n' and v:get_int16() == 13)
42
check(v.type == 'q' and v:get_uint16() == 38)
44
check(v.type == 'i' and v:get_int32() == 32)
46
check(v.type == 'u' and v:get_uint32() == 35)
48
check(v.type == 'x' and v:get_int64() == 39)
50
check(v.type == 't' and v:get_uint64() == 987)
51
v = V.new('d', 3.1415927)
52
check(v.type == 'd' and v:get_double() == 3.1415927)
53
v = V.new('s', 'Hello')
54
check(v.type == 's' and v:get_string() == 'Hello')
55
v = V.new('o', '/object/path')
56
check(v.type == 'o' and v:get_string() == '/object/path')
58
check(v.type == 'g' and v:get_string() == 'asi')
59
local vv = V.new('s', 'inner')
61
check(v.type == 'v' and v:get_variant() == vv)
62
v = V.new('ay', 'bytestring')
63
check(v.type == 'ay' and tostring(v:get_bytestring()) == 'bytestring')
66
function variant.newv_variant()
67
local V, v, vv = GLib.Variant
70
check(v.type == 'v' and v:n_children() == 1 and v:get_child_value(0) == vv)
73
function variant.newv_maybe()
74
local V, v = GLib.Variant
76
check(v.type == 'mi' and v:n_children() == 1
77
and v:get_child_value(0).type == 'i'
78
and v:get_child_value(0):get_int32() == 42)
80
check(v.type == 'mi' and v:n_children() == 0)
83
function variant.newv_tuple()
84
local V, v = GLib.Variant
86
check(v.type == '()' and v:n_children() == 0)
87
v = V.new('(i)', {42})
88
check(v.type == '(i)' and v:n_children() == 1
89
and v:get_child_value(0).type == 'i'
90
and v:get_child_value(0):get_int32() == 42)
91
v = V.new('(mii)', { nil, 1 })
92
check(v.type == '(mii)' and v:n_children() == 2
93
and v:get_child_value(0):n_children() == 0)
96
function variant.newv_dictentry()
97
local V, v = GLib.Variant
98
v = V('{is}', {42, 'Hello'})
99
check(v.type == '{is}' and v:n_children() == 2
100
and v:get_child_value(0).type == 'i'
101
and v:get_child_value(0):get_int32() == 42
102
and v:get_child_value(1).type == 's'
103
and v:get_child_value(1):get_string() == 'Hello')
106
function variant.newv_array()
107
local V, v = GLib.Variant
108
v = V('as', { 'Hello', 'world' })
109
check(v.type == 'as' and v:n_children() == 2
110
and v:get_child_value(0):get_string() == 'Hello'
111
and v:get_child_value(1):get_string() == 'world')
113
check(v:n_children() == 0)
114
v = V('ams', { 'Hello', nil, 'world', n = 3 })
115
check(v:n_children() == 3)
116
check(v:get_child_value(0):n_children() == 1
117
and v:get_child_value(0):get_child_value(0):get_string() == 'Hello')
118
check(v:get_child_value(1):n_children() == 0)
119
check(v:get_child_value(2):n_children() == 1
120
and v:get_child_value(2):get_child_value(0):get_string() == 'world')
123
function variant.newv_dictionary()
124
local V, v, vv = GLib.Variant
125
v = V('a{sd}', { PI = 3.14, one = 1 })
126
check(v:n_children() == 2)
127
vv = v:lookup_value('PI', GLib.VariantType.DOUBLE)
128
check(vv.type == 'd' and vv:get_double() == 3.14)
129
vv = v:lookup_value('one', GLib.VariantType.DOUBLE)
130
check(vv.type == 'd' and vv:get_double() == 1)
133
function variant.newv_badtype()
134
local V, v = GLib.Variant
135
check(not pcall(V.new, '{vs}'))
136
check(not pcall(V.new, '{s}'))
137
check(not pcall(V.new, '{}'))
138
check(not pcall(V.new, '())'))
139
check(not pcall(V.new, 'a'))
140
check(not pcall(V.new, 'm'))
141
check(not pcall(V.new, '{asi}'))
142
check(not pcall(V.new, '{mdd}'))
143
check(not pcall(V.new, '{is'))
144
check(not pcall(V.new, '{is)'))
145
check(not pcall(V.new, 'r'))
146
check(not pcall(V.new, '*'))
147
check(not pcall(V.new, '?'))
148
check(not pcall(V.new, 'ii'))
151
function variant.value_simple()
152
local V, v = GLib.Variant
153
check(V('b', true).value == true)
154
check(V('y', 10).value == 10)
155
check(V('n', 11).value == 11)
156
check(V('q', 12).value == 12)
157
check(V('i', 13).value == 13)
158
check(V('u', 14).value == 14)
159
check(V('q', 15).value == 15)
160
check(V('t', 16).value == 16)
161
check(V('s', 'I').value == 'I')
162
check(V('o', '/o/p').value == '/o/p')
163
check(V('g', '(ii)').value == '(ii)')
165
check(V('v', v).value == v)
166
check(V('ay', 'bytestring').value == 'bytestring')
169
function variant.value_container()
170
local V, v = GLib.Variant
171
check(V('mi', 1).value == 1)
172
check(V('mi', nil).value == nil)
174
r = V('{sd}', {'one', 1}).value
175
check(type(r) == 'table' and #r == 2 and r[1] == 'one' and r[2] == 1)
176
r = V('(imii)', {2, nil, 1}).value
177
check(type(r) == 'table' and r.n == 3 and r[1] == 2 and r[2] == nil
183
function variant.value_dictionary()
184
local V, v = GLib.Variant
185
v = V('a{sd}', { one = 1, two = 2 })
186
check(v.value.one == 1)
187
check(v.value.two == 2)
188
check(v.value.three == nil)
189
check(v.value[1] == nil)
191
v = V('a{is}', { [1] = 'one', [2] = 'two' })
192
check(v.value[1] == 'one')
193
check(v.value[2] == 'two')
194
check(v.value[3] == nil)
195
check(v.value.three == nil)
198
function variant.length()
199
local V, v = GLib.Variant
200
check(#V('s', 'Hello') == 0)
201
check(#V('i', 1) == 0)
202
check(#V('v', V('i', 1)) == 1)
203
check(#V('mi', nil) == 0)
204
check(#V('mi', 1) == 1)
205
check(#V('(ii)', {1, 2}) == 2)
206
check(#V('{sd}', { 'one', 1 }) == 2)
207
check(#V('a{sd}', { one = 1 }) == 1)
208
check(#V('ai', {}) == 0)
209
check(#V('ami', { 1, nil, 2, n = 3 }) == 3)
212
function variant.indexing()
213
local V, v = GLib.Variant
215
check(v[1] == 1 and v[2] == nil)
216
v = V('{sd}', { 'one', 1 })
217
check(v[1] == 'one' and v[2] == 1 and v[3] == nil)
218
v = V('a{sd}', { one = 1 })
219
check(v[1][1] == 'one' and v[1][2] == 1 and v[2] == nil)
220
v = V('(si)', { 'hi', 3 })
221
check(v[1] == 'hi' and v[2] == 3 and v[3] == nil)
222
check(V('s', 'hello')[1] == nil)
225
function variant.serialize()
226
local V, v1, v2 = GLib.Variant
228
v2 = V.new_from_data(v1.type, v1.data)
231
-- Make sure that new_from_data properly keeps underlying data alive.
232
v1 = nil collectgarbage()
233
local _ = v2:print(true)