Metatest 0.1.0

Kay Schluehr kay.schluehr at gmx.net
Wed Sep 19 06:06:57 EDT 2007


On 19 Sep., 01:30, Jonathan Fine <jf... at pytex.org> wrote:

> > there is no fundamental reason why it can't be separated from
> > eeconsole.py.
>
> OK.  That might be a good idea.

Ironically, I liked the idea of having more expressive assert
statements - a discussion you brought up. But this requires either
syntcatical analysis in the general case ( EE and assert magic ) some
particular API ( Ben Finney ) or a somewhat restricted, but fully
embedded, domain specific language ( Metatest ).

Here is what I got after an hour of hacking and filtering assert.
Additional remarks are in line comments:

_______________________________________________________________________________

 ZERO

 On Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)]

 Reuses session report ZERO_20.eerp

_______________________________________________________________________________

# define a test function add and check it out

>>> def add(y,x):
...     return x+y
...
>>> assert add(1,2) == 0
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AssertionError: 3 == 0

# An tested assertion is splitted into lhs, op, rhs. If this is not
possible assert
# won't be changed by magics.
# Here we see a simple formatting of the result in the form:
#     "%s %s %s" % (eval(lhs), op, eval(rhs))
# More elaborated formatting schemes are user defined:

>>> class MyFormatter(AssertionFormatter):
... 	def format(self, lhs, op, rhs):
... 		if op == "==":
... 			return "Expected: %s. Received: %s"%(lhs, rhs)
... 		else:
... 			return super(MyFormatter, self).format(lhs, op, rhs)
...

# The class AssertionFormatter is defined in
EasyExtend.fibers.zero.fiber.
# It provides a single method
#
#        format(lhs: str, op: str, rhs: str) -> str
#
# The default behaviour is the one
# demonstrated above. One can overwrite format in subclasses and pass
the subclass
# instance to assert.
# The arguments will be supplied by the framework.


# Here we see the new formatter in action:

>>> assert add(1,2) == 0, MyFormatter
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AssertionError: Expected: 3. Received: 0

# Now it falls back to the default formatting:

?>>>
>>> !
>>> assert add(1,2) <=0, MyFormatter
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AssertionError: 3 <= 0
>>> quit

--------------------.
Recorded assertions |
--------------------------------------------------------------------------------------------------
Status |eerp ln|repl ln| Assertion
-------+-------+-------
+--------------------------------------------------------------------------
ERROR  | 14    | 14    | assert add(1,2) == 0
ERROR  | 26    | 26    | assert add(1,2) == 0, MyFormatter
ERROR  | 31    | 31    | assert add(1,2) <=0, MyFormatter
-------+-------+-------
+--------------------------------------------------------------------------

Regards, Kay




More information about the Python-list mailing list