2006-11-30 15:22:25 -07:00
|
|
|
"""
|
|
|
|
Tests useful in assertion checking, prints out nicely formated messages too.
|
|
|
|
"""
|
|
|
|
|
2010-02-26 01:14:33 -07:00
|
|
|
from allmydata.util.humanreadable import hr
|
2006-11-30 15:22:25 -07:00
|
|
|
|
|
|
|
def _assert(___cond=False, *___args, **___kwargs):
|
|
|
|
if ___cond:
|
|
|
|
return True
|
|
|
|
msgbuf=[]
|
|
|
|
if ___args:
|
|
|
|
msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),))))
|
|
|
|
msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]])
|
|
|
|
if ___kwargs:
|
|
|
|
msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
|
|
|
|
else:
|
|
|
|
if ___kwargs:
|
|
|
|
msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
|
|
|
|
msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]])
|
|
|
|
|
2019-03-30 16:58:02 +01:00
|
|
|
raise AssertionError("".join(msgbuf))
|
2006-11-30 15:22:25 -07:00
|
|
|
|
|
|
|
def precondition(___cond=False, *___args, **___kwargs):
|
2008-12-16 17:37:45 -07:00
|
|
|
if ___cond:
|
|
|
|
return True
|
|
|
|
msgbuf=["precondition", ]
|
|
|
|
if ___args or ___kwargs:
|
|
|
|
msgbuf.append(": ")
|
|
|
|
if ___args:
|
|
|
|
msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),))))
|
|
|
|
msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]])
|
|
|
|
if ___kwargs:
|
|
|
|
msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
|
|
|
|
else:
|
|
|
|
if ___kwargs:
|
|
|
|
msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
|
|
|
|
msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]])
|
2006-11-30 15:22:25 -07:00
|
|
|
|
2019-03-30 16:58:02 +01:00
|
|
|
raise AssertionError("".join(msgbuf))
|
2006-11-30 15:22:25 -07:00
|
|
|
|
|
|
|
def postcondition(___cond=False, *___args, **___kwargs):
|
|
|
|
if ___cond:
|
|
|
|
return True
|
|
|
|
msgbuf=["postcondition", ]
|
|
|
|
if ___args or ___kwargs:
|
|
|
|
msgbuf.append(": ")
|
|
|
|
if ___args:
|
|
|
|
msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),))))
|
|
|
|
msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]])
|
|
|
|
if ___kwargs:
|
|
|
|
msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
|
|
|
|
else:
|
|
|
|
if ___kwargs:
|
|
|
|
msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
|
|
|
|
msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]])
|
|
|
|
|
2019-03-30 16:58:02 +01:00
|
|
|
raise AssertionError("".join(msgbuf))
|