Skip to content

Commit

Permalink
fix: process cases that multiple parsers found for given type, etc.
Browse files Browse the repository at this point in the history
process cases that multiple parsers found for given type, file extension
and so such as JSON and YAML. The previous code expects there is no such
cases and that causes exceptions.
  • Loading branch information
ssato committed Jan 11, 2019
1 parent 0e74d9e commit cff772b
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions tests/api.py
Expand Up @@ -17,6 +17,7 @@
import anyconfig.backend.json
import anyconfig.compat
import anyconfig.dicts
import anyconfig.processors
import anyconfig.template
import tests.common

Expand Down Expand Up @@ -84,17 +85,32 @@ def _is_file_object(obj):

class Test_10_find_loader(unittest.TestCase):

def _assert_isinstance(self, obj, cls, msg=False):
self.assertTrue(isinstance(obj, cls), msg or repr(obj))
psrs = anyconfig.backends.Parsers().list()

def _assert_isinstances(self, obj, clss, msg=False):
self.assertTrue(any(isinstance(obj, cls) for cls in clss),
msg or "%r vs %r" % (obj, clss))

def test_10_find_loader__w_parser_type_or_instance(self):
def _finds_by_type(typ):
fnc = anyconfig.processors.finds_with_pred
return fnc(lambda p: typ == p.type(), self.psrs)

def test_10_find_loader__w_parser_type_or_instance_or_by_file(self):
cpath = "dummy.conf"
for psr in anyconfig.backends.Parsers().list():
self._assert_isinstance(TT.find_loader(cpath, psr.type()), psr)
self._assert_isinstance(TT.find_loader(cpath, psr()), psr)
for psr in self.psrs:
ldrs = _finds_by_type(psr.type())
self._assert_isinstances(TT.find_loader(cpath, psr.type()), ldrs)
self._assert_isinstances(TT.find_loader(cpath, psr()), ldrs)

def test_20_find_loader__w_parser_by_file(self):
def _find_ldrs_by_ext(ext):
fnc = anyconfig.processors.finds_with_pred
return fnc(lambda p: ext in p.extensions(), self.psrs)

for psr in self.psrs:
for ext in psr.extensions():
self._assert_isinstance(TT.find_loader("dummy." + ext), psr,
"ext=%s, psr=%r" % (ext, psr))
ldrs = _find_ldrs_by_ext(ext)
self._assert_isinstances(TT.find_loader("dummy." + ext), ldrs)

def test_30_find_loader__unknown_parser_type(self):
self.assertRaises(TT.UnknownProcessorTypeError,
Expand Down

0 comments on commit cff772b

Please sign in to comment.