forked from coala/coala-bears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClangComplexityBearTest.py
96 lines (84 loc) · 3.68 KB
/
ClangComplexityBearTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import unittest
from queue import Queue
from clang.cindex import Index
from coalib.results.Result import Result
from coalib.results.SourceRange import SourceRange
from coalib.settings.Section import Section
from bears.c_languages.ClangComplexityBear import (
ClangComplexityBear)
from coalib.testing.BearTestHelper import generate_skip_decorator
from coalib.testing.LocalBearTestHelper import execute_bear
@generate_skip_decorator(ClangComplexityBear)
class ClangComplexityBearTest(unittest.TestCase):
def setUp(self):
self.filename = os.path.abspath(os.path.join(os.path.dirname(__file__),
'codeclone_detection',
'conditions_samples.c'))
self.file = 'fake'
self.queue = Queue()
self.section = Section('test section')
self.bear = ClangComplexityBear(self.section, self.queue)
def test_calculation(self):
"""
Testing that number of decision points and exit points are calculated
correctly.
"""
expected = [
('used(int, int)', 3),
('returned(int, int)', 1),
('loopy(int, int)', 5),
('in_condition(int, int)', 1),
('assignation(int, int)', 2),
('arithmetics(int, int)', 1),
('levels(int, int, int)', 10),
('structing(struct test_struct, struct test_struct *)', 1),
('switching(int, int)', 2)]
root = Index.create().parse(self.filename).cursor
complexities_gen = self.bear.complexities(root, self.filename)
results = [(cursor.displayname, complexity)
for cursor, complexity in complexities_gen]
self.assertSequenceEqual(results, expected)
def test_output(self):
"""
Validating that the yielded results are correct.
"""
affected_code = (SourceRange.from_values(
self.filename,
start_line=111,
start_column=1,
end_line=143,
end_column=2),)
expected_result = Result(
self.bear,
"The function 'levels(int, int, int)' should be simplified. Its "
'cyclomatic complexity is 10 which exceeds maximal recommended '
'value of 8.',
affected_code=affected_code)
with execute_bear(self.bear, self.filename, self.file, 8) as out:
self.assertEqual(len(out), 1)
out[0].additional_info = '' # Let's not test this, static and huge
self.assertEqual(out[0], expected_result)
def test_empty_declared_function(self):
"""
Should not take into account and display empty function declarations.
"""
self.filename = os.path.abspath(os.path.join(os.path.dirname(__file__),
'test_files',
'empty_declarations.c'))
expected = [('with_body(int *)', 1)]
root = Index.create().parse(self.filename).cursor
complexities_gen = self.bear.complexities(root, self.filename)
results = [(cursor.displayname, complexity)
for cursor, complexity in complexities_gen]
self.assertSequenceEqual(results, expected)
def test_file_does_not_exist(self):
"""
Tests that bear throws TranslationUnitLoadError when file does not
exist.
"""
from clang.cindex import TranslationUnitLoadError
generator = self.bear.execute('not_existing', self.file)
self.assertNotEqual(generator, None)
with self.assertRaisesRegex(TranslationUnitLoadError, 'C value error'):
yield generator