diff options
author | Peter Drahoš <drahosp@gmail.com> | 2010-10-01 03:22:32 +0200 |
---|---|---|
committer | Peter Drahoš <drahosp@gmail.com> | 2010-10-01 03:22:32 +0200 |
commit | 89d9c98af1ac352ba4d49d660e61b0853d6e1a86 (patch) | |
tree | 15c56d2ce66b4ab147171c0f674cdb4a435ff13f /tests/assert.lua | |
download | lanes-89d9c98af1ac352ba4d49d660e61b0853d6e1a86.tar.gz lanes-89d9c98af1ac352ba4d49d660e61b0853d6e1a86.tar.bz2 lanes-89d9c98af1ac352ba4d49d660e61b0853d6e1a86.zip |
Import to git
Diffstat (limited to 'tests/assert.lua')
-rw-r--r-- | tests/assert.lua | 318 |
1 files changed, 318 insertions, 0 deletions
diff --git a/tests/assert.lua b/tests/assert.lua new file mode 100644 index 0000000..85febfb --- /dev/null +++ b/tests/assert.lua | |||
@@ -0,0 +1,318 @@ | |||
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 parameter & 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= 20070603, -- 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 | |||
63 | ----- | ||
64 | -- Integer range: INT_MIN..INT_MAX | ||
65 | -- | ||
66 | local function try_maxint( n ) | ||
67 | return (n > n-1) and n-1 -- false when outside the integer range | ||
68 | end | ||
69 | |||
70 | local INT_MAX= | ||
71 | try_maxint( 2^64 ) or | ||
72 | try_maxint( 2^32 ) or | ||
73 | try_maxint( 2^24 ) or -- float (24-bit mantissa) | ||
74 | assert( false ) | ||
75 | |||
76 | local INT_MIN= -(INT_MAX+1) | ||
77 | |||
78 | |||
79 | ---=== assert.*() ===--- | ||
80 | |||
81 | local at_msg= "type assertion error" -- TBD: better messages, about that exact situation | ||
82 | local av_msg= "value assertion error" | ||
83 | |||
84 | -- void= _assert( val [, msg_str [, lev_uint]] ) | ||
85 | -- | ||
86 | local function _assert( cond, msg, lev ) | ||
87 | -- original 'assert' provides no level override, so we use 'error'. | ||
88 | -- | ||
89 | if not cond then | ||
90 | error( msg or "assertion failed!", (lev or 1)+1 ) | ||
91 | end | ||
92 | end | ||
93 | |||
94 | -- Note: following code uses the _new_ 'assert()' by purpose, since it provides | ||
95 | -- a level override (original doesn't) | ||
96 | -- | ||
97 | local function assert_v( v0 ) | ||
98 | return function(v,msg) | ||
99 | _assert( v == v0, msg or av_msg, 2 ) | ||
100 | return v | ||
101 | end | ||
102 | end | ||
103 | local function assert_t( str ) | ||
104 | return function(v,msg) | ||
105 | _assert( type(v) == str, msg or at_msg, 2 ) | ||
106 | return v | ||
107 | end | ||
108 | end | ||
109 | local function assert_t2( str ) | ||
110 | return function(v,subtype,msg) | ||
111 | local t,st= type(v) | ||
112 | _assert( t==str and ((not subtype) or (st==subtype)), | ||
113 | msg or at_msg, 2 ) | ||
114 | return v | ||
115 | end | ||
116 | end | ||
117 | |||
118 | assert= | ||
119 | { | ||
120 | __call= function(_,v,msg) -- plain 'assert()' (compatibility) | ||
121 | if v then return v end | ||
122 | _assert( v, msg, 2 ) | ||
123 | end, | ||
124 | |||
125 | -- Hopefully, Lua will allow use of 'nil', 'function' and other reserved words as table | ||
126 | -- shortcuts in the future (5.1.1 does not). | ||
127 | -- | ||
128 | ["nil"]= assert_v( nil ), | ||
129 | boolean= assert_t "boolean", | ||
130 | table= assert_t2 "table", | ||
131 | ["function"]= assert_t "function", | ||
132 | userdata= assert_t2 "userdata", | ||
133 | |||
134 | string= function( v, msg ) | ||
135 | local s= tostring(v) | ||
136 | _assert( s, msg or at_msg, 2 ) | ||
137 | return v | ||
138 | end, | ||
139 | |||
140 | char= function( v, msg ) | ||
141 | -- 'char' is _not_ doing int->string conversion | ||
142 | _assert( type(v)=="string" and v:len()==1, msg or at_msg, 2 ) | ||
143 | return v | ||
144 | end, | ||
145 | |||
146 | number= function( v, msg ) | ||
147 | _assert( tonumber(v), msg or at_msg, 2 ) | ||
148 | return v | ||
149 | end, | ||
150 | |||
151 | int= function( v, msg ) | ||
152 | local n= tonumber(v) | ||
153 | _assert( n and (n >= INT_MIN) and (n <= INT_MAX) and math.floor(n) == n, | ||
154 | msg or at_msg, 2 ) | ||
155 | return v | ||
156 | end, | ||
157 | |||
158 | uint= function( v, msg ) | ||
159 | local n= tonumber(v) | ||
160 | -- unsigned integer upper range is the same as integers' (there's no | ||
161 | -- real unsigned within the Lua) | ||
162 | _assert( n and (n >= 0) and (n <= INT_MAX) and math.floor(n) == n, | ||
163 | msg or at_msg, 2 ) | ||
164 | return v | ||
165 | end, | ||
166 | |||
167 | ['true']= assert_v( true ), | ||
168 | ['false']= assert_v( false ), | ||
169 | |||
170 | string_or_table= function( v, msg ) | ||
171 | assert( tostring(v) or type(v)=="table", msg or at_msg, 2 ) | ||
172 | return v | ||
173 | end, | ||
174 | |||
175 | number_or_string= function( v, msg ) | ||
176 | assert( tonumber(v) or type(v)=="table", msg or at_msg, 2 ) | ||
177 | return v | ||
178 | end, | ||
179 | |||
180 | any= function( v, msg ) | ||
181 | assert( v ~= nil, msg or av_msg, 2 ) | ||
182 | return v | ||
183 | end, | ||
184 | |||
185 | -- Range assertion, with extra parameters per instance | ||
186 | -- | ||
187 | -- Note: values may be of _any_ type that can do >=, <= comparisons. | ||
188 | -- | ||
189 | range= function( lo, hi ) | ||
190 | _assert( lo and hi and lo <= hi, "Bad limits", 2 ) | ||
191 | -- make sure the limits make sense (just once) | ||
192 | |||
193 | return function(v,msg,lev) | ||
194 | if ( (lo and v<lo) or (hi and v>hi) ) then | ||
195 | msg= msg or "not in range: ("..(lo or "")..","..(hi or "")..")" | ||
196 | _assert( false, msg, 2 ) | ||
197 | end | ||
198 | return v | ||
199 | end | ||
200 | end, | ||
201 | |||
202 | -- Table contents assertion | ||
203 | -- - no unknown (non-numeric) keys are allowed | ||
204 | -- - numeric keys are ignored | ||
205 | -- | ||
206 | -- Constraints patch should point to this, when using the ":{ ... }" constraint. | ||
207 | -- | ||
208 | ["{}"]= function( tbl ) | ||
209 | |||
210 | -- check all keys in 't' (including numeric, if any) that they do exist, | ||
211 | -- and carry the right type | ||
212 | -- | ||
213 | local function subf1(v,t,msg,lev) | ||
214 | _assert(lev) | ||
215 | for k,f in pairs(t) do | ||
216 | -- 'f' is an assert function, or subtable | ||
217 | local ft= type(f) | ||
218 | if ft=="function" then | ||
219 | f( v[k], msg, lev+1 ) | ||
220 | elseif ft=="table" then | ||
221 | _assert( type(v[k])=="table", msg or "no subtable "..tostring(k), lev+1 ) | ||
222 | subf1( v[k], f, msg, lev+1 ) | ||
223 | else | ||
224 | error( "Bad constraints table for '"..tostring(k).."'! (not a function)", lev+1 ) | ||
225 | end | ||
226 | end | ||
227 | end | ||
228 | |||
229 | -- check there are no other (non-numeric) keys in 'v' | ||
230 | local function subf2(v,t,msg,lev) | ||
231 | _assert(lev) | ||
232 | for k,vv in pairs(v) do | ||
233 | if type(k)=="number" then | ||
234 | -- skip them | ||
235 | elseif not t[k] then | ||
236 | _assert( false, msg or "extra field: '"..tostring(k).."'", lev+1 ) | ||
237 | elseif type(vv)=="table" then | ||
238 | subf2( vv, t[k], msg, lev+1 ) | ||
239 | end | ||
240 | end | ||
241 | end | ||
242 | |||
243 | _assert( type(tbl)=="table", "Wrong parameter to assert['{}']" ) | ||
244 | |||
245 | return function( v, msg, lev ) | ||
246 | lev= (lev or 1)+1 | ||
247 | _assert( type(v)=="table" ,msg, lev ) | ||
248 | subf1( v, tbl, msg, lev ) | ||
249 | subf2( v, tbl, msg, lev ) | ||
250 | return v | ||
251 | end | ||
252 | end, | ||
253 | |||
254 | -- ... | ||
255 | } | ||
256 | setmetatable( assert, assert ) | ||
257 | |||
258 | assert.void= assert["nil"] | ||
259 | |||
260 | |||
261 | ----- | ||
262 | -- void= assert.fails( function [,err_msg_str] ) | ||
263 | -- | ||
264 | -- Special assert function, to make sure the call within it fails, and gives a | ||
265 | -- specific error message (to be used in unit testing). | ||
266 | -- | ||
267 | function assert.fails( func_block, err_msg ) | ||
268 | -- | ||
269 | local st,err= pcall( func_block ) | ||
270 | if st then | ||
271 | _assert( false, "Block expected to fail, but didn't.", 2 ) | ||
272 | elseif err_msg and err ~= err_msg then | ||
273 | _assert( false, "Failed with wrong error message: \n".. | ||
274 | "'"..err.."'\nexpected: '"..err_msg.."'", 2 ) | ||
275 | end | ||
276 | end | ||
277 | |||
278 | |||
279 | ----- | ||
280 | -- void= assert.failsnot( function [,err_msg_str] ) | ||
281 | -- | ||
282 | -- Similar to 'assert.fails' but expects the code to survive. | ||
283 | -- | ||
284 | function assert.failsnot( func_block, err_msg ) | ||
285 | -- | ||
286 | local st,err= pcall( func_block ) | ||
287 | if not st then | ||
288 | _assert( false, "Block expected NOT to fail, but did.".. | ||
289 | (err and "\n\tError: '"..err.."'" or ""), 2 ) | ||
290 | end | ||
291 | end | ||
292 | |||
293 | |||
294 | ----- | ||
295 | -- void= assert.nilerr( function [,err_msg_str] ) | ||
296 | -- | ||
297 | -- Expects the function to return with 'nil,err' failure code, with | ||
298 | -- optionally error string matching. Similar to --> 'assert.fails()' | ||
299 | -- | ||
300 | function assert.nilerr( func_block, err_msg ) | ||
301 | -- | ||
302 | local v,err= func_block() | ||
303 | _assert( v==nil, "Expected to return nil, but didn't: "..tostring(v), 2 ) | ||
304 | if err_msg and err ~= err_msg then | ||
305 | _assert( false, "Failed with wrong error message: \n".. | ||
306 | "'"..err.."'\nexpected: '"..err_msg.."'", 2 ) | ||
307 | end | ||
308 | end | ||
309 | |||
310 | |||
311 | -- Sanity check | ||
312 | -- | ||
313 | assert( true ) | ||
314 | assert.fails( function() assert( false ) end ) | ||
315 | assert.fails( function() assert( nil ) end ) | ||
316 | |||
317 | |||
318 | return m -- just info | ||