def test_factory_exception(): factory = Factory(Base, [test_factory_exception.__module__]) assert factory['validclass'] == ValidClass with raises(ValueError) as exc: factory.register(InvalidClass) assert "ValueError: Invalid class, not recognized as a Base" in str(exc)
def test_factory_exception(): factory = Factory(Base, [test_factory_exception.__module__]) assert factory['validclass'] == ValidClass with raises(ValueError) as exc: factory.register(InvalidClass) assert "Invalid class (must inherit from Base class)" in str(exc)
def test_nodoclog(caplog): factory = Factory(Base, [test_nodoclog.__module__]) for conf in factory: conf.docs pass assert caplog.record_tuples == [('benchmarkstt.factory', 30, "No docstring for 'validclass'")]
from benchmarkstt.factory import Factory class Base: def __init__(self, a='', b=''): raise NotImplementedError() def get_opcodes(self): """ Return list of 5-tuples describing how to turn `a` into `b`. Each tuple is of the form `(tag, i1, i2, j1, j2)`. The first tuple has `i1 == j1 == 0`, and remaining tuples have `i1` equals the `i2` from the tuple preceding it, and likewise for `j1` equals the previous `j2`. The tags are strings, with these meanings: - 'replace': `a[i1:i2]` should be replaced by `b[j1:j2]` - 'delete': `a[i1:i2]` should be deleted. Note that `j1==j2` in this case. - 'insert': `b[j1:j2]` should be inserted at `a[i1:i1]`. Note that `i1==i2` in this case. - 'equal': `a[i1:i2] == b[j1:j2]` """ raise NotImplementedError() factory = Factory(Base)
title = file if path is not None: file = os.path.join(path, file) with open(file, encoding=encoding) as f: self._normalizer = NormalizationComposite(title=title) for line in csv.reader(f): try: self._normalizer.add(normalizer(*line)) except TypeError as e: raise ValueError("%s:%d %r(%r) %r" % (file, line.lineno, normalizer, line, e)) def _normalize(self, text: str) -> str: return self._normalizer.normalize(text) factory = Factory(Base, _normalizer_namespaces) class FileFactory(Factory): def create(self, name, file=None, encoding=None, path=None): cls = super().__getitem__(name) return File(cls, file, encoding, path=path) def __getitem__(self, item): raise NotImplementedError("Not supported") file_factory = FileFactory(BaseWithFileSupport, _normalizer_namespaces)
def test_factory(): factory = Factory(Base, []) assert factory.is_valid(ValidClass) is True factory.register(ValidClass) assert factory['validclass'] == ValidClass factory['alias'] = ValidClass assert factory['alias'] == ValidClass assert type(factory.create('alias')) == ValidClass with raises(ValueError) as exc: factory.register(ValidClass) assert "Conflict: alias 'validclass' is already registered" in str(exc) with raises(ValueError) as exc: factory.register_namespace(module_name) assert "Conflict: alias 'validclass' is already registered" in str(exc) del factory[ValidClass] assert type(factory.create('alias')) == ValidClass with raises(ImportError) as exc: factory.create('validclass') assert "Could not find class" in str(exc) factory.unregister('alias') with raises(ImportError) as exc: factory.create('alias') assert "Could not find class" in str(exc)