Skip to content

Commit

Permalink
Implement doctest traceback compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
PiDelport committed Feb 22, 2012
1 parent 8ae8da3 commit a2ce435
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 31 deletions.
16 changes: 16 additions & 0 deletions ludibrio/specification/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
This module provides doctest compatibility for Python < 3.
"""
import sys


if sys.version_info < (3,):
# Python 2 doesn't include the exception's module name in tracebacks, but
# Python 3 does. Simulate this for doctests by setting __name__ to the
# exception's fully-qualified name.
from ludibrio.matcher import ParameterException
from ludibrio.mock import MockExpectationError
from ludibrio.spy import SpyExpectationError
ParameterException.__name__ = 'ludibrio.matcher.ParameterException'
MockExpectationError.__name__ = 'ludibrio.mock.MockExpectationError'
SpyExpectationError.__name__ = 'ludibrio.spy.SpyExpectationError'
9 changes: 6 additions & 3 deletions ludibrio/specification/unit/mock_msg_error.dt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Sample of generation of error for Mock.
=======================================

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

::

>>> from ludibrio import Mock
Expand All @@ -13,7 +16,7 @@ Sample of generation of error for Mock.
...
Traceback (most recent call last):
...
MockExpectationError: Call waiting: notcalled
ludibrio.mock.MockExpectationError: Call waiting: notcalled


::
Expand All @@ -26,7 +29,7 @@ Sample of generation of error for Mock.
>>> print greetings.hello('Gustavo Rezende')
Traceback (most recent call last):
...
MockExpectationError: Mock Object received unexpected call:hello
ludibrio.mock.MockExpectationError: Mock Object received unexpected call:hello


::
Expand All @@ -36,4 +39,4 @@ Sample of generation of error for Mock.
>>> greetings.excuse_me(name='Diego Manhaes')
Traceback (most recent call last):
...
MockExpectationError: Mock Object received unexpected call:excuse_me(name='Diego Manhaes')
ludibrio.mock.MockExpectationError: Mock Object received unexpected call:excuse_me(name='Diego Manhaes')
5 changes: 4 additions & 1 deletion ludibrio/specification/unit/mock_unordered.dt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Sample Mock unordered
=====================

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

::

>>> from ludibrio import Mock
Expand All @@ -17,4 +20,4 @@ Sample Mock unordered
>>> print greetings.hello('Gustavo')
Traceback (most recent call last):
...
MockExpectationError: Mock Object received unexpected call:hello
ludibrio.mock.MockExpectationError: Mock Object received unexpected call:hello
5 changes: 4 additions & 1 deletion ludibrio/specification/unit/specialarguments/contains.dt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
contains
~~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Verifies if an object is contains (contain) another. The contain and include matchers do exactly the same job::

>>> from ludibrio.matcher import contains
Expand All @@ -12,5 +15,5 @@ Verifies if an object is contains (contain) another. The contain and include mat
>>> contains("Hi") == "Hello, Gustavo Rezende"
Traceback (most recent call last):
...
ParameterException: 'Hello, Gustavo Rezende' is not contains 'Hi'
ludibrio.matcher.ParameterException: 'Hello, Gustavo Rezende' is not contains 'Hi'

5 changes: 4 additions & 1 deletion ludibrio/specification/unit/specialarguments/custom.dt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Custom matchers
---------------

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Extending the Ludibrio with custom matchers is very easy. The function name must be the name of the matcher. The function must have 2 parameters and it must return True.

In the example, when should fails, a message can be "4 is not the square root of 9"; in another way, if the fail is in a should_not, the message could be "3 is the square root of 9", if the expectation was be_the_square_root_of(9) == 3. The example is below::
Expand All @@ -19,4 +22,4 @@ In the example, when should fails, a message can be "4 is not the square root of
>>> 2 == the_square_root_of(4.1)
Traceback (most recent call last):
...
ParameterException: 2 is not the square root of 4.1...
ludibrio.matcher.ParameterException: 2 is not the square root of 4.1...
5 changes: 4 additions & 1 deletion ludibrio/specification/unit/specialarguments/ended_with.dt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
ended_with
~~~~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Verifies if a string ends with a given suffix::

>>> from ludibrio.matcher import *
Expand All @@ -12,5 +15,5 @@ Verifies if a string ends with a given suffix::
>>> 'hello motto' == ended_with('world')
Traceback (most recent call last):
...
ParameterException: 'hello motto' is not ended with 'world'
ludibrio.matcher.ParameterException: 'hello motto' is not ended with 'world'

5 changes: 4 additions & 1 deletion ludibrio/specification/unit/specialarguments/equal_to.dt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
equal_to
~~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Checks object equality (not identity)::


Expand All @@ -12,7 +15,7 @@ Checks object equality (not identity)::
>>> 2 == equal_to(3)
Traceback (most recent call last):
...
ParameterException: 2 is not equal to 3
ludibrio.matcher.ParameterException: 2 is not equal to 3
>>> name = 'dsl'
>>> name == equal_to('dsl')
True
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
equal_to_ignoring_case
~~~~~~~~~~~~~~~~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Checks equality of strings ignoring case::

>>> from ludibrio.matcher import *
Expand All @@ -18,12 +21,12 @@ Checks equality of strings ignoring case::
>>> 'I' == equal_to_ignoring_case('wi')
Traceback (most recent call last):
...
ParameterException: 'I' is not equal to 'wi' ignoring case
ludibrio.matcher.ParameterException: 'I' is not equal to 'wi' ignoring case

>>> 'I' == equal_to_ignoring_case('iw')
Traceback (most recent call last):
...
ParameterException: 'I' is not equal to 'iw' ignoring case
ludibrio.matcher.ParameterException: 'I' is not equal to 'iw' ignoring case

>>> 'Atenção' == equal_to_ignoring_case('ATENÇÃO')
True
Expand Down
7 changes: 5 additions & 2 deletions ludibrio/specification/unit/specialarguments/greater_than.dt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
greater_than
~~~~~~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Simply checks the given comparisons (>)::


Expand All @@ -12,11 +15,11 @@ Simply checks the given comparisons (>)::
>>> 1 == greater_than(1)
Traceback (most recent call last):
...
ParameterException: 1 is not greater than 1
ludibrio.matcher.ParameterException: 1 is not greater than 1
>>> 1 == greater_than(2)
Traceback (most recent call last):
...
ParameterException: 1 is not greater than 2
ludibrio.matcher.ParameterException: 1 is not greater than 2
>>> name = 'b'
>>> name == greater_than('a')
True
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
greater_than_or_equal_to
~~~~~~~~~~~~~~~~~~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Simply checks the given comparisons (>=)::


Expand All @@ -14,7 +17,7 @@ Simply checks the given comparisons (>=)::
>>> 1 == greater_than_or_equal_to(2)
Traceback (most recent call last):
...
ParameterException: 1 is not greater than or equal to 2
ludibrio.matcher.ParameterException: 1 is not greater than or equal to 2
>>> name = 'b'
>>> name == greater_than_or_equal_to('a')
True
Expand Down
9 changes: 6 additions & 3 deletions ludibrio/specification/unit/specialarguments/in_any_order.dt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
in_any_order
~~~~~~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Check if a iterable includes all elements of another::

>>> from ludibrio.matcher import in_any_order
Expand All @@ -12,21 +15,21 @@ Check if a iterable includes all elements of another::
>>> [1, 2, 3] == in_any_order([3, 4])
Traceback (most recent call last):
...
ParameterException: [1, 2, 3] does not have in any order [3, 4]
ludibrio.matcher.ParameterException: [1, 2, 3] does not have in any order [3, 4]

>>> [1, 2, 3] == in_any_order((3, 1))
True

>>> [1, 2, 3] == in_any_order((3, 4))
Traceback (most recent call last):
...
ParameterException: [1, 2, 3] does not have in any order (3, 4)
ludibrio.matcher.ParameterException: [1, 2, 3] does not have in any order (3, 4)

>>> 'should' == in_any_order(('s', 'd', 'l'))
True

>>> 'should' == in_any_order(('h', 'a'))
Traceback (most recent call last):
...
ParameterException: 'should' does not have in any order ('h', 'a')
ludibrio.matcher.ParameterException: 'should' does not have in any order ('h', 'a')

5 changes: 4 additions & 1 deletion ludibrio/specification/unit/specialarguments/instance_of.dt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
instance_of
~~~~~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Verifies if an object is of a given type::

>>> from ludibrio.matcher import instance_of
Expand All @@ -18,5 +21,5 @@ Verifies if an object is of a given type::
>>> foo == instance_of(Bar)
Traceback (most recent call last):
...
ParameterException: <...Foo object at ...> is not a instance of <class '...Bar'>
ludibrio.matcher.ParameterException: <...Foo object at ...> is not a instance of <class '...Bar'>

5 changes: 4 additions & 1 deletion ludibrio/specification/unit/specialarguments/into.dt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
into
~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Verifies if an object is contained (into) another. The contain and include matchers do exactly the same job::


Expand All @@ -11,7 +14,7 @@ Verifies if an object is contained (into) another. The contain and include match
>>> into([1,2,3]) == 5
Traceback (most recent call last):
...
ParameterException: 5 is not in [1, 2, 3]
ludibrio.matcher.ParameterException: 5 is not in [1, 2, 3]

>>> into([1,2,3]) == 3
True
5 changes: 4 additions & 1 deletion ludibrio/specification/unit/specialarguments/kind_of.dt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
kind_of
~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Verifies if an object is of a given type::

>>> from ludibrio.matcher import *
Expand All @@ -17,5 +20,5 @@ Verifies if an object is of a given type::
>>> foo == kind_of(Bar)
Traceback (most recent call last):
...
ParameterException: <...Foo object at ...> is not a kind of <class '...Bar'>
ludibrio.matcher.ParameterException: <...Foo object at ...> is not a kind of <class '...Bar'>

7 changes: 5 additions & 2 deletions ludibrio/specification/unit/specialarguments/less_than.dt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
less_than
~~~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Simply checks the given comparisons (<)::

>>> from ludibrio.matcher import less_than
Expand All @@ -11,11 +14,11 @@ Simply checks the given comparisons (<)::
>>> 2 == less_than(2)
Traceback (most recent call last):
...
ParameterException: 2 is not less than 2
ludibrio.matcher.ParameterException: 2 is not less than 2
>>> 2 == less_than(1)
Traceback (most recent call last):
...
ParameterException: 2 is not less than 1
ludibrio.matcher.ParameterException: 2 is not less than 1
>>> name = 'a'
>>> name == less_than('b')
True
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
less_than_or_equal_to
~~~~~~~~~~~~~~~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Simply checks the given comparisons (<=)::

>>> from ludibrio import less_than_or_equal_to
Expand All @@ -13,7 +16,7 @@ Simply checks the given comparisons (<=)::
>>> 2 == less_than_or_equal_to(1)
Traceback (most recent call last):
...
ParameterException: 2 is not less than or equal to 1
ludibrio.matcher.ParameterException: 2 is not less than or equal to 1
>>> name = 'a'
>>> name == less_than_or_equal_to('b')
True
Expand Down
5 changes: 4 additions & 1 deletion ludibrio/specification/unit/specialarguments/like.dt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
like
~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Checks matching against a regular expression::

>>> from ludibrio.matcher import like
Expand All @@ -11,4 +14,4 @@ Checks matching against a regular expression::
>>> '123 is a number' == like(r'^[12]+ is a number')
Traceback (most recent call last):
...
ParameterException: '123 is a number' is not like '^[12]+ is a number'
ludibrio.matcher.ParameterException: '123 is a number' is not like '^[12]+ is a number'
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Parameter matching
==================

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Ludibrio offers a very flexible way to match parameters in method calls. In simple cases, parameters are matched using basic equality. For instance::

>>> from ludibrio import *
Expand Down Expand Up @@ -49,5 +52,5 @@ In Stub::
>>> test.method('',2.0)
Traceback (most recent call last):
...
ParameterException: 2.0 is not a kind of <type 'int'>
ludibrio.matcher.ParameterException: 2.0 is not a kind of <type 'int'>

5 changes: 4 additions & 1 deletion ludibrio/specification/unit/specialarguments/started_with.dt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
started_with
~~~~~~~~~~~~

.. Python < 3 compatibility
>>> import ludibrio.specification.compat

Verifies if a string starts with a given prefix::

>>> from ludibrio.matcher import *
Expand All @@ -12,5 +15,5 @@ Verifies if a string starts with a given prefix::
>>> 'Hi world' == started_with('hello')
Traceback (most recent call last):
...
ParameterException: 'Hi world' is not started with 'hello'
ludibrio.matcher.ParameterException: 'Hi world' is not started with 'hello'

Loading

0 comments on commit a2ce435

Please sign in to comment.