|
1
|
|
|
# |
|
2
|
|
|
# Copyright (C) 2012 - 2015 Satoru SATOH <ssato @ redhat.com> |
|
3
|
|
|
# License: MIT |
|
4
|
|
|
# |
|
5
|
|
|
# pylint: disable=missing-docstring, invalid-name, protected-access |
|
6
|
|
|
from __future__ import absolute_import |
|
7
|
|
|
|
|
8
|
|
|
import os.path |
|
9
|
|
|
import unittest |
|
10
|
|
|
import anyconfig.backend.json |
|
11
|
|
|
import anyconfig.backends as TT |
|
12
|
|
|
import anyconfig.ioinfo |
|
13
|
|
|
|
|
14
|
|
|
from anyconfig.compat import pathlib |
|
15
|
|
|
from anyconfig.globals import UnknownParserTypeError, UnknownFileTypeError |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
CNF_PATH = os.path.join(os.path.dirname(__file__), "00-cnf.json") |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class Test(unittest.TestCase): |
|
22
|
|
|
|
|
23
|
|
|
def test_10_list_types(self): |
|
24
|
|
|
types = TT.list_types() |
|
25
|
|
|
|
|
26
|
|
|
self.assertTrue(isinstance(types, list)) |
|
27
|
|
|
self.assertTrue(bool(list)) # ensure it's not empty. |
|
28
|
|
|
|
|
29
|
|
|
def test_20_find_parser_by_type__ng_cases(self): |
|
30
|
|
|
self.assertRaises(ValueError, TT.find_parser_by_type, None) |
|
31
|
|
|
self.assertRaises(UnknownParserTypeError, TT.find_parser_by_type, |
|
32
|
|
|
"_unkonw_type_") |
|
33
|
|
|
|
|
34
|
|
|
def test_22_find_parser_by_type(self): |
|
35
|
|
|
self.assertTrue(isinstance(TT.find_parser_by_type("json"), |
|
36
|
|
|
anyconfig.backend.json.Parser)) |
|
37
|
|
|
|
|
38
|
|
|
def test_30_find_parser_ng_cases(self): |
|
39
|
|
|
self.assertRaises(ValueError, TT.find_parser, None) |
|
40
|
|
|
self.assertRaises(UnknownParserTypeError, TT.find_parser, None, |
|
41
|
|
|
"_unkonw_type_") |
|
42
|
|
|
self.assertRaises(UnknownFileTypeError, TT.find_parser, |
|
43
|
|
|
"cnf.unknown_ext") |
|
44
|
|
|
|
|
45
|
|
|
def test_32_find_parser_ng_cases(self): |
|
46
|
|
|
pcls = anyconfig.backend.json.Parser |
|
47
|
|
|
self.assertTrue(isinstance(TT.find_parser("x.conf", |
|
48
|
|
|
forced_type="json"), |
|
49
|
|
|
pcls)) |
|
50
|
|
|
self.assertTrue(isinstance(TT.find_parser("x.json"), pcls)) |
|
51
|
|
|
|
|
52
|
|
|
cnf = os.path.join(os.path.dirname(__file__), "00-cnf.json") |
|
53
|
|
|
with open(cnf) as inp: |
|
54
|
|
|
self.assertTrue(isinstance(TT.find_parser(inp), pcls)) |
|
55
|
|
|
|
|
56
|
|
|
if pathlib is not None: |
|
57
|
|
|
inp = pathlib.Path("x.json") |
|
58
|
|
|
self.assertTrue(isinstance(TT.find_parser(inp), pcls)) |
|
59
|
|
|
|
|
60
|
|
|
def test_34_find_parser__input_object(self): |
|
61
|
|
|
inp = anyconfig.ioinfo.make(CNF_PATH, |
|
62
|
|
|
TT._PARSERS_BY_EXT, TT._PARSERS_BY_TYPE) |
|
63
|
|
|
psr = TT.find_parser(inp) |
|
64
|
|
|
self.assertTrue(isinstance(psr, anyconfig.backend.json.Parser)) |
|
65
|
|
|
|
|
66
|
|
|
# vim:sw=4:ts=4:et: |
|
67
|
|
|
|