aboutsummaryrefslogtreecommitdiffstats
path: root/conf.d/project/lua.d/aft.lua
blob: e522c4045631a83c1bc5402d6c166b3ab2ee5202 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
--[[
    Copyright (C) 2018 "IoT.bzh"
    Author Romain Forlot <romain.forlot@iot.bzh>

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.


    NOTE: strict mode: every global variables should be prefixed by '_'
--]]

package.path = package.path .. ';./var/?.lua'
local lu = require('luaunit')

local AFT = {
	context = _ctx
}

--[[
  Events listener and assertion function to test correctness of received
  event data.
]]

_events_cb = {}

function _evt_catcher_ (source, action, evt)
  -- local table_end = table_length(_events) + 1
  -- _events[event.event] = event.data
  if _events_cb[evt.event] ~= nil then
    _events_cb[evt.event](evt)
  end
end

function AFT.assertEvtReceived(event)
	lu.execOneFunction(nil,nil,nil, function()
		assertIsString(event, "Event parameter must be a string")
		assertTrue(true)
	end)
end

--[[
  Assert Wrapping function meant to tests API Verbs calls
]]

local function assertVerbCallParameters(src, api, verb, args)
	AFT.assertIsUserdata(src, "Source must be an opaque userdata pointer which will be passed to the binder")
	AFT.assertIsString(api, "API and Verb must be string")
	AFT.assertIsString(verb, "API and Verb must be string")
	AFT.assertIsTable(args, "Arguments must use LUA Table (event empty)")
end

function AFT.assertVerb(api, verb, args, cb)
	lu.LuaUnit:runSuiteByInstances({{"assertVerb", function()
		assertVerbCallParameters(AFT.context, api, verb, args)
		local err,responseJ = AFB:servsync(AFT.context, api, verb, args)
		lu.assertFalse(err)
		lu.assertStrContains(responseJ.request.status, "success", nil, nil, "Call for API/Verb failed.")

		if type(cb) == 'function' then
			cb(responseJ)
		elseif type(cb) == 'table' then
			assertTrue(table_eq(responseJ, cb))
		elseif not type(cb) == nil then
			assertTrue(false, "Wrong parameter passed to assertion. Last parameter should be function, table representing a JSON object or nil")
		end
	end}} )
end

function AFT.assertVerbError(api, verb, args, cb)
	lu.execOneFunction(nil,nil,nil, function()
		assertVerbCallParameters(AFT.context, api, verb, args)
		local err,responseJ = AFB:servsync(AFT.context, api, verb, args)
		lu.assertFalse(err)
		lu.assertNotStrContains(responseJ.request.status, "success", nil, nil, "Call for API/Verb succeed but it shouldn't.")

		if type(cb) == 'function' then
			cb(responseJ)
		elseif type(cb) == 'table' then
			assertFalse(table_eq(responseJ, cb))
		elseif not type(cb) == nil then
			assertFalse(true, "Wrong parameter passed to assertion. Last parameter should be function, table representing a JSON object or nil")
		end
	end)
end

--[[
	Make all assertions accessible using AFT and declare some convenients
	aliases.
]]

