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