sync to Atlas repo

This commit is contained in:
Luciano Ramalho
2014-12-06 15:38:22 -02:00
parent 2f495627fb
commit b38e6fc5f2
7 changed files with 72 additions and 43 deletions

View File

@@ -1,17 +1,12 @@
from abc import ABC, abstractmethod
import abc
class Tombola(abc.ABC): # <1>
class Tombola(ABC): # <1>
@abstractmethod
def __init__(self, iterable): # <2>
"""New instance is loaded from an iterable."""
@abstractmethod
def load(self, iterable):
@abc.abstractmethod
def load(self, iterable): # <2>
"""Add items from an iterable."""
@abstractmethod
@abc.abstractmethod
def pick(self): # <3>
"""Remove item at random, returning it.

View File

@@ -1,33 +1,36 @@
import sys
import importlib
# BEGIN TOMBOLA_RUNNER
import doctest
from tombola import Tombola
# modules to test
import bingo, lotto, tombolist, drum # <1>
TEST_FILE = 'tombola_tests.rst'
MODULE_NAMES = 'bingo lotto tombolist drum'.split()
TEST_MSG = '{0:16} {1.attempted:2} tests, {1.failed:2} failed - {2}'
def main(argv):
verbose = '-v' in argv
real_subclasses = Tombola.__subclasses__() # <2>
virtual_subclasses = list(Tombola._abc_registry) # <3>
for cls in real_subclasses + virtual_subclasses: # <4>
test(cls, verbose)
def test(cls, verbose=False):
res = doctest.testfile(TEST_FILE,
globs={'TombolaUnderTest': cls},
verbose=verbose,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
res = doctest.testfile(
TEST_FILE,
globs={'ConcreteTombola': cls}, # <5>
verbose=verbose,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
tag = 'FAIL' if res.failed else 'OK'
print(TEST_MSG.format(cls.__name__, res, tag))
print(TEST_MSG.format(cls.__name__, res, tag)) # <6>
if __name__ == '__main__':
for name in MODULE_NAMES: # import modules to test, by name
importlib.import_module(name)
verbose = '-v' in sys.argv
real_subclasses = Tombola.__subclasses__()
virtual_subclasses = list(Tombola._abc_registry)
for cls in real_subclasses + virtual_subclasses:
test(cls, verbose)
import sys
main(sys.argv)
# END TOMBOLA_RUNNER

View File

@@ -8,7 +8,7 @@ Every concrete subclass of Tombola should pass these tests.
Create and load instance from iterable::
>>> balls = list(range(3))
>>> globe = TombolaUnderTest(balls)
>>> globe = ConcreteTombola(balls)
>>> globe.loaded()
True
@@ -42,7 +42,7 @@ Reload::
Check that `LookupError` (or a subclass) is the exception
thrown when the device is empty::
>>> globe = TombolaUnderTest([])
>>> globe = ConcreteTombola([])
>>> try:
... globe.pick()
... except LookupError as exc:
@@ -53,7 +53,7 @@ thrown when the device is empty::
Load and pick 100 balls to verify that they are all come out::
>>> balls = list(range(100))
>>> globe = TombolaUnderTest(balls)
>>> globe = ConcreteTombola(balls)
>>> picks = []
>>> while globe.loaded():
... picks.append(globe.pick())
@@ -75,3 +75,6 @@ even if the implementation is OK. The probability of the 100
balls coming out, by chance, in the order they were loaded is
1/100!, or approximately 1.07e-158. It's much easier to win the
Lotto or to become a billionaire working as a programmer.
THE END

View File

@@ -16,6 +16,4 @@ class TomboList(list): # <2>
def loaded(self): return bool(self) # <6>
"""
Tombola.register(TomboList) # <- Python 3.2 or earlier
"""
# Tombola.register(TomboList) # <- Python 3.2 or earlier