local luaunit_list_of_funcs = {
	--  official function name from luaunit test framework

	-- general assertions
	'assertEquals',
	'assertItemsEquals',
	'assertNotEquals',
	'assertAlmostEquals',
	'assertNotAlmostEquals',
	'assertEvalToTrue',
	'assertEvalToFalse',
	'assertStrContains',
	'assertStrIContains',
	'assertNotStrContains',
	'assertNotStrIContains',
	'assertStrMatches',
	'assertError',
	'assertErrorMsgEquals',
	'assertErrorMsgContains',
	'assertErrorMsgMatches',
	'assertIs',
	'assertNotIs',
	'wrapFunctions',
	'wrapFunctions',

	-- type assertions: assertIsXXX -> assert_is_xxx
	'assertIsNumber',
	'assertIsString',
	'assertIsTable',
	'assertIsBoolean',
	'assertIsNil',
	'assertIsTrue',
	'assertIsFalse',
	'assertIsNaN',
	'assertIsInf',
	'assertIsPlusInf',
	'assertIsMinusInf',
	'assertIsPlusZero',
	'assertIsMinusZero',
	'assertIsFunction',
	'assertIsThread',
	'assertIsUserdata',

	-- type assertions: assertIsXXX -> assertXxx
	'assertIsNumber',
	'assertIsString',
	'assertIsTable',
	'assertIsBoolean',
	'assertIsNil',
	'assertIsTrue',
	'assertIsFalse',
	'assertIsNaN',
	'assertIsInf',
	'assertIsPlusInf',
	'assertIsMinusInf',
	'assertIsPlusZero',
	'assertIsMinusZero',
	'assertIsFunction',
	'assertIsThread',
	'assertIsUserdata',

	-- type assertions: assertIsXXX -> assert_xxx (luaunit v2 compat)
	'assertIsNumber',
	'assertIsString',
	'assertIsTable',
	'assertIsBoolean',
	'assertIsNil',
	'assertIsTrue',
	'assertIsFalse',
	'assertIsNaN',
	'assertIsInf',
	'assertIsPlusInf',
	'assertIsMinusInf',
	'assertIsPlusZero',
	'assertIsMinusZero',
	'assertIsFunction',
	'assertIsThread',
	'assertIsUserdata',

	-- type assertions: assertNotIsXXX -> assert_not_is_xxx
	'assertNotIsNumber',
	'assertNotIsString',
	'assertNotIsTable',
	'assertNotIsBoolean',
	'assertNotIsNil',
	'assertNotIsTrue',
	'assertNotIsFalse',
	'assertNotIsNaN',
	'assertNotIsInf',
	'assertNotIsPlusInf',
	'assertNotIsMinusInf',
	'assertNotIsPlusZero',
	'assertNotIsMinusZero',
	'assertNotIsFunction',
	'assertNotIsThread',
	'assertNotIsUserdata',

	-- type assertions: assertNotIsXXX -> assertNotXxx (luaunit v2 compat)
	'assertNotIsNumber',
	'assertNotIsString',
	'assertNotIsTable',
	'assertNotIsBoolean',
	'assertNotIsNil',
	'assertNotIsTrue',
	'assertNotIsFalse',
	'assertNotIsNaN',
	'assertNotIsInf',
	'assertNotIsPlusInf',
	'assertNotIsMinusInf',
	'assertNotIsPlusZero',
	'assertNotIsMinusZero',
	'assertNotIsFunction',
	'assertNotIsThread',
	'assertNotIsUserdata',

	-- type assertions: assertNotIsXXX -> assert_not_xxx
	'assertNotIsNumber',
	'assertNotIsString',
	'assertNotIsTable',
	'assertNotIsBoolean',
	'assertNotIsNil',
	'assertNotIsTrue',
	'assertNotIsFalse',
	'assertNotIsNaN',
	'assertNotIsInf',
	'assertNotIsPlusInf',
	'assertNotIsMinusInf',
	'assertNotIsPlusZero',
	'assertNotIsMinusZero',
	'assertNotIsFunction',
	'assertNotIsThread',
	'assertNotIsUserdata',

	-- all assertions with Coroutine duplicate Thread assertions
	'assertIsThread',
	'assertIsThread',
	'assertIsThread',
	'assertIsThread',
	'assertNotIsThread',
	'assertNotIsThread',
	'assertNotIsThread',
	'assertNotIsThread',
}

local aft_list_of_funcs = {
	-- AF Binder generic assertions
	{ 'assertVerb',      'assertVerbStatusSuccess' },
	{ 'assertVerb',      'assertVerbResponseEquals' },
	{ 'assertVerb',      'assertVerbResponseContains' },
	{ 'assertVerb',      'assertVerbCb' },
	{ 'assertVerbError', 'assertVerbStatusError' },
	{ 'assertVerbError', 'assertVerbResponseEqualsError' },
	{ 'assertVerbError', 'assertVerbResponseContainsError' },
	{ 'assertVerbError', 'assertVerbCbError' },
}

-- Create all aliases in M
for _, v in pairs( luaunit_list_of_funcs ) do
	local funcname = v
	AFT[funcname] = lu[funcname]
end

-- Create all aliases in M
for _, v in pairs( aft_list_of_funcs ) do
	local funcname, alias = v[1], v[2]
	AFT[alias] = AFT[funcname]
end

function _launch_test(context, args)
	_ctx = context
	for _,f in pairs(args.files) do
		print('******** Do file: var/'.. f)
		dofile('var/'..f)
	end
end

return AFT