variant.lua 7.1 KB

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