~ubuntu-branches/ubuntu/wily/lua-lgi/wily

« back to all changes in this revision

Viewing changes to tests/variant.lua

  • Committer: Package Import Robot
  • Author(s): Enrico Tassi
  • Date: 2012-04-02 13:16:07 UTC
  • Revision ID: package-import@ubuntu.com-20120402131607-qcd5l8n9rx8lsq59
Tags: upstream-0.4+29+g74cbbb1
ImportĀ upstreamĀ versionĀ 0.4+29+g74cbbb1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--[[--------------------------------------------------------------------------
 
2
 
 
3
  LGI testsuite, GLib.Variant test suite.
 
4
 
 
5
  Copyright (c) 2010, 2011 Pavel Holejsovsky
 
6
  Licensed under the MIT license:
 
7
  http://www.opensource.org/licenses/mit-license.php
 
8
 
 
9
--]]--------------------------------------------------------------------------
 
10
 
 
11
local lgi = require 'lgi'
 
12
local GLib = lgi.GLib
 
13
local GObject = lgi.GObject
 
14
 
 
15
local check = testsuite.check
 
16
 
 
17
-- Variant testing
 
18
local variant = testsuite.group.new('variant')
 
19
 
 
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)
 
26
   val.value = var2
 
27
   check(val.value == var2)
 
28
   val.value = nil
 
29
   check(val.value == nil)
 
30
   check(val.gtype == GObject.Type.VARIANT)
 
31
end
 
32
 
 
33
function variant.newv_basic()
 
34
   local V, v = GLib.Variant
 
35
   v = V.new('b', true)
 
36
   check(v.type == 'b' and v:get_boolean() == true)
 
37
   v = V.new('y', 32)
 
38
   check(v.type == 'y' and v:get_byte() == 32)
 
39
   v = V.new('n', 13)
 
40
   check(v.type == 'n' and v:get_int16() == 13)
 
41
   v = V.new('q', 38)
 
42
   check(v.type == 'q' and v:get_uint16() == 38)
 
43
   v = V.new('i', 32)
 
44
   check(v.type == 'i' and v:get_int32() == 32)
 
45
   v = V.new('u', 35)
 
46
   check(v.type == 'u' and v:get_uint32() == 35)
 
47
   v = V.new('x', 39)
 
48
   check(v.type == 'x' and v:get_int64() == 39)
 
49
   v = V.new('t', 987)
 
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')
 
57
   v = V.new('g', "asi")
 
58
   check(v.type == 'g' and v:get_string() == 'asi')
 
59
   local vv = V.new('s', 'inner')
 
60
   v = V.new('v', vv)
 
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')
 
64
end
 
65
 
 
66
function variant.newv_variant()
 
67
   local V, v, vv = GLib.Variant
 
68
   vv = V('i', 14)
 
69
   v = V('v', vv)
 
70
   check(v.type == 'v' and v:n_children() == 1 and v:get_child_value(0) == vv)
 
71
end
 
72
 
 
73
function variant.newv_maybe()
 
74
   local V, v = GLib.Variant
 
75
   v = V('mi', 42)
 
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)
 
79
   v = V('mi')
 
80
   check(v.type == 'mi' and v:n_children() == 0)
 
81
end
 
82
 
 
83
function variant.newv_tuple()
 
84
   local V, v = GLib.Variant
 
85
   v = V.new('()')
 
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)
 
94
end
 
95
 
 
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')
 
104
end
 
105
 
 
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')
 
112
   v = V('as', {})
 
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')
 
121
end
 
122
 
 
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)
 
131
end
 
132
 
 
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'))
 
149
end
 
150
 
 
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)')
 
164
   v = V('i', 1)
 
165
   check(V('v', v).value == v)
 
166
   check(V('ay', 'bytestring').value == 'bytestring')
 
167
end
 
168
 
 
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)
 
173
   local r
 
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
 
178
         and r[3] == 1)
 
179
   v = V('as', {})
 
180
   check(v.value == v)
 
181
end
 
182
 
 
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)
 
190
 
 
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)
 
196
end
 
197
 
 
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)
 
210
end
 
211
 
 
212
function variant.indexing()
 
213
   local V, v = GLib.Variant
 
214
   v = V('mi', 1)
 
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)
 
223
end
 
224
 
 
225
function variant.serialize()
 
226
   local V, v1, v2 = GLib.Variant
 
227
   v1 = V('s', 'Hello')
 
228
   v2 = V.new_from_data(v1.type, v1.data)
 
229
   check(v1:equal(v2))
 
230
 
 
231
   -- Make sure that new_from_data properly keeps underlying data alive.
 
232
   v1 = nil collectgarbage()
 
233
   local _ = v2:print(true)
 
234
end