gireg.lua 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. --[[--------------------------------------------------------------------------
  2. LGI testsuite, GI Regress-based testsuite.
  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 bytes = require 'bytes'
  9. local fail = testsuite.fail
  10. local check = testsuite.check
  11. local checkv = testsuite.checkv
  12. local gireg = testsuite.group.new('gireg')
  13. function gireg.type_boolean()
  14. local R = lgi.Regress
  15. checkv(R.test_boolean(true), true, 'boolean')
  16. checkv(R.test_boolean(false), false, 'boolean')
  17. check(select('#', R.test_boolean(true)) == 1)
  18. check(select('#', R.test_boolean(false)) == 1)
  19. checkv(R.test_boolean(), false, 'boolean')
  20. checkv(R.test_boolean(nil), false, 'boolean')
  21. checkv(R.test_boolean(0), true, 'boolean')
  22. checkv(R.test_boolean(1), true, 'boolean')
  23. checkv(R.test_boolean('string'), true, 'boolean')
  24. checkv(R.test_boolean({}), true, 'boolean')
  25. checkv(R.test_boolean(function() end), true, 'boolean')
  26. end
  27. function gireg.type_int8()
  28. local R = lgi.Regress
  29. checkv(R.test_int8(0), 0, 'number')
  30. checkv(R.test_int8(1), 1, 'number')
  31. checkv(R.test_int8(-1), -1, 'number')
  32. checkv(R.test_int8(1.1), 1, 'number')
  33. checkv(R.test_int8(-1.1), -1, 'number')
  34. checkv(R.test_int8(0x7f), 0x7f, 'number')
  35. checkv(R.test_int8(-0x80), -0x80, 'number')
  36. check(not pcall(R.test_int8, 0x80))
  37. check(not pcall(R.test_int8, -0x81))
  38. check(not pcall(R.test_int8))
  39. check(not pcall(R.test_int8, nil))
  40. check(not pcall(R.test_int8, 'string'))
  41. check(not pcall(R.test_int8, true))
  42. check(not pcall(R.test_int8, {}))
  43. check(not pcall(R.test_int8, function() end))
  44. end
  45. function gireg.type_uint8()
  46. local R = lgi.Regress
  47. checkv(R.test_uint8(0), 0, 'number')
  48. checkv(R.test_uint8(1), 1, 'number')
  49. checkv(R.test_uint8(1.1), 1, 'number')
  50. checkv(R.test_uint8(0xff), 0xff, 'number')
  51. check(not pcall(R.test_uint8, 0x100))
  52. check(not pcall(R.test_uint8, -1))
  53. check(not pcall(R.test_uint8))
  54. check(not pcall(R.test_uint8, nil))
  55. check(not pcall(R.test_uint8, 'string'))
  56. check(not pcall(R.test_uint8, true))
  57. check(not pcall(R.test_uint8, {}))
  58. check(not pcall(R.test_uint8, function() end))
  59. end
  60. function gireg.type_int16()
  61. local R = lgi.Regress
  62. checkv(R.test_int16(0), 0, 'number')
  63. checkv(R.test_int16(1), 1, 'number')
  64. checkv(R.test_int16(-1), -1, 'number')
  65. checkv(R.test_int16(1.1), 1, 'number')
  66. checkv(R.test_int16(-1.1), -1, 'number')
  67. checkv(R.test_int16(0x7fff), 0x7fff, 'number')
  68. checkv(R.test_int16(-0x8000), -0x8000, 'number')
  69. check(not pcall(R.test_int16, 0x8000))
  70. check(not pcall(R.test_int16, -0x8001))
  71. check(not pcall(R.test_int16))
  72. check(not pcall(R.test_int16, nil))
  73. check(not pcall(R.test_int16, 'string'))
  74. check(not pcall(R.test_int16, true))
  75. check(not pcall(R.test_int16, {}))
  76. check(not pcall(R.test_int16, function() end))
  77. end
  78. function gireg.type_uint16()
  79. local R = lgi.Regress
  80. checkv(R.test_uint16(0), 0, 'number')
  81. checkv(R.test_uint16(1), 1, 'number')
  82. checkv(R.test_uint16(1.1), 1, 'number')
  83. checkv(R.test_uint16(0xffff), 0xffff, 'number')
  84. check(not pcall(R.test_uint16, 0x10000))
  85. check(not pcall(R.test_uint16, -1))
  86. check(not pcall(R.test_uint16))
  87. check(not pcall(R.test_uint16, nil))
  88. check(not pcall(R.test_uint16, 'string'))
  89. check(not pcall(R.test_uint16, true))
  90. check(not pcall(R.test_uint16, {}))
  91. check(not pcall(R.test_uint16, function() end))
  92. end
  93. function gireg.type_int32()
  94. local R = lgi.Regress
  95. checkv(R.test_int32(0), 0, 'number')
  96. checkv(R.test_int32(1), 1, 'number')
  97. checkv(R.test_int32(-1), -1, 'number')
  98. checkv(R.test_int32(1.1), 1, 'number')
  99. checkv(R.test_int32(-1.1), -1, 'number')
  100. checkv(R.test_int32(0x7fffffff), 0x7fffffff, 'number')
  101. checkv(R.test_int32(-0x80000000), -0x80000000, 'number')
  102. check(not pcall(R.test_int32, 0x80000000))
  103. check(not pcall(R.test_int32, -0x80000001))
  104. check(not pcall(R.test_int32))
  105. check(not pcall(R.test_int32, nil))
  106. check(not pcall(R.test_int32, 'string'))
  107. check(not pcall(R.test_int32, true))
  108. check(not pcall(R.test_int32, {}))
  109. check(not pcall(R.test_int32, function() end))
  110. end
  111. function gireg.type_uint32()
  112. local R = lgi.Regress
  113. checkv(R.test_uint32(0), 0, 'number')
  114. checkv(R.test_uint32(1), 1, 'number')
  115. checkv(R.test_uint32(1.1), 1, 'number')
  116. checkv(R.test_uint32(0xffffffff), 0xffffffff, 'number')
  117. check(not pcall(R.test_uint32, 0x100000000))
  118. check(not pcall(R.test_uint32, -1))
  119. check(not pcall(R.test_uint32))
  120. check(not pcall(R.test_uint32, nil))
  121. check(not pcall(R.test_uint32, 'string'))
  122. check(not pcall(R.test_uint32, true))
  123. check(not pcall(R.test_uint32, {}))
  124. check(not pcall(R.test_uint32, function() end))
  125. end
  126. function gireg.type_int64()
  127. local R = lgi.Regress
  128. checkv(R.test_int64(0), 0, 'number')
  129. checkv(R.test_int64(1), 1, 'number')
  130. checkv(R.test_int64(-1), -1, 'number')
  131. checkv(R.test_int64(1.1), 1, 'number')
  132. checkv(R.test_int64(-1.1), -1, 'number')
  133. check(not pcall(R.test_int64))
  134. check(not pcall(R.test_int64, nil))
  135. check(not pcall(R.test_int64, 'string'))
  136. check(not pcall(R.test_int64, true))
  137. check(not pcall(R.test_int64, {}))
  138. check(not pcall(R.test_int64, function() end))
  139. -- Following tests fail because Lua's internal number representation
  140. -- is always 'double', and conversion between double and int64 big
  141. -- constants is always lossy. Not sure if it can be solved somehow.
  142. -- checkv(R.test_int64(0x7fffffffffffffff), 0x7fffffffffffffff, 'number')
  143. -- checkv(R.test_int64(-0x8000000000000000), -0x8000000000000000, 'number')
  144. -- check(not pcall(R.test_int64, 0x8000000000000000))
  145. -- check(not pcall(R.test_int64, -0x8000000000000001))
  146. end
  147. function gireg.type_uint64()
  148. local R = lgi.Regress
  149. checkv(R.test_uint64(0), 0, 'number')
  150. checkv(R.test_uint64(1), 1, 'number')
  151. checkv(R.test_uint64(1.1), 1, 'number')
  152. check(not pcall(R.test_uint64, -1))
  153. check(not pcall(R.test_uint64))
  154. check(not pcall(R.test_uint64, nil))
  155. check(not pcall(R.test_uint64, 'string'))
  156. check(not pcall(R.test_uint64, true))
  157. check(not pcall(R.test_uint64, {}))
  158. check(not pcall(R.test_uint64, function() end))
  159. -- See comment above about lossy conversions.
  160. -- checkv(R.test_uint64(0xffffffffffffffff), 0xffffffffffffffff, 'number')
  161. -- check(not pcall(R.test_uint64, 0x10000000000000000))
  162. end
  163. function gireg.type_short()
  164. local R = lgi.Regress
  165. checkv(R.test_short(0), 0, 'number')
  166. checkv(R.test_short(1), 1, 'number')
  167. checkv(R.test_short(-1), -1, 'number')
  168. checkv(R.test_short(1.1), 1, 'number')
  169. checkv(R.test_short(-1.1), -1, 'number')
  170. end
  171. function gireg.type_ushort()
  172. local R = lgi.Regress
  173. checkv(R.test_ushort(0), 0, 'number')
  174. checkv(R.test_ushort(1), 1, 'number')
  175. checkv(R.test_ushort(1.1), 1, 'number')
  176. check(not pcall(R.test_ushort, -1))
  177. end
  178. function gireg.type_int()
  179. local R = lgi.Regress
  180. checkv(R.test_int(0), 0, 'number')
  181. checkv(R.test_int(1), 1, 'number')
  182. checkv(R.test_int(-1), -1, 'number')
  183. checkv(R.test_int(1.1), 1, 'number')
  184. checkv(R.test_int(-1.1), -1, 'number')
  185. end
  186. function gireg.type_uint()
  187. local R = lgi.Regress
  188. checkv(R.test_uint(0), 0, 'number')
  189. checkv(R.test_uint(1), 1, 'number')
  190. checkv(R.test_uint(1.1), 1, 'number')
  191. check(not pcall(R.test_uint, -1))
  192. end
  193. function gireg.type_ssize()
  194. local R = lgi.Regress
  195. checkv(R.test_ssize(0), 0, 'number')
  196. checkv(R.test_ssize(1), 1, 'number')
  197. checkv(R.test_ssize(-1), -1, 'number')
  198. checkv(R.test_ssize(1.1), 1, 'number')
  199. checkv(R.test_ssize(-1.1), -1, 'number')
  200. end
  201. function gireg.type_size()
  202. local R = lgi.Regress
  203. checkv(R.test_size(0), 0, 'number')
  204. checkv(R.test_size(1), 1, 'number')
  205. checkv(R.test_size(1.1), 1, 'number')
  206. check(not pcall(R.test_size, -1))
  207. end
  208. -- Helper, checks that given value has requested type and value, with some
  209. -- tolerance because of low precision of gfloat type.
  210. local function checkvf(val, exp, tolerance)
  211. check(type(val) == 'number', string.format(
  212. "got type `%s', expected `number'", type(val)), 2)
  213. check(math.abs(val - exp) <= tolerance,
  214. string.format("got value `%s', expected `%s'",
  215. tostring(val), tostring(exp)), 2)
  216. end
  217. function gireg.type_float()
  218. local R = lgi.Regress
  219. local t = 0.0000001
  220. checkvf(R.test_float(0), 0, t)
  221. checkvf(R.test_float(1), 1, t)
  222. checkvf(R.test_float(1.1), 1.1, t)
  223. checkvf(R.test_float(-1), -1, t)
  224. checkvf(R.test_float(-1.1), -1.1, t)
  225. checkvf(R.test_float(0x8000), 0x8000, t)
  226. checkvf(R.test_float(0xffff), 0xffff, t)
  227. checkvf(R.test_float(-0x8000), -0x8000, t)
  228. checkvf(R.test_float(-0xffff), -0xffff, t)
  229. check(not pcall(R.test_float))
  230. check(not pcall(R.test_float, nil))
  231. check(not pcall(R.test_float, 'string'))
  232. check(not pcall(R.test_float, true))
  233. check(not pcall(R.test_float, {}))
  234. check(not pcall(R.test_float, function() end))
  235. end
  236. function gireg.type_double()
  237. local R = lgi.Regress
  238. checkv(R.test_double(0), 0, 'number')
  239. checkv(R.test_double(1), 1, 'number')
  240. checkv(R.test_double(1.1), 1.1, 'number')
  241. checkv(R.test_double(-1), -1, 'number')
  242. checkv(R.test_double(-1.1), -1.1, 'number')
  243. checkv(R.test_double(0x80000000), 0x80000000, 'number')
  244. checkv(R.test_double(0xffffffff), 0xffffffff, 'number')
  245. checkv(R.test_double(-0x80000000), -0x80000000, 'number')
  246. checkv(R.test_double(-0xffffffff), -0xffffffff, 'number')
  247. check(not pcall(R.test_double))
  248. check(not pcall(R.test_double, nil))
  249. check(not pcall(R.test_double, 'string'))
  250. check(not pcall(R.test_double, true))
  251. check(not pcall(R.test_double, {}))
  252. check(not pcall(R.test_double, function() end))
  253. end
  254. function gireg.type_timet()
  255. local R = lgi.Regress
  256. checkv(R.test_timet(0), 0, 'number')
  257. checkv(R.test_timet(1), 1, 'number')
  258. checkv(R.test_timet(10000), 10000, 'number')
  259. check(not pcall(R.test_timet))
  260. check(not pcall(R.test_timet, nil))
  261. check(not pcall(R.test_timet, 'string'))
  262. check(not pcall(R.test_timet, true))
  263. check(not pcall(R.test_timet, {}))
  264. check(not pcall(R.test_timet, function() end))
  265. end
  266. function gireg.type_gtype()
  267. local R = lgi.Regress
  268. checkv(R.test_gtype(), nil, 'nil')
  269. checkv(R.test_gtype(nil), nil, 'nil')
  270. checkv(R.test_gtype(0), nil, 'nil')
  271. checkv(R.test_gtype('void'), 'void', 'string')
  272. checkv(R.test_gtype(4), 'void', 'string')
  273. checkv(R.test_gtype('GObject'), 'GObject', 'string')
  274. checkv(R.test_gtype(80), 'GObject', 'string')
  275. checkv(R.test_gtype(R.TestObj), 'RegressTestObj', 'string')
  276. check(not pcall(R.test_gtype, true))
  277. check(not pcall(R.test_gtype, function() end))
  278. end
  279. function gireg.utf8_const_return()
  280. local R = lgi.Regress
  281. local utf8_const = 'const \226\153\165 utf8'
  282. check(R.test_utf8_const_return() == utf8_const)
  283. end
  284. function gireg.utf8_nonconst_return()
  285. local R = lgi.Regress
  286. local utf8_nonconst = 'nonconst \226\153\165 utf8'
  287. check(R.test_utf8_nonconst_return() == utf8_nonconst)
  288. end
  289. function gireg.utf8_const_in()
  290. local R = lgi.Regress
  291. local utf8_const = 'const \226\153\165 utf8'
  292. R.test_utf8_const_in(utf8_const)
  293. R.test_utf8_const_in(bytes.new(utf8_const .. '\0'))
  294. end
  295. function gireg.utf8_out()
  296. local R = lgi.Regress
  297. local utf8_nonconst = 'nonconst \226\153\165 utf8'
  298. check(R.test_utf8_out() == utf8_nonconst)
  299. end
  300. function gireg.utf8_inout()
  301. local R = lgi.Regress
  302. local utf8_const = 'const \226\153\165 utf8'
  303. local utf8_nonconst = 'nonconst \226\153\165 utf8'
  304. check(R.test_utf8_inout(utf8_const) == utf8_nonconst)
  305. end
  306. function gireg.filename_return()
  307. local R = lgi.Regress
  308. local fns = R.test_filename_return()
  309. check(type(fns) == 'table')
  310. check(#fns == 2)
  311. check(fns[1] == 'åäö')
  312. check(fns[2] == '/etc/fstab')
  313. end
  314. function gireg.utf8_int_out_utf8()
  315. local R = lgi.Regress
  316. check(R.test_int_out_utf8('') == 0)
  317. check(R.test_int_out_utf8('abc') == 3)
  318. local utf8_const = 'const \226\153\165 utf8'
  319. check(R.test_int_out_utf8(utf8_const) == 12)
  320. end
  321. function gireg.multi_double_args()
  322. local R = lgi.Regress
  323. local o1, o2 = R.test_multi_double_args(1)
  324. check(o1 == 2 and o2 == 3)
  325. check(#{R.test_multi_double_args(1)} == 2)
  326. end
  327. function gireg.utf8_out_out()
  328. local R = lgi.Regress
  329. local o1, o2 = R.test_utf8_out_out()
  330. check(o1 == 'first' and o2 == 'second')
  331. check(#{R.test_utf8_out_out()} == 2)
  332. end
  333. function gireg.utf8_out_nonconst_return()
  334. local R = lgi.Regress
  335. local o1, o2 = R.test_utf8_out_nonconst_return()
  336. check(o1 == 'first' and o2 == 'second')
  337. check(#{R.test_utf8_out_nonconst_return()} == 2)
  338. end
  339. function gireg.utf8_null_in()
  340. local R = lgi.Regress
  341. R.test_utf8_null_in(nil)
  342. R.test_utf8_null_in()
  343. end
  344. function gireg.utf8_null_out()
  345. local R = lgi.Regress
  346. check(R.test_utf8_null_out() == nil)
  347. end
  348. function gireg.array_int_in()
  349. local R = lgi.Regress
  350. check(R.test_array_int_in{1,2,3} == 6)
  351. check(R.test_array_int_in{1.1,2,3} == 6)
  352. check(R.test_array_int_in{} == 0)
  353. check(not pcall(R.test_array_int_in, nil))
  354. check(not pcall(R.test_array_int_in, 'help'))
  355. check(not pcall(R.test_array_int_in, {'help'}))
  356. end
  357. function gireg.array_int_out()
  358. local R = lgi.Regress
  359. local a = R.test_array_int_out()
  360. check(#a == 5)
  361. check(a[1] == 0 and a[2] == 1 and a[3] == 2 and a[4] == 3 and a[5] == 4)
  362. check(#{R.test_array_int_out()} == 1)
  363. end
  364. function gireg.array_int_inout()
  365. local R = lgi.Regress
  366. local a = R.test_array_int_inout({1, 2, 3, 4, 5})
  367. check(#a == 4)
  368. check(a[1] == 3 and a[2] == 4 and a[3] == 5 and a[4] == 6)
  369. check(#{R.test_array_int_inout({1, 2, 3, 4, 5})} == 1)
  370. check(not pcall(R.test_array_int_inout, nil))
  371. check(not pcall(R.test_array_int_inout, 'help'))
  372. check(not pcall(R.test_array_int_inout, {'help'}))
  373. end
  374. function gireg.array_gint8_in()
  375. local R = lgi.Regress
  376. check(R.test_array_gint8_in{1,2,3} == 6)
  377. check(R.test_array_gint8_in{1.1,2,3} == 6)
  378. check(R.test_array_gint8_in('0123') == 48 + 49 + 50 + 51)
  379. check(R.test_array_gint8_in(bytes.new('0123')) == 48 + 49 + 50 + 51)
  380. check(R.test_array_gint8_in{} == 0)
  381. check(not pcall(R.test_array_gint8_in, nil))
  382. check(not pcall(R.test_array_gint8_in, {'help'}))
  383. end
  384. function gireg.array_gint16_in()
  385. local R = lgi.Regress
  386. check(R.test_array_gint16_in{1,2,3} == 6)
  387. check(R.test_array_gint16_in{1.1,2,3} == 6)
  388. check(R.test_array_gint16_in{} == 0)
  389. check(not pcall(R.test_array_gint16_in, nil))
  390. check(not pcall(R.test_array_gint16_in, 'help'))
  391. check(not pcall(R.test_array_gint16_in, {'help'}))
  392. end
  393. function gireg.array_gint32_in()
  394. local R = lgi.Regress
  395. check(R.test_array_gint32_in{1,2,3} == 6)
  396. check(R.test_array_gint32_in{1.1,2,3} == 6)
  397. check(R.test_array_gint32_in{} == 0)
  398. check(not pcall(R.test_array_gint32_in, nil))
  399. check(not pcall(R.test_array_gint32_in, 'help'))
  400. check(not pcall(R.test_array_gint32_in, {'help'}))
  401. end
  402. function gireg.array_gint64_in()
  403. local R = lgi.Regress
  404. check(R.test_array_gint64_in{1,2,3} == 6)
  405. check(R.test_array_gint64_in{1.1,2,3} == 6)
  406. check(R.test_array_gint64_in{} == 0)
  407. check(not pcall(R.test_array_gint64_in, nil))
  408. check(not pcall(R.test_array_gint64_in, 'help'))
  409. check(not pcall(R.test_array_gint64_in, {'help'}))
  410. end
  411. function gireg.array_strv_in()
  412. local R = lgi.Regress
  413. check(R.test_strv_in{'1', '2', '3'})
  414. check(not pcall(R.test_strv_in))
  415. check(not pcall(R.test_strv_in, '1'))
  416. check(not pcall(R.test_strv_in, 1))
  417. check(not R.test_strv_in{'3', '2', '1'})
  418. check(not R.test_strv_in{'1', '2', '3', '4'})
  419. end
  420. function gireg.array_gtype_in()
  421. local R = lgi.Regress
  422. local GObject = lgi.GObject
  423. local str = R.test_array_gtype_in {
  424. lgi.GObject.Value._gtype,
  425. lgi.GObject.type_from_name('gchar')
  426. }
  427. check(str == '[GValue,gchar,]')
  428. check(R.test_array_gtype_in({}) == '[]')
  429. check(not pcall(R.test_array_gtype_in))
  430. check(not pcall(R.test_array_gtype_in, ''))
  431. check(not pcall(R.test_array_gtype_in, 1))
  432. check(not pcall(R.test_array_gtype_in, function() end))
  433. end
  434. function gireg.array_strv_out()
  435. local R = lgi.Regress
  436. local a = R.test_strv_out()
  437. check(type(a) == 'table' and #a == 5)
  438. check(table.concat(a, ' ') == 'thanks for all the fish')
  439. check(#{R.test_strv_out()} == 1)
  440. end
  441. function gireg.array_strv_out_container()
  442. local R = lgi.Regress
  443. local a = R.test_strv_out_container()
  444. check(type(a) == 'table' and #a == 3)
  445. check(table.concat(a, ' ') == '1 2 3')
  446. end
  447. function gireg.array_strv_outarg()
  448. local R = lgi.Regress
  449. local a = R.test_strv_outarg()
  450. check(type(a) == 'table' and #a == 3)
  451. check(table.concat(a, ' ') == '1 2 3')
  452. check(#{R.test_strv_outarg()} == 1)
  453. end
  454. function gireg.array_fixed_size_int_out()
  455. local R = lgi.Regress
  456. local a = R.test_array_fixed_size_int_out()
  457. check(type(a) == 'table' and #a == 5)
  458. check(a[1] == 0 and a[2] == 1 and a[3] == 2 and a[4] == 3 and a[5] == 4)
  459. check(#{R.test_array_fixed_size_int_out()} == 1)
  460. end
  461. function gireg.array_fixed_size_int_return()
  462. local R = lgi.Regress
  463. local a = R.test_array_fixed_size_int_return()
  464. check(type(a) == 'table' and #a == 5)
  465. check(a[1] == 0 and a[2] == 1 and a[3] == 2 and a[4] == 3 and a[5] == 4)
  466. check(#{R.test_array_fixed_size_int_return()} == 1)
  467. end
  468. function gireg.array_strv_out_c()
  469. local R = lgi.Regress
  470. local a = R.test_strv_out_c()
  471. check(type(a) == 'table' and #a == 5)
  472. check(table.concat(a, ' ') == 'thanks for all the fish')
  473. end
  474. function gireg.array_int_full_out()
  475. local R = lgi.Regress
  476. local a = R.test_array_int_full_out()
  477. check(type(a) == 'table' and #a == 5)
  478. check(a[1] == 0 and a[2] == 1 and a[3] == 2 and a[4] == 3 and a[5] == 4)
  479. check(#{R.test_array_int_full_out()} == 1)
  480. end
  481. function gireg.array_int_full_out()
  482. local R = lgi.Regress
  483. local a = R.test_array_int_full_out()
  484. check(type(a) == 'table' and #a == 5)
  485. check(a[1] == 0 and a[2] == 1 and a[3] == 2 and a[4] == 3 and a[5] == 4)
  486. check(#{R.test_array_int_full_out()} == 1)
  487. end
  488. function gireg.array_int_null_in()
  489. local R = lgi.Regress
  490. R.test_array_int_null_in()
  491. R.test_array_int_null_in(nil)
  492. end
  493. function gireg.array_int_null_out()
  494. local R = lgi.Regress
  495. local a = R.test_array_int_null_out()
  496. check(type(a) == 'table' and not next(a))
  497. end
  498. function gireg.glist_nothing_return()
  499. local R = lgi.Regress
  500. check(select('#', R.test_glist_nothing_return()) == 1)
  501. a = R.test_glist_nothing_return()
  502. check(type(a) == 'table' and #a == 3)
  503. check(a[1] == '1' and a[2] == '2' and a[3] == '3')
  504. end
  505. function gireg.glist_nothing_return2()
  506. local R = lgi.Regress
  507. check(select('#', R.test_glist_nothing_return2()) == 1)
  508. a = R.test_glist_nothing_return2()
  509. check(type(a) == 'table' and #a == 3)
  510. check(a[1] == '1' and a[2] == '2' and a[3] == '3')
  511. end
  512. function gireg.glist_container_return()
  513. local R = lgi.Regress
  514. check(select('#', R.test_glist_container_return()) == 1)
  515. a = R.test_glist_container_return()
  516. check(type(a) == 'table' and #a == 3)
  517. check(a[1] == '1' and a[2] == '2' and a[3] == '3')
  518. end
  519. function gireg.glist_everything_return()
  520. local R = lgi.Regress
  521. check(select('#', R.test_glist_everything_return()) == 1)
  522. a = R.test_glist_everything_return()
  523. check(type(a) == 'table' and #a == 3)
  524. check(a[1] == '1' and a[2] == '2' and a[3] == '3')
  525. end
  526. function gireg.glist_nothing_in()
  527. local R = lgi.Regress
  528. R.test_glist_nothing_in {'1', '2', '3'}
  529. end
  530. function gireg.glist_nothing_in2()
  531. local R = lgi.Regress
  532. R.test_glist_nothing_in2 {'1', '2', '3'}
  533. end
  534. function gireg.glist_null_in()
  535. local R = lgi.Regress
  536. R.test_glist_null_in {}
  537. R.test_glist_null_in(nil)
  538. R.test_glist_null_in()
  539. end
  540. function gireg.glist_null_out()
  541. local R = lgi.Regress
  542. check(select('#', R.test_glist_null_out()) == 1)
  543. local a = R.test_glist_null_out()
  544. check(type(a) == 'table' and #a == 0)
  545. end
  546. function gireg.gslist_nothing_return()
  547. local R = lgi.Regress
  548. check(select('#', R.test_gslist_nothing_return()) == 1)
  549. a = R.test_gslist_nothing_return()
  550. check(type(a) == 'table' and #a == 3)
  551. check(a[1] == '1' and a[2] == '2' and a[3] == '3')
  552. end
  553. function gireg.gslist_nothing_return2()
  554. local R = lgi.Regress
  555. check(select('#', R.test_gslist_nothing_return2()) == 1)
  556. a = R.test_gslist_nothing_return2()
  557. check(type(a) == 'table' and #a == 3)
  558. check(a[1] == '1' and a[2] == '2' and a[3] == '3')
  559. end
  560. function gireg.gslist_container_return()
  561. local R = lgi.Regress
  562. check(select('#', R.test_gslist_container_return()) == 1)
  563. a = R.test_gslist_container_return()
  564. check(type(a) == 'table' and #a == 3)
  565. check(a[1] == '1' and a[2] == '2' and a[3] == '3')
  566. end
  567. function gireg.gslist_everything_return()
  568. local R = lgi.Regress
  569. check(select('#', R.test_gslist_everything_return()) == 1)
  570. a = R.test_gslist_everything_return()
  571. check(type(a) == 'table' and #a == 3)
  572. check(a[1] == '1' and a[2] == '2' and a[3] == '3')
  573. end
  574. function gireg.gslist_nothing_in()
  575. local R = lgi.Regress
  576. R.test_gslist_nothing_in {'1', '2', '3'}
  577. end
  578. function gireg.gslist_nothing_in2()
  579. local R = lgi.Regress
  580. R.test_gslist_nothing_in2 {'1', '2', '3'}
  581. end
  582. function gireg.gslist_null_in()
  583. local R = lgi.Regress
  584. R.test_gslist_null_in {}
  585. R.test_gslist_null_in(nil)
  586. R.test_gslist_null_in()
  587. end
  588. function gireg.gslist_null_out()
  589. local R = lgi.Regress
  590. check(select('#', R.test_gslist_null_out()) == 1)
  591. local a = R.test_gslist_null_out()
  592. check(type(a) == 'table' and #a == 0)
  593. end
  594. function gireg.ghash_null_return()
  595. local R = lgi.Regress
  596. check(select('#', R.test_ghash_null_return()) == 1)
  597. check(R.test_ghash_null_return() == nil)
  598. end
  599. local function size_htab(h)
  600. local size = 0
  601. for _ in pairs(h) do size = size + 1 end
  602. return size
  603. end
  604. function gireg.ghash_nothing_return()
  605. local R = lgi.Regress
  606. local count = 0
  607. check(select('#', R.test_ghash_nothing_return()) == 1)
  608. local h = R.test_ghash_nothing_return()
  609. check(type(h) == 'table')
  610. check(size_htab(h) == 3)
  611. check(h.foo == 'bar' and h.baz == 'bat' and h.qux == 'quux')
  612. end
  613. function gireg.ghash_container_return()
  614. local R = lgi.Regress
  615. local count = 0
  616. check(select('#', R.test_ghash_container_return()) == 1)
  617. local h = R.test_ghash_container_return()
  618. check(type(h) == 'table')
  619. check(size_htab(h) == 3)
  620. check(h.foo == 'bar' and h.baz == 'bat' and h.qux == 'quux')
  621. end
  622. function gireg.ghash_everything_return()
  623. local R = lgi.Regress
  624. local count = 0
  625. check(select('#', R.test_ghash_everything_return()) == 1)
  626. local h = R.test_ghash_everything_return()
  627. check(type(h) == 'table')
  628. check(size_htab(h) == 3)
  629. check(h.foo == 'bar' and h.baz == 'bat' and h.qux == 'quux')
  630. end
  631. function gireg.ghash_null_in()
  632. local R = lgi.Regress
  633. R.test_ghash_null_in(nil)
  634. R.test_ghash_null_in()
  635. check(not pcall(R.test_ghash_null_in,1))
  636. check(not pcall(R.test_ghash_null_in,'string'))
  637. check(not pcall(R.test_ghash_null_in,function() end))
  638. end
  639. function gireg.ghash_null_out()
  640. local R = lgi.Regress
  641. check(R.test_ghash_null_out() == nil)
  642. end
  643. function gireg.ghash_nothing_in()
  644. local R = lgi.Regress
  645. R.test_ghash_nothing_in({ foo = 'bar', baz = 'bat', qux = 'quux' })
  646. check(not pcall(R.test_ghash_nothing_in))
  647. check(not pcall(R.test_ghash_nothing_in, 1))
  648. check(not pcall(R.test_ghash_nothing_in, 'test'))
  649. check(not pcall(R.test_ghash_nothing_in, function() end))
  650. end
  651. function gireg.ghash_nested_everything_return()
  652. local R = lgi.Regress
  653. check(select('#', R.test_ghash_nested_everything_return) == 1);
  654. local a = R.test_ghash_nested_everything_return()
  655. check(type(a) == 'table')
  656. check(size_htab(a) == 1)
  657. check(type(a.wibble) == 'table')
  658. check(size_htab(a.wibble) == 3)
  659. check(a.wibble.foo == 'bar' and a.wibble.baz == 'bat'
  660. and a.wibble.qux == 'quux')
  661. end
  662. function gireg.enum()
  663. local R = lgi.Regress
  664. check(R.TestEnum.VALUE1 == 0)
  665. check(R.TestEnum.VALUE2 == 1)
  666. check(R.TestEnum.VALUE3 == -1)
  667. check(R.TestEnum[0] == 'VALUE1')
  668. check(R.TestEnum[1] == 'VALUE2')
  669. check(R.TestEnum[-1] == 'VALUE3')
  670. check(R.TestEnum[43] == 43)
  671. check(R.test_enum_param(0) == 'value1')
  672. check(R.test_enum_param(1) == 'value2')
  673. check(R.test_enum_param(-1) == 'value3')
  674. check(R.TestEnumUnsigned.VALUE1 == 1)
  675. check(R.TestEnumUnsigned.VALUE2 == 0x80000000)
  676. check(R.TestEnumUnsigned[1] == 'VALUE1')
  677. check(R.TestEnumUnsigned[0x80000000] == 'VALUE2')
  678. check(R.TestEnumUnsigned[-1] == -1)
  679. end
  680. function gireg.flags()
  681. local R = lgi.Regress
  682. check(R.TestFlags.FLAG1 == 1)
  683. check(R.TestFlags.FLAG2 == 2)
  684. check(R.TestFlags.FLAG3 == 4)
  685. check(R.TestFlags[7].FLAG1 == true)
  686. check(R.TestFlags[7].FLAG2 == true)
  687. check(R.TestFlags[7].FLAG3 == true)
  688. check(R.TestFlags[3].FLAG1 == true)
  689. check(R.TestFlags[3].FLAG2 == true)
  690. check(R.TestFlags[3].FLAG3 == nil)
  691. check(R.TestFlags[10].FLAG2 == true)
  692. check(R.TestFlags[10][1] == 8)
  693. checkv(R.TestFlags { 'FLAG1', 'FLAG2' }, 3, 'number')
  694. checkv(R.TestFlags { 1, 2, 'FLAG1', R.TestFlags.FLAG2 }, 3, 'number')
  695. checkv(R.TestFlags { 10, FLAG2 = 2 }, 10, 'number')
  696. checkv(R.TestFlags { 2, 'FLAG2' }, 2, 'number')
  697. end
  698. function gireg.flags_out()
  699. local R = lgi.Regress
  700. local out = R.global_get_flags_out()
  701. check(type(out) == 'table')
  702. check(out.FLAG1 == true)
  703. check(out.FLAG2 == nil)
  704. check(out.FLAG3 == true)
  705. check(#out == 0)
  706. end
  707. function gireg.const()
  708. local R = lgi.Regress
  709. checkv(R.INT_CONSTANT, 4422, 'number')
  710. checkv(R.DOUBLE_CONSTANT, 44.22, 'number')
  711. checkv(R.STRING_CONSTANT, 'Some String', 'string')
  712. checkv(R.Mixed_Case_Constant, 4423, 'number')
  713. end
  714. function gireg.struct_a()
  715. local R = lgi.Regress
  716. check(select('#', R.TestStructA()) == 1)
  717. local a = R.TestStructA()
  718. check(type(a) == 'userdata')
  719. a.some_int = 42
  720. check(a.some_int == 42)
  721. a.some_int8 = 12
  722. check(a.some_int8 == 12)
  723. a.some_double = 3.14
  724. check(a.some_double == 3.14)
  725. a.some_enum = R.TestEnum.VALUE2
  726. check(a.some_enum == 'VALUE2')
  727. a = R.TestStructA { some_int = 42, some_int8 = 12,
  728. some_double = 3.14, some_enum = 'VALUE2' }
  729. a.some_int = 43
  730. a.some_int8 = 13
  731. check(a.some_int == 43)
  732. check(a.some_int8 == 13)
  733. check(a.some_double == 3.14)
  734. check(a.some_enum == 'VALUE2')
  735. a.some_double = 3.15
  736. check(a.some_int == 43)
  737. check(a.some_int8 == 13)
  738. check(a.some_double == 3.15)
  739. check(a.some_enum == 'VALUE2')
  740. a.some_enum = R.TestEnum.VALUE3
  741. check(a.some_int == 43)
  742. check(a.some_int8 == 13)
  743. check(a.some_double == 3.15)
  744. check(a.some_enum == 'VALUE3')
  745. check(not pcall(function() return a.foo end))
  746. check(not pcall(function() a.foo = 1 end))
  747. check(select('#', (function() a.some_int = 0 end)()) == 0)
  748. check(select('#', (function() return a.some_int end)()) == 1)
  749. check(select('#', (function() local b = a.some_int end)()) == 0)
  750. end
  751. function gireg.struct_a_clone()
  752. local R = lgi.Regress
  753. local a = R.TestStructA { some_int = 42, some_int8 = 12, some_double = 3.14,
  754. some_enum = R.TestEnum.VALUE2 }
  755. check(a == a)
  756. check(select('#', a:clone()) == 1)
  757. local b = a:clone()
  758. check(type(b) == 'userdata')
  759. check(b ~= a)
  760. check(b == b)
  761. check(b.some_int == 42)
  762. check(b.some_int8 == 12)
  763. check(b.some_double == 3.14)
  764. check(b.some_enum == 'VALUE2')
  765. check(a.some_int == 42)
  766. check(a.some_int8 == 12)
  767. check(a.some_double == 3.14)
  768. check(a.some_enum == 'VALUE2')
  769. end
  770. function gireg.struct_b()
  771. local R = lgi.Regress
  772. local b = R.TestStructB()
  773. -- Basic fields assignments.
  774. b.some_int8 = 13
  775. check(b.some_int8 == 13)
  776. b.nested_a.some_int = -1
  777. check(b.some_int8 == 13)
  778. check(b.nested_a.some_int == -1)
  779. b.nested_a.some_int8 = -2
  780. check(b.some_int8 == 13)
  781. check(b.nested_a.some_int == -1)
  782. check(b.nested_a.some_int8 == -2)
  783. -- Whole nested structure assignment.
  784. b.nested_a = { some_int = 42, some_int8 = 12,
  785. some_double = 3.14, some_enum = R.TestEnum.VALUE2 }
  786. check(b.nested_a.some_int == 42)
  787. check(b.nested_a.some_int8 == 12)
  788. check(b.nested_a.some_double == 3.14)
  789. check(b.nested_a.some_enum == 'VALUE2')
  790. -- Nested structure construction.
  791. b = R.TestStructB { some_int8 = 21, nested_a =
  792. { some_int = 42, some_int8 = 12,
  793. some_double = 3.14, some_enum = R.TestEnum.VALUE2 } }
  794. check(b.some_int8 == 21)
  795. check(b.nested_a.some_int == 42)
  796. check(b.nested_a.some_int8 == 12)
  797. check(b.nested_a.some_double == 3.14)
  798. check(b.nested_a.some_enum == 'VALUE2')
  799. end
  800. function gireg.struct_b_clone()
  801. local R = lgi.Regress
  802. local b = R.TestStructB { some_int8 = 21, nested_a =
  803. { some_int = 42, some_int8 = 12,
  804. some_double = 3.14,
  805. some_enum = R.TestEnum.VALUE2 } }
  806. check(b == b)
  807. check(select('#', b:clone()) == 1)
  808. local bc = b:clone()
  809. check(type(bc) == 'userdata')
  810. check(bc ~= b)
  811. check(bc == bc)
  812. check(bc.some_int8 == 21)
  813. check(bc.nested_a.some_int == 42)
  814. check(bc.nested_a.some_int8 == 12)
  815. check(bc.nested_a.some_double == 3.14)
  816. check(bc.nested_a.some_enum == 'VALUE2')
  817. check(bc.nested_a.some_int == 42)
  818. check(bc.nested_a.some_int8 == 12)
  819. check(bc.nested_a.some_double == 3.14)
  820. check(bc.nested_a.some_enum == 'VALUE2')
  821. check(b.some_int8 == 21)
  822. check(b.nested_a.some_int == 42)
  823. check(b.nested_a.some_int8 == 12)
  824. check(b.nested_a.some_double == 3.14)
  825. check(b.nested_a.some_enum == 'VALUE2')
  826. check(b.nested_a.some_int == 42)
  827. check(b.nested_a.some_int8 == 12)
  828. check(b.nested_a.some_double == 3.14)
  829. check(b.nested_a.some_enum == 'VALUE2')
  830. end
  831. function gireg.boxed_a_equals()
  832. local R = lgi.Regress
  833. check(R.TestSimpleBoxedA({ some_int = 1, some_int8 = 2,
  834. some_double = 3.14 }):equals(
  835. R.TestSimpleBoxedA({ some_int = 1, some_int8 = 2,
  836. some_double = 3.14 })))
  837. check(not R.TestSimpleBoxedA({ some_int = 2, some_int8 = 2,
  838. some_double = 3.14 }):equals(
  839. R.TestSimpleBoxedA({ some_int = 1, some_int8 = 2,
  840. some_double = 3.14 })))
  841. check(R.TestSimpleBoxedA():equals(R.TestSimpleBoxedA()))
  842. check(not pcall(R.TestSimpleBoxedA().equals))
  843. check(not pcall(R.TestSimpleBoxedA().equals, nil))
  844. check(not pcall(R.TestSimpleBoxedA().equals, {}))
  845. check(not pcall(R.TestSimpleBoxedA().equals, 1))
  846. check(not pcall(R.TestSimpleBoxedA().equals, 'string'))
  847. check(not pcall(R.TestSimpleBoxedA().equals, function() end))
  848. end
  849. function gireg.boxed_a_const_return()
  850. local R = lgi.Regress
  851. check(select('#', R.test_simple_boxed_a_const_return()) == 1)
  852. local a = R.test_simple_boxed_a_const_return()
  853. check(a.some_int == 5)
  854. check(a.some_int8 == 6)
  855. check(a.some_double == 7)
  856. end
  857. function gireg.boxed_new()
  858. local R = lgi.Regress
  859. check(select('#', R.TestBoxed.new()) == 1)
  860. local bn = R.TestBoxed.new()
  861. local bac1 = R.TestBoxed.new_alternative_constructor1(1)
  862. check(bac1.some_int8 == 1)
  863. local bac2 = R.TestBoxed.new_alternative_constructor2(1, 2)
  864. check(bac2.some_int8 == 3)
  865. local bac3 = R.TestBoxed.new_alternative_constructor3('25')
  866. check(bac3.some_int8 == 25)
  867. end
  868. function gireg.boxed_copy()
  869. local R = lgi.Regress
  870. local b = R.TestBoxed.new()
  871. b.some_int8 = 1
  872. b.nested_a = { some_int = 1, some_int8 = 2, some_double = 3.14 }
  873. check(select('#', b:copy()) == 1)
  874. local bc = b:copy()
  875. check(bc ~= b)
  876. check(bc.some_int8 == 1)
  877. check(bc.nested_a.some_int == 1)
  878. check(bc.nested_a.some_int8 == 2)
  879. check(bc.nested_a.some_double == 3.14)
  880. end
  881. function gireg.boxed_equals()
  882. local R = lgi.Regress
  883. local b1 = R.TestBoxed.new()
  884. b1.some_int8 = 1
  885. b1.nested_a = { some_int = 1, some_int8 = 2, some_double = 3.14 }
  886. local b2 = R.TestBoxed.new()
  887. b2.some_int8 = 1
  888. b2.nested_a = { some_int = 1, some_int8 = 2, some_double = 3.14 }
  889. check(b1:equals(b2))
  890. b1.some_int8 = 2
  891. check(not b1:equals(b2))
  892. b1.some_int8 = 1
  893. b1.nested_a.some_int = 2
  894. check(not b1:equals(b2))
  895. b1.nested_a.some_int = 1
  896. check(b1:equals(b2))
  897. end
  898. function gireg.closure_simple()
  899. local R = lgi.Regress
  900. local GObject = lgi.GObject
  901. local closure = GObject.Closure(function(...)
  902. check(select('#', ...) == 0)
  903. return 42
  904. end)
  905. checkv(R.test_closure(closure, 42), 42, 'number')
  906. local res = GObject.Value('gint')
  907. closure:invoke(res, {}, nil)
  908. check(res.gtype == 'gint' and res.value == 42)
  909. end
  910. function gireg.closure_arg()
  911. local GObject = lgi.GObject
  912. local R = lgi.Regress
  913. local closure = GObject.Closure(function(int, ...)
  914. check(select('#', ...) == 0)
  915. return int
  916. end)
  917. checkv(R.test_closure_one_arg(closure, 43), 43, 'number')
  918. local res = GObject.Value('gint')
  919. closure:invoke(res, { GObject.Value('gint', 43) }, nil)
  920. check(res.gtype == 'gint' and res.value == 43)
  921. end
  922. function gireg.gvalue_assign()
  923. local GObject = lgi.GObject
  924. local V = GObject.Value
  925. local v = V()
  926. check(v.gtype == nil)
  927. check(v.value == nil)
  928. v.gtype = 'gchararray'
  929. check(v.gtype == 'gchararray')
  930. check(v.value == nil)
  931. v.value = 'label'
  932. check(v.value == 'label')
  933. v.value = nil
  934. check(v.value == nil)
  935. check(v.gtype == 'gchararray')
  936. v.value = 'label'
  937. v.gtype = nil
  938. check(v.gtype == nil)
  939. check(v.value == nil)
  940. v.gtype = 'gint'
  941. v.value = 1
  942. check(v.gtype == 'gint')
  943. check(v.value == 1)
  944. v.gtype = 'gdouble'
  945. check(v.gtype == 'gdouble')
  946. check(v.value == 1)
  947. v.value = 3.14
  948. v.gtype = 'gint'
  949. check(v.gtype == 'gint')
  950. check(v.value == 3)
  951. end
  952. function gireg.gvalue_arg()
  953. local GObject = lgi.GObject
  954. local R = lgi.Regress
  955. checkv(R.test_int_value_arg(GObject.Value('gint', 42)), 42, 'number')
  956. end
  957. function gireg.gvalue_return()
  958. local R = lgi.Regress
  959. local v = R.test_value_return(43)
  960. checkv(v.value, 43, 'number')
  961. check(v.gtype == 'gint', 'incorrect value type')
  962. end
  963. function gireg.gvalue_date()
  964. local GObject = lgi.GObject
  965. local GLib = lgi.GLib
  966. local R = lgi.Regress
  967. local v
  968. v = R.test_date_in_gvalue()
  969. check(v.gtype == 'GDate')
  970. check(v.value:get_day() == 5)
  971. check(v.value:get_month() == 'DECEMBER')
  972. check(v.value:get_year() == 1984)
  973. local d = GLib.Date()
  974. d:set_dmy(25, 1, 1975)
  975. v = GObject.Value(GLib.Date, d)
  976. check(v.gtype == 'GDate')
  977. check(v.value:get_day() == 25)
  978. check(v.value:get_month() == 'JANUARY')
  979. check(v.value:get_year() == 1975)
  980. end
  981. function gireg.gvalue_strv()
  982. local GObject = lgi.GObject
  983. local R = lgi.Regress
  984. local v = R.test_strv_in_gvalue()
  985. check(v.gtype == 'GStrv')
  986. check(#v.value == 3)
  987. check(v.value[1] == 'one')
  988. check(v.value[2] == 'two')
  989. check(v.value[3] == 'three')
  990. v = GObject.Value('GStrv', { '1', '2', '3' })
  991. check(#v.value == 3)
  992. check(v.value[1] == '1')
  993. check(v.value[2] == '2')
  994. check(v.value[3] == '3')
  995. end
  996. function gireg.obj_create()
  997. local R = lgi.Regress
  998. local o = R.TestObj()
  999. check(o)
  1000. check(type(o) == 'userdata')
  1001. check(select('#', R.TestObj()) == 1)
  1002. o = R.TestObj.new_from_file('unused')
  1003. check(type(o) == 'userdata')
  1004. check(select('#', R.TestObj.new_from_file('unused')) == 1)
  1005. end
  1006. function gireg.obj_methods()
  1007. local R = lgi.Regress
  1008. local GLib = lgi.GLib
  1009. local Gio = lgi.Gio
  1010. if R.TestObj._method.do_matrix then
  1011. R.TestObj._method.invoke_matrix = R.TestObj._method.do_matrix
  1012. R.TestObj._method.do_matrix = nil
  1013. end
  1014. local o = R.TestObj()
  1015. check(o:instance_method() == -1)
  1016. check(o.static_method(42) == 42)
  1017. local y, z, q = o:torture_signature_0(1, 'foo', 2)
  1018. check(y == 1)
  1019. check(z == 2)
  1020. check(q == 5)
  1021. local y, z, q = o:torture_signature_1(1, 'foo', 2)
  1022. check(y == 1)
  1023. check(z == 2)
  1024. check(q == 5)
  1025. local res, err = o:torture_signature_1(1, 'foo', 3)
  1026. check(not res)
  1027. check(GLib.Error:is_type_of(err))
  1028. check(err:matches(Gio.IOErrorEnum, 'FAILED'))
  1029. check(o:invoke_matrix('unused') == 42)
  1030. end
  1031. function gireg.obj_null_args()
  1032. local R = lgi.Regress
  1033. R.func_obj_null_in(nil)
  1034. R.func_obj_null_in()
  1035. check(R.TestObj.null_out() == nil)
  1036. check(select('#', R.TestObj.null_out()) == 1)
  1037. end
  1038. function gireg.obj_virtual_methods()
  1039. local R = lgi.Regress
  1040. local o = R.TestObj()
  1041. check(o:do_matrix('unused') == 42)
  1042. end
  1043. function gireg.obj_prop_int()
  1044. local R = lgi.Regress
  1045. local o = R.TestObj()
  1046. check(o.int == 0)
  1047. o.int = 42
  1048. check(o.int == 42)
  1049. check(not pcall(function() o.int = {} end))
  1050. check(not pcall(function() o.int = 'lgi' end))
  1051. check(not pcall(function() o.int = nil end))
  1052. check(not pcall(function() o.int = function() end end))
  1053. end
  1054. function gireg.obj_prop_float()
  1055. local R = lgi.Regress
  1056. local o = R.TestObj()
  1057. check(o.float == 0)
  1058. o.float = 42.1
  1059. checkvf(o.float, 42.1, 0.00001)
  1060. check(not pcall(function() o.float = {} end))
  1061. check(not pcall(function() o.float = 'lgi' end))
  1062. check(not pcall(function() o.float = nil end))
  1063. check(not pcall(function() o.float = function() end end))
  1064. end
  1065. function gireg.obj_prop_double()
  1066. local R = lgi.Regress
  1067. local o = R.TestObj()
  1068. check(o.double == 0)
  1069. o.double = 42.1
  1070. checkvf(o.double, 42.1, 0.0000000001)
  1071. check(not pcall(function() o.double = {} end))
  1072. check(not pcall(function() o.double = 'lgi' end))
  1073. check(not pcall(function() o.double = nil end))
  1074. check(not pcall(function() o.double = function() end end))
  1075. end
  1076. function gireg.obj_prop_string()
  1077. local R = lgi.Regress
  1078. local o = R.TestObj()
  1079. check(o.string == nil)
  1080. o.string = 'lgi'
  1081. check(o.string == 'lgi')
  1082. o.string = nil
  1083. check(o.string == nil)
  1084. check(not pcall(function() o.string = {} end))
  1085. check(not pcall(function() o.string = function() end end))
  1086. end
  1087. function gireg.obj_prop_bare()
  1088. local R = lgi.Regress
  1089. local o = R.TestObj()
  1090. check(o.bare == nil)
  1091. local pv = R.TestObj()
  1092. o.bare = pv
  1093. check(o.bare == pv)
  1094. o.bare = nil
  1095. check(o.bare == nil)
  1096. o:set_bare(pv)
  1097. check(o.bare == pv)
  1098. o.bare = nil
  1099. check(o.bare == nil)
  1100. check(not pcall(function() o.bare = {} end))
  1101. check(not pcall(function() o.bare = 42 end))
  1102. check(not pcall(function() o.bare = 'lgi' end))
  1103. check(not pcall(function() o.bare = function() end end))
  1104. check(not pcall(function() o.bare = R.TestBoxed() end))
  1105. end
  1106. function gireg.obj_prop_boxed()
  1107. local R = lgi.Regress
  1108. local o = R.TestObj()
  1109. check(o.boxed == nil)
  1110. local pv = R.TestBoxed()
  1111. o.boxed = pv
  1112. check(o.boxed:equals(pv))
  1113. o.boxed = nil
  1114. check(o.boxed == nil)
  1115. check(not pcall(function() o.boxed = {} end))
  1116. check(not pcall(function() o.boxed = 42 end))
  1117. check(not pcall(function() o.boxed = 'lgi' end))
  1118. check(not pcall(function() o.boxed = function() end end))
  1119. check(not pcall(function() o.boxed = R.TestObj() end))
  1120. end
  1121. function gireg.obj_prop_hash()
  1122. local R = lgi.Regress
  1123. local o = R.TestObj()
  1124. check(o.hash_table == nil)
  1125. o.hash_table = { a = 1, b = 2 }
  1126. local ov = o.hash_table
  1127. check(ov.a == 1 and ov.b == 2)
  1128. check(not pcall(function() o.hash_table = 42 end))
  1129. check(not pcall(function() o.hash_table = 'lgi' end))
  1130. check(not pcall(function() o.hash_table = function() end end))
  1131. check(not pcall(function() o.hash_table = R.TestObj() end))
  1132. check(not pcall(function() o.hash_table = R.TestBoxed() end))
  1133. end
  1134. function gireg.obj_prop_list()
  1135. local R = lgi.Regress
  1136. local o = R.TestObj()
  1137. check(o.hash_table == nil)
  1138. o.list = { 'one', 'two', 'three', }
  1139. local ov = o.list
  1140. check(#ov == 3 and ov[1] == 'one' and ov[2] == 'two' and ov[3] == 'three')
  1141. check(not pcall(function() o.list = 42 end))
  1142. check(not pcall(function() o.list = 'lgi' end))
  1143. check(not pcall(function() o.list = function() end end))
  1144. check(not pcall(function() o.list = R.TestObj() end))
  1145. check(not pcall(function() o.list = R.TestBoxed() end))
  1146. end
  1147. function gireg.obj_prop_dynamic()
  1148. local R = lgi.Regress
  1149. local o = R.TestObj()
  1150. -- Remove static property information, force lgi to use dynamic
  1151. -- GLib property system.
  1152. local old_prop = R.TestObj.int
  1153. R.TestObj._property.int = nil
  1154. R.TestObj._cached = nil
  1155. check(R.TestObj.int == nil)
  1156. check(o.int == 0)
  1157. o.int = 3
  1158. check(o.int == 3)
  1159. check(not pcall(function() o.int = 'string' end))
  1160. check(not pcall(function() o.int = {} end))
  1161. check(not pcall(function() o.int = true end))
  1162. check(not pcall(function() o.int = function() end end))
  1163. -- Restore TestObj to work normally.
  1164. R.TestObj._property.int = old_prop
  1165. end
  1166. function gireg.obj_subobj()
  1167. local R = lgi.Regress
  1168. local o = R.TestSubObj()
  1169. local pv = R.TestObj()
  1170. check(o:instance_method() == 0)
  1171. o.bare = pv
  1172. check(o.bare == pv)
  1173. o:unset_bare()
  1174. check(o.bare == nil)
  1175. o = R.TestSubObj.new()
  1176. o:set_bare(pv)
  1177. check(o.bare == pv)
  1178. o:unset_bare()
  1179. check(o.bare == nil)
  1180. end
  1181. function gireg.obj_naming()
  1182. local R = lgi.Regress
  1183. local o = R.TestWi8021x()
  1184. o:set_testbool(true)
  1185. check(o.testbool == true)
  1186. o.testbool = false
  1187. check(o:get_testbool() == false)
  1188. end
  1189. function gireg.obj_floating()
  1190. local R = lgi.Regress
  1191. local o = R.TestFloating()
  1192. check(o)
  1193. o = nil
  1194. collectgarbage()
  1195. collectgarbage()
  1196. end
  1197. function gireg.obj_fundamental()
  1198. local R = lgi.Regress
  1199. local f = R.TestFundamentalSubObject.new('foo-nda-mental')
  1200. check(f)
  1201. check(f.data == 'foo-nda-mental')
  1202. local v = lgi.GObject.Value(R.TestFundamentalSubObject, f)
  1203. check(v.value == f)
  1204. f = nil
  1205. collectgarbage()
  1206. end
  1207. function gireg.callback_simple()
  1208. local R = lgi.Regress
  1209. check(R.test_callback(function() return 42 end) == 42)
  1210. check(R.test_callback() == 0)
  1211. check(R.test_callback(nil) == 0)
  1212. check(not pcall(R.test_callback, 1))
  1213. check(not pcall(R.test_callback, 'foo'))
  1214. check(not pcall(R.test_callback, {}))
  1215. check(not pcall(R.test_callback, R))
  1216. check(R.test_multi_callback(function() return 22 end) == 44)
  1217. check(R.test_multi_callback() == 0)
  1218. end
  1219. function gireg.callback_data()
  1220. local R = lgi.Regress
  1221. local called
  1222. R.test_simple_callback(function() called = true end)
  1223. check(called)
  1224. check(R.test_callback_user_data(function() return 42 end) == 42)
  1225. called = nil
  1226. R.TestObj.static_method_callback(function() called = true return 42 end)
  1227. check(called)
  1228. local o = R.TestObj()
  1229. called = nil
  1230. o.static_method_callback(function() called = true return 42 end)
  1231. check(called)
  1232. called = nil
  1233. o:instance_method_callback(function() called = true return 42 end)
  1234. check(called)
  1235. end
  1236. function gireg.callback_notified()
  1237. local R = lgi.Regress
  1238. check(R.test_callback_destroy_notify(function() return 1 end) == 1)
  1239. check(R.test_callback_destroy_notify(function() return 2 end) == 2)
  1240. check(R.test_callback_destroy_notify(function() return 3 end) == 3)
  1241. collectgarbage()
  1242. collectgarbage()
  1243. check(R.test_callback_thaw_notifications() == 6)
  1244. R.TestObj.new_callback(function() return 1 end)
  1245. collectgarbage()
  1246. collectgarbage()
  1247. check(R.test_callback_thaw_notifications() == 1)
  1248. end
  1249. function gireg.callback_async()
  1250. local R = lgi.Regress
  1251. R.test_callback_async(function() return 1 end)
  1252. collectgarbage()
  1253. collectgarbage()
  1254. check(R.test_callback_thaw_async() == 1)
  1255. end