Completed
Push — master ( ef7389...74fe88 )
by Satoru
01:52
created

Test_30_make.test_40__pathlib()   A

Complexity

Conditions 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
c 1
b 1
f 0
dl 0
loc 14
rs 9.7
1
#
2
# Copyright (C) 2018 Satoru SATOH <ssato @ redhat.com>
3
# License: MIT
4
#
5
# pylint: disable=missing-docstring, invalid-name
6
import os.path
7
import unittest
8
9
import anyconfig.backends
10
import anyconfig.backend.ini
11
import anyconfig.backend.json
12
import anyconfig.compat
13
import anyconfig.ioinfo as TT
14
import anyconfig.utils
15
16
from anyconfig.globals import (
17
    IOI_PATH_STR, IOI_PATH_OBJ, IOI_STREAM,
18
)
19
20
21
IPATH_0 = os.path.join(os.path.dirname(__file__), "00-cnf.json")
22
IPATH_0_FULL = anyconfig.utils.normpath(IPATH_0)
23
24
25
class Test_10_inspect_io_obj(unittest.TestCase):
26
27
    def test_10_path_str(self):
28
        self.assertEqual(TT.inspect_io_obj(IPATH_0),
29
                         (IOI_PATH_STR, IPATH_0_FULL, open))
30
31
    def test_20_stream(self):
32
        self.assertEqual(TT.inspect_io_obj(open(IPATH_0)),
33
                         (IOI_STREAM, IPATH_0_FULL,
34
                          anyconfig.utils.noop))
35
36
    def test_30_path_obj(self):
37
        if anyconfig.compat.pathlib is None:
38
            return
39
40
        ipo = anyconfig.compat.pathlib.Path(IPATH_0)
41
        self.assertEqual(TT.inspect_io_obj(ipo),
42
                         (IOI_PATH_OBJ, IPATH_0_FULL, ipo.open))
43
44
45
class Test_30_make(unittest.TestCase):
46
    (ipath, ipath_full) = (IPATH_0, IPATH_0_FULL)
47
    prs = anyconfig.backends.PARSERS
48
49
    def __init__(self, *args, **kwargs):
50
        super(Test_30_make, self).__init__(*args, **kwargs)
51
        self.fun = TT.make
52
53
    def __checks_helper(self, inp, *args):
54
        self.assertEqual(inp.src, args[0])
55
        self.assertEqual(inp.path, args[1])
56
        self.assertEqual(inp.type, args[2])
57
        self.assertTrue(isinstance(inp.processor, args[3]))
58
        self.assertEqual(inp.opener, args[4])
59
60
    def test_20__forced_type(self):
61
        res = self.fun(None, anyconfig.backends.PARSERS, forced_type="ini")
62
        self.__checks_helper(res, None, None, None,
63
                             anyconfig.backend.ini.Parser,
64
                             anyconfig.utils.noop)
65
66
    def test_30__by_fileext(self):
67
        res = self.fun(self.ipath, self.prs)
68
        self.__checks_helper(res, self.ipath, self.ipath_full, IOI_PATH_STR,
69
                             anyconfig.backend.json.Parser, open)
70
71
    def test_40__pathlib(self):
72
        ipath = self.ipath
73
        if anyconfig.compat.pathlib is not None:
74
            # Replace w/ pathlib.Path object.
75
            ipath = anyconfig.compat.pathlib.Path(ipath)
76
            itype = IOI_PATH_OBJ
77
            opener = ipath.open
78
        else:
79
            itype = IOI_PATH_STR
80
            opener = open
81
82
        res = self.fun(ipath, self.prs)
83
        self.__checks_helper(res, ipath, self.ipath_full, itype,
84
                             anyconfig.backend.json.Parser, opener)
85
86
    def test_50__stream(self):
87
        ifo = open(self.ipath)
88
        res = self.fun(ifo, self.prs)
89
        self.__checks_helper(res, ifo, self.ipath_full, IOI_STREAM,
90
                             anyconfig.backend.json.Parser,
91
                             anyconfig.utils.noop)
92
93
# vim:sw=4:ts=4:et:
94