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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
--
-- ASSERT.LUA Copyright (c) 2006-07, <akauppi@gmail.com>
--
-- Converting the Lua 'assert' function into a namespace table (without
-- breaking compatibility with the basic 'assert()' calling).
--
-- This module allows shorthand use s.a. 'assert.table()' for asserting
-- variable types, and is also being used by Lua-super constraints system
-- for testing function argument & return types.
--
-- All in all, a worthy module and could be part of Lua future versions.
--
-- Note: the 'assert' table is available for your own assertions, too. Just add
-- more functions s.a. 'assert.myobj()' to check for custom invariants.
-- They will then be available for the constraints check, too.
--
-- Author: <akauppi@gmail.com>
--
--[[
/******************************************************************************
* Lua 5.1.1 support and extension functions (assert.lua)
*
* Copyright (C) 2006-07, Asko Kauppi.
*
* NOTE: This license concerns only the particular source file; not necessarily
* the project with which it has been delivered (the project may have a more
* restrictive license, s.a. [L]GPL).
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
]]--
local m= { _info= { MODULE= "Assert.* functions for constraints, and unit testing",
AUTHOR= "akauppi@gmail.com",
VERSION= 20070603, -- last change (yyyymmdd)
LICENSE= "MIT/X11" } }
-- Global changes:
-- 'assert' redefined, in a backwards compatible way
--
-- Module functions:
-- none
assert( type(assert) == "function" ) -- system assert function
-----
-- Integer range: INT_MIN..INT_MAX
--
local function try_maxint( n )
return (n > n-1) and n-1 -- false when outside the integer range
end
local INT_MAX=
try_maxint( 2^64 ) or
try_maxint( 2^32 ) or
try_maxint( 2^24 ) or -- float (24-bit mantissa)
assert( false )
local INT_MIN= -(INT_MAX+1)
---=== assert.*() ===---
local at_msg= "type assertion error" -- TBD: better messages, about that exact situation
local av_msg= "value assertion error"
-- void= _assert( val [, msg_str [, lev_uint]] )
--
local function _assert( cond, msg, lev )
-- original 'assert' provides no level override, so we use 'error'.
--
if not cond then
error( msg or "assertion failed!", (lev or 1)+1 )
end
end
-- Note: following code uses the _new_ 'assert()' by purpose, since it provides
-- a level override (original doesn't)
--
local function assert_v( v0 )
return function(v,msg)
_assert( v == v0, msg or av_msg, 2 )
return v
end
end
local function assert_t( str )
return function(v,msg)
_assert( type(v) == str, msg or at_msg, 2 )
return v
end
end
local function assert_t2( str )
return function(v,subtype,msg)
local t,st= type(v)
_assert( t==str and ((not subtype) or (st==subtype)),
msg or at_msg, 2 )
return v
end
end
assert=
{
__call= function(_,v,msg) -- plain 'assert()' (compatibility)
if v then return v end
_assert( v, msg, 2 )
end,
-- Hopefully, Lua will allow use of 'nil', 'function' and other reserved words as table
-- shortcuts in the future (5.1.1 does not).
--
["nil"]= assert_v( nil ),
boolean= assert_t "boolean",
table= assert_t2 "table",
["function"]= assert_t "function",
userdata= assert_t2 "userdata",
string= function( v, msg )
local s= tostring(v)
_assert( s, msg or at_msg, 2 )
return v
end,
char= function( v, msg )
-- 'char' is _not_ doing int->string conversion
_assert( type(v)=="string" and v:len()==1, msg or at_msg, 2 )
return v
end,
number= function( v, msg )
_assert( tonumber(v), msg or at_msg, 2 )
return v
end,
int= function( v, msg )
local n= tonumber(v)
_assert( n and (n >= INT_MIN) and (n <= INT_MAX) and math.floor(n) == n,
msg or at_msg, 2 )
return v
end,
uint= function( v, msg )
local n= tonumber(v)
-- unsigned integer upper range is the same as integers' (there's no
-- real unsigned within the Lua)
_assert( n and (n >= 0) and (n <= INT_MAX) and math.floor(n) == n,
msg or at_msg, 2 )
return v
end,
['true']= assert_v( true ),
['false']= assert_v( false ),
string_or_table= function( v, msg )
assert( tostring(v) or type(v)=="table", msg or at_msg, 2 )
return v
end,
number_or_string= function( v, msg )
assert( tonumber(v) or type(v)=="table", msg or at_msg, 2 )
return v
end,
any= function( v, msg )
assert( v ~= nil, msg or av_msg, 2 )
return v
end,
-- Range assertion, with extra arguments per instance
--
-- Note: values may be of _any_ type that can do >=, <= comparisons.
--
range= function( lo, hi )
_assert( lo and hi and lo <= hi, "Bad limits", 2 )
-- make sure the limits make sense (just once)
return function(v,msg,lev)
if ( (lo and v<lo) or (hi and v>hi) ) then
msg= msg or "not in range: ("..(lo or "")..","..(hi or "")..")"
_assert( false, msg, 2 )
end
return v
end
end,
-- Table contents assertion
-- - no unknown (non-numeric) keys are allowed
-- - numeric keys are ignored
--
-- Constraints patch should point to this, when using the ":{ ... }" constraint.
--
["{}"]= function( tbl )
-- check all keys in 't' (including numeric, if any) that they do exist,
-- and carry the right type
--
local function subf1(v,t,msg,lev)
_assert(lev)
for k,f in pairs(t) do
-- 'f' is an assert function, or subtable
local ft= type(f)
if ft=="function" then
f( v[k], msg, lev+1 )
elseif ft=="table" then
_assert( type(v[k])=="table", msg or "no subtable "..tostring(k), lev+1 )
subf1( v[k], f, msg, lev+1 )
else
error( "Bad constraints table for '"..tostring(k).."'! (not a function)", lev+1 )
end
end
end
-- check there are no other (non-numeric) keys in 'v'
local function subf2(v,t,msg,lev)
_assert(lev)
for k,vv in pairs(v) do
if type(k)=="number" then
-- skip them
elseif not t[k] then
_assert( false, msg or "extra field: '"..tostring(k).."'", lev+1 )
elseif type(vv)=="table" then
subf2( vv, t[k], msg, lev+1 )
end
end
end
_assert( type(tbl)=="table", "Wrong argument to assert['{}']" )
return function( v, msg, lev )
lev= (lev or 1)+1
_assert( type(v)=="table" ,msg, lev )
subf1( v, tbl, msg, lev )
subf2( v, tbl, msg, lev )
return v
end
end,
-- ...
}
setmetatable( assert, assert )
assert.void= assert["nil"]
-----
-- void= assert.fails( function [,err_msg_str] )
--
-- Special assert function, to make sure the call within it fails, and gives a
-- specific error message (to be used in unit testing).
--
function assert.fails( func_block, err_msg )
--
local st,err= pcall( func_block )
if st then
_assert( false, "Block expected to fail, but didn't.", 2 )
elseif err_msg and err ~= err_msg then
_assert( false, "Failed with wrong error message: \n"..
"'"..err.."'\nexpected: '"..err_msg.."'", 2 )
end
end
-----
-- void= assert.failsnot( function [,err_msg_str] )
--
-- Similar to 'assert.fails' but expects the code to survive.
--
function assert.failsnot( func_block, err_msg )
--
local st,err= pcall( func_block )
if not st then
_assert( false, "Block expected NOT to fail, but did."..
(err and "\n\tError: '"..err.."'" or ""), 2 )
end
end
-----
-- void= assert.nilerr( function [,err_msg_str] )
--
-- Expects the function to return with 'nil,err' failure code, with
-- optionally error string matching. Similar to --> 'assert.fails()'
--
function assert.nilerr( func_block, err_msg )
--
local v,err= func_block()
_assert( v==nil, "Expected to return nil, but didn't: "..tostring(v), 2 )
if err_msg and err ~= err_msg then
_assert( false, "Failed with wrong error message: \n"..
"'"..err.."'\nexpected: '"..err_msg.."'", 2 )
end
end
-- Sanity check
--
assert( true )
assert.fails( function() assert( false ) end )
assert.fails( function() assert( nil ) end )
return m -- just info
|