diff options
Diffstat (limited to 'unit_tests/scripts/_assert.lua')
| -rw-r--r-- | unit_tests/scripts/_assert.lua | 321 |
1 files changed, 321 insertions, 0 deletions
diff --git a/unit_tests/scripts/_assert.lua b/unit_tests/scripts/_assert.lua new file mode 100644 index 0000000..9bc8d32 --- /dev/null +++ b/unit_tests/scripts/_assert.lua | |||
| @@ -0,0 +1,321 @@ | |||
| 1 | -- | ||
| 2 | -- ASSERT.LUA Copyright (c) 2006-07, <akauppi@gmail.com> | ||
| 3 | -- | ||
| 4 | -- Converting the Lua 'assert' function into a namespace table (without | ||
| 5 | -- breaking compatibility with the basic 'assert()' calling). | ||
| 6 | -- | ||
| 7 | -- This module allows shorthand use s.a. 'assert.table()' for asserting | ||
| 8 | -- variable types, and is also being used by Lua-super constraints system | ||
| 9 | -- for testing function argument & return types. | ||
| 10 | -- | ||
| 11 | -- All in all, a worthy module and could be part of Lua future versions. | ||
| 12 | -- | ||
| 13 | -- Note: the 'assert' table is available for your own assertions, too. Just add | ||
| 14 | -- more functions s.a. 'assert.myobj()' to check for custom invariants. | ||
| 15 | -- They will then be available for the constraints check, too. | ||
| 16 | -- | ||
| 17 | -- Author: <akauppi@gmail.com> | ||
| 18 | -- | ||
| 19 | --[[ | ||
| 20 | /****************************************************************************** | ||
| 21 | * Lua 5.1.1 support and extension functions (assert.lua) | ||
| 22 | * | ||
| 23 | * Copyright (C) 2006-07, Asko Kauppi. | ||
| 24 | * | ||
| 25 | * NOTE: This license concerns only the particular source file; not necessarily | ||
| 26 | * the project with which it has been delivered (the project may have a more | ||
| 27 | * restrictive license, s.a. [L]GPL). | ||
| 28 | * | ||
| 29 | * Permission is hereby granted, free of charge, to any person obtaining | ||
| 30 | * a copy of this software and associated documentation files (the | ||
| 31 | * "Software"), to deal in the Software without restriction, including | ||
| 32 | * without limitation the rights to use, copy, modify, merge, publish, | ||
| 33 | * distribute, sublicense, and/or sell copies of the Software, and to | ||
| 34 | * permit persons to whom the Software is furnished to do so, subject to | ||
| 35 | * the following conditions: | ||
| 36 | * | ||
| 37 | * The above copyright notice and this permission notice shall be | ||
| 38 | * included in all copies or substantial portions of the Software. | ||
| 39 | * | ||
| 40 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 41 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 42 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
| 43 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
| 44 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
| 45 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
| 46 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 47 | ******************************************************************************/ | ||
| 48 | ]]-- | ||
| 49 | |||
| 50 | local m= { _info= { MODULE= "Assert.* functions for constraints, and unit testing", | ||
| 51 | AUTHOR= "akauppi@gmail.com", | ||
| 52 | VERSION= 20240702, -- last change (yyyymmdd) | ||
| 53 | LICENSE= "MIT/X11" } } | ||
| 54 | |||
| 55 | -- Global changes: | ||
| 56 | -- 'assert' redefined, in a backwards compatible way | ||
| 57 | -- | ||
| 58 | -- Module functions: | ||
| 59 | -- none | ||
| 60 | |||
| 61 | assert( type(assert) == "function" ) -- system assert function | ||
| 62 | assert( type(error) == "function") | ||
| 63 | |||
| 64 | ----- | ||
| 65 | -- Integer range: INT_MIN..INT_MAX | ||
| 66 | -- | ||
| 67 | local function try_maxint( n ) | ||
| 68 | return (n > n-1) and n-1 -- false when outside the integer range | ||
| 69 | end | ||
| 70 | |||
| 71 | local INT_MAX= | ||
| 72 | try_maxint( 2^64 ) or | ||
| 73 | try_maxint( 2^32 ) or | ||
| 74 | try_maxint( 2^24 ) or -- float (24-bit mantissa) | ||
| 75 | assert( false ) | ||
| 76 | |||
| 77 | local INT_MIN= -(INT_MAX+1) | ||
| 78 | |||
| 79 | |||
| 80 | ---=== assert.*() ===--- | ||
| 81 | |||
| 82 | local at_msg= "type assertion error" -- TBD: better messages, about that exact situation | ||
| 83 | local av_msg= "value assertion error" | ||
| 84 | |||
| 85 | -- void= _assert( val [, msg_str [, lev_uint]] ) | ||
| 86 | -- | ||
| 87 | local function _assert( cond, msg, lev ) | ||
| 88 | -- original 'assert' provides no level override, so we use 'error'. | ||
| 89 | -- | ||
| 90 | if not cond then | ||
| 91 | error( msg or "assertion failed!", (lev or 1)+1 ) | ||
| 92 | end | ||
| 93 | end | ||
| 94 | |||
| 95 | -- Note: following code uses the _new_ 'assert()' by purpose, since it provides | ||
| 96 | -- a level override (original doesn't) | ||
| 97 | -- | ||
| 98 | local function assert_v( v0 ) | ||
| 99 | return function(v,msg) | ||
| 100 | _assert( v == v0, msg or av_msg, 2 ) | ||
| 101 | return v | ||
| 102 | end | ||
| 103 | end | ||
| 104 | local function assert_t( str ) | ||
| 105 | return function(v,msg) | ||
| 106 | _assert( type(v) == str, msg or at_msg, 2 ) | ||
| 107 | return v | ||
| 108 | end | ||
| 109 | end | ||
| 110 | local function assert_t2( str ) | ||
| 111 | return function(v,subtype,msg) | ||
| 112 | local t,st= type(v) | ||
| 113 | _assert( t==str and ((not subtype) or (st==subtype)), | ||
| 114 | msg or at_msg, 2 ) | ||
| 115 | return v | ||
| 116 | end | ||
| 117 | end | ||
| 118 | |||
| 119 | -- global base function assert() is replaced by this table | ||
| 120 | assert= | ||
| 121 | { | ||
| 122 | __metatable = "assert()", | ||
| 123 | __call= function(_,v,msg) -- plain 'assert()' (compatibility) | ||
| 124 | if v then return v end | ||
| 125 | _assert( v, msg, 2 ) | ||
| 126 | end, | ||
| 127 | |||
| 128 | -- Hopefully, Lua will allow use of 'nil', 'function' and other reserved words as table | ||
| 129 | -- shortcuts in the future (5.1.1 does not). | ||
| 130 | -- | ||
| 131 | ["nil"]= assert_v( nil ), | ||
| 132 | boolean= assert_t "boolean", | ||
| 133 | table= assert_t2 "table", | ||
| 134 | ["function"]= assert_t "function", | ||
| 135 | userdata= assert_t2 "userdata", | ||
| 136 | |||
| 137 | string= function( v, msg ) | ||
| 138 | local s= tostring(v) | ||
| 139 | _assert( s, msg or at_msg, 2 ) | ||
| 140 | return v | ||
| 141 | end, | ||
| 142 | |||
| 143 | char= function( v, msg ) | ||
| 144 | -- 'char' is _not_ doing int->string conversion | ||
| 145 | _assert( type(v)=="string" and v:len()==1, msg or at_msg, 2 ) | ||
| 146 | return v | ||
| 147 | end, | ||
| 148 | |||
| 149 | number= function( v, msg ) | ||
| 150 | _assert( tonumber(v), msg or at_msg, 2 ) | ||
| 151 | return v | ||
| 152 | end, | ||
| 153 | |||
| 154 | int= function( v, msg ) | ||
| 155 | local n= tonumber(v) | ||
| 156 | _assert( n and (n >= INT_MIN) and (n <= INT_MAX) and math.floor(n) == n, | ||
| 157 | msg or at_msg, 2 ) | ||
| 158 | return v | ||
| 159 | end, | ||
| 160 | |||
| 161 | uint= function( v, msg ) | ||
| 162 | local n= tonumber(v) | ||
| 163 | -- unsigned integer upper range is the same as integers' (there's no | ||
| 164 | -- real unsigned within the Lua) | ||
| 165 | _assert( n and (n >= 0) and (n <= INT_MAX) and math.floor(n) == n, | ||
| 166 | msg or at_msg, 2 ) | ||
| 167 | return v | ||
| 168 | end, | ||
| 169 | |||
| 170 | ['true']= assert_v( true ), | ||
| 171 | ['false']= assert_v( false ), | ||
| 172 | |||
| 173 | string_or_table= function( v, msg ) | ||
| 174 | assert( tostring(v) or type(v)=="table", msg or at_msg, 2 ) | ||
| 175 | return v | ||
| 176 | end, | ||
| 177 | |||
| 178 | number_or_string= function( v, msg ) | ||
| 179 | assert( tonumber(v) or type(v)=="table", msg or at_msg, 2 ) | ||
| 180 | return v | ||
| 181 | end, | ||
| 182 | |||
| 183 | any= function( v, msg ) | ||
| 184 | assert( v ~= nil, msg or av_msg, 2 ) | ||
| 185 | return v | ||
| 186 | end, | ||
| 187 | |||
| 188 | -- Range assertion, with extra arguments per instance | ||
| 189 | -- | ||
| 190 | -- Note: values may be of _any_ type that can do >=, <= comparisons. | ||
| 191 | -- | ||
| 192 | range= function( lo, hi ) | ||
| 193 | _assert( lo and hi and lo <= hi, "Bad limits", 2 ) | ||
| 194 | -- make sure the limits make sense (just once) | ||
| 195 | |||
| 196 | return function(v,msg,lev) | ||
| 197 | if ( (lo and v<lo) or (hi and v>hi) ) then | ||
| 198 | msg= msg or "not in range: ("..(lo or "")..","..(hi or "")..")" | ||
| 199 | _assert( false, msg, 2 ) | ||
| 200 | end | ||
| 201 | return v | ||
| 202 | end | ||
| 203 | end, | ||
| 204 | |||
| 205 | -- Table contents assertion | ||
| 206 | -- - no unknown (non-numeric) keys are allowed | ||
| 207 | -- - numeric keys are ignored | ||
| 208 | -- | ||
| 209 | -- Constraints patch should point to this, when using the ":{ ... }" constraint. | ||
| 210 | -- | ||
| 211 | ["{}"]= function( tbl ) | ||
| 212 | |||
| 213 | -- check all keys in 't' (including numeric, if any) that they do exist, | ||
| 214 | -- and carry the right type | ||
| 215 | -- | ||
| 216 | local function subf1(v,t,msg,lev) | ||
| 217 | _assert(lev) | ||
| 218 | for k,f in pairs(t) do | ||
| 219 | -- 'f' is an assert function, or subtable | ||
| 220 | local ft= type(f) | ||
| 221 | if ft=="function" then | ||
| 222 | f( v[k], msg, lev+1 ) | ||
| 223 | elseif ft=="table" then | ||
| 224 | _assert( type(v[k])=="table", msg or "no subtable "..tostring(k), lev+1 ) | ||
| 225 | subf1( v[k], f, msg, lev+1 ) | ||
| 226 | else | ||
| 227 | error( "Bad constraints table for '"..tostring(k).."'! (not a function)", lev+1 ) | ||
| 228 | end | ||
| 229 | end | ||
| 230 | end | ||
| 231 | |||
| 232 | -- check there are no other (non-numeric) keys in 'v' | ||
| 233 | local function subf2(v,t,msg,lev) | ||
| 234 | _assert(lev) | ||
| 235 | for k,vv in pairs(v) do | ||
| 236 | if type(k)=="number" then | ||
| 237 | -- skip them | ||
| 238 | elseif not t[k] then | ||
| 239 | _assert( false, msg or "extra field: '"..tostring(k).."'", lev+1 ) | ||
| 240 | elseif type(vv)=="table" then | ||
| 241 | subf2( vv, t[k], msg, lev+1 ) | ||
| 242 | end | ||
| 243 | end | ||
| 244 | end | ||
| 245 | |||
| 246 | _assert( type(tbl)=="table", "Wrong argument to assert['{}']" ) | ||
| 247 | |||
| 248 | return function( v, msg, lev ) | ||
| 249 | lev= (lev or 1)+1 | ||
| 250 | _assert( type(v)=="table" ,msg, lev ) | ||
| 251 | subf1( v, tbl, msg, lev ) | ||
| 252 | subf2( v, tbl, msg, lev ) | ||
| 253 | return v | ||
| 254 | end | ||
| 255 | end, | ||
| 256 | |||
| 257 | -- ... | ||
| 258 | } | ||
| 259 | setmetatable( assert, assert ) | ||
| 260 | |||
| 261 | assert.void= assert["nil"] | ||
| 262 | |||
| 263 | |||
| 264 | ----- | ||
| 265 | -- void= assert.fails( function [,err_msg_str] ) | ||
| 266 | -- | ||
| 267 | -- Special assert function, to make sure the call within it fails, and gives a | ||
| 268 | -- specific error message (to be used in unit testing). | ||
| 269 | -- | ||
| 270 | function assert.fails( func_block, err_msg ) | ||
| 271 | -- | ||
| 272 | local st,err= pcall( func_block ) | ||
| 273 | if st then | ||
| 274 | _assert( false, "Block expected to fail, but didn't.", 2 ) | ||
| 275 | elseif err_msg and err ~= err_msg then | ||
| 276 | _assert( false, "Failed with wrong error message: \n".. | ||
| 277 | "'"..err.."'\nexpected: '"..err_msg.."'", 2 ) | ||
| 278 | end | ||
| 279 | end | ||
| 280 | |||
| 281 | |||
| 282 | ----- | ||
| 283 | -- void= assert.failsnot( function [,err_msg_str] ) | ||
| 284 | -- | ||
| 285 | -- Similar to 'assert.fails' but expects the code to survive. | ||
| 286 | -- | ||
| 287 | function assert.failsnot( func_block, err_msg ) | ||
| 288 | -- | ||
| 289 | local st,err= pcall( func_block ) | ||
| 290 | if not st then | ||
| 291 | _assert( false, "Block expected NOT to fail, but did.".. | ||
| 292 | (err and "\n\tError: '"..err.."'" or ""), 2 ) | ||
| 293 | end | ||
| 294 | end | ||
| 295 | |||
| 296 | |||
| 297 | ----- | ||
| 298 | -- void= assert.nilerr( function [,err_msg_str] ) | ||
| 299 | -- | ||
| 300 | -- Expects the function to return with 'nil,err' failure code, with | ||
| 301 | -- optionally error string matching. Similar to --> 'assert.fails()' | ||
| 302 | -- | ||
| 303 | function assert.nilerr( func_block, err_msg ) | ||
| 304 | -- | ||
| 305 | local v,err= func_block() | ||
| 306 | _assert( v==nil, "Expected to return nil, but didn't: "..tostring(v), 2 ) | ||
| 307 | if err_msg and err ~= err_msg then | ||
| 308 | _assert( false, "Failed with wrong error message: \n".. | ||
| 309 | "'"..err.."'\nexpected: '"..err_msg.."'", 2 ) | ||
| 310 | end | ||
| 311 | end | ||
| 312 | |||
| 313 | |||
| 314 | -- Sanity check | ||
| 315 | -- | ||
| 316 | assert( true ) | ||
| 317 | assert.fails( function() assert( false ) end ) | ||
| 318 | assert.fails( function() assert( nil ) end ) | ||
| 319 | |||
| 320 | |||
| 321 | return m -- just info | ||
