Completed
Pull Request — master (#2295)
by Mischa
01:52
created

DocstyleDefinitionTest.test_load()   B

Complexity

Conditions 6

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
dl 0
loc 30
rs 7.5384
c 1
b 0
f 0
1
import unittest
2
3
from coalib.bearlib.languages.documentation.DocstyleDefinition import (
4
    DocstyleDefinition)
5
from coalib.misc.ContextManagers import make_temp
6
7
8
class DocstyleDefinitionTest(unittest.TestCase):
9
10
    def test_fail_instantation(self):
11
        with self.assertRaises(ValueError):
12
            DocstyleDefinition("PYTHON", "doxyGEN", (("##", "#"),))
13
14
        with self.assertRaises(ValueError):
15
            DocstyleDefinition("WEIRD-PY",
16
                               "schloxygen",
17
                               (("##+", "x", "y", "z"),))
18
19
        with self.assertRaises(ValueError):
20
            DocstyleDefinition("PYTHON",
21
                               "doxygen",
22
                               (("##", "", "#"), ('"""', '"""')))
23
24
        with self.assertRaises(TypeError):
25
            DocstyleDefinition(123, ["doxygen"], (('"""', '"""')))
26
27
    def test_properties(self):
28
        uut = DocstyleDefinition("C", "doxygen", (("/**", "*", "*/"),))
29
30
        self.assertEqual(uut.language, "c")
31
        self.assertEqual(uut.docstyle, "doxygen")
32
        self.assertEqual(uut.markers, (("/**", "*", "*/"),))
33
34
        uut = DocstyleDefinition("PYTHON", "doxyGEN", [("##", "", "#")])
35
36
        self.assertEqual(uut.language, "python")
37
        self.assertEqual(uut.docstyle, "doxygen")
38
        self.assertEqual(uut.markers, (("##", "", "#"),))
39
40
        uut = DocstyleDefinition("I2C",
41
                                 "my-custom-tool",
42
                                 (["~~", "/~", "/~"], (">!", ">>", ">>")))
43
44
        self.assertEqual(uut.language, "i2c")
45
        self.assertEqual(uut.docstyle, "my-custom-tool")
46
        self.assertEqual(uut.markers, (("~~", "/~", "/~"), (">!", ">>", ">>")))
47
48
        uut = DocstyleDefinition("Cpp", "doxygen", ("~~", "/~", "/~"))
49
50
        self.assertEqual(uut.language, "cpp")
51
        self.assertEqual(uut.docstyle, "doxygen")
52
        self.assertEqual(uut.markers, (("~~", "/~", "/~"),))
53
54
    def test_load(self):
55
        # Test unregistered docstyle.
56
        with self.assertRaises(FileNotFoundError):
57
            next(DocstyleDefinition.load("PYTHON", "INVALID"))
58
59
        # Test unregistered language in existing docstyle.
60
        with self.assertRaises(KeyError):
61
            next(DocstyleDefinition.load("bake-a-cake", "default"))
62
63
        # Test wrong argument type.
64
        with self.assertRaises(TypeError):
65
            next(DocstyleDefinition.load(123, ["list"]))
66
67
        # Test python 3 default configuration and if everything is parsed
68
        # right.
69
        result = DocstyleDefinition.load("PYTHON3", "default")
70
71
        self.assertEqual(result.language, "python3")
72
        self.assertEqual(result.docstyle, "default")
73
        self.assertEqual(result.markers, (('"""', '', '"""'),))
74
75
        # Test external coalang
76
        with make_temp() as coalang:
77
            with open(coalang, "w") as file:
78
                file.write("[COOL]\ndoc-markers = @@,@@,@@\n")
79
            result = DocstyleDefinition.load(
80
                                       "cool", "custom", coalang_path=coalang)
81
            self.assertEqual(result.language, "cool")
82
            self.assertEqual(result.docstyle, "custom")
83
            self.assertEqual(result.markers, (('@@', '@@', '@@'),))
84