File: testdbc9d.py

package info (click to toggle)
python-contract 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 268 kB
  • ctags: 305
  • sloc: python: 1,159; makefile: 56
file content (53 lines) | stat: -rwxr-xr-x 1,060 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
"""

Tests tightening preconditions... which should fail with an
InvalidPreconditionError

>>> b = Base()
>>> b.method(10)
90

>>> d = Derived()
>>> d.method(20)
200

>>> d.method(10)
Traceback (most recent call last):
 ...
PreconditionViolationError: ('testdbc9d.Derived.method', 6)
"""

from testdbc9b import Base

class Derived(Base):
    def method(self, a):
        """We cannot tighten preconditions, we must only be able to loosen
        them.  Otherwise, abstract classes cannot work.  The client thinks
        they're calling with a particular contract and Surprise!  it
        doesn't work now.

        pre: a > 10
        """
        return a * 10

def _test():
    import contract, doctest, testdbc9d
    contract.checkmod(testdbc9d, contract.CHECK_ALL)
    return doctest.testmod(testdbc9d)

if __name__ == '__main__':
    t = _test()
    if t[0]:
        print "test: %d/%d failures" % t
    else:
        print "test: %d successes" % t[1]

b = Base()
b.method(10)
b.method(6)

d = Derived()
d.method(20)
d.method(10)