Completed
Pull Request — master (#1116)
by Lasse
01:41
created

setUp()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 9
rs 9.6667
1
import os
2
import sys
3
import unittest
4
from queue import Queue
5
6
sys.path.insert(0, ".")
7
8
from bears.c_languages.codeclone_detection.ClangFunctionDifferenceBear import (
9
    ClangFunctionDifferenceBear)
10
from bears.c_languages.codeclone_detection.ClangCloneDetectionBear import (
11
    ClangCloneDetectionBear)
12
from coalib.bearlib.parsing.clang.cindex import Index, LibclangError
13
from coalib.settings.Section import Section
14
from coalib.settings.Setting import Setting
15
16
17
class ClangCloneDetectionBearTest(unittest.TestCase):
18
    def setUp(self):
19
        self.base_test_path = os.path.abspath(os.path.join(
20
            os.path.dirname(__file__),
21
            "clone_detection_samples"))
22
        self.section = Section("default")
23
        self.section.append(Setting("files", "", origin=self.base_test_path))
24
        self.section.append(Setting("max_clone_difference", "0.308"))
25
        self.clone_files = [os.listdir(os.path.join(self.base_test_path,
26
                                                    "clones"))]
27
28
    def test_dependencies(self):
29
        self.assertIn(ClangFunctionDifferenceBear,
30
                      ClangCloneDetectionBear.get_dependencies())
31
32
    def test_configuration(self):
33
        self.section.append(Setting("average_calculation", "true"))
34
        self.section.append(Setting("poly_postprocessing", "false"))
35
        self.section.append(Setting("exp_postprocessing", "true"))
36
37
        self.clone_files = [
38
            os.path.join(self.base_test_path, "clones", "s4c.c")]
39
40
        # Ignore the results, it may be possible that it still passes :)
41
        self.check_clone_detection_bear(self.clone_files,
42
                                        lambda results, msg: True)
43
44
    def test_non_clones(self):
45
        self.non_clone_files = [
46
            os.path.join(self.base_test_path, "non_clones", elem)
47
            for elem in os.listdir(os.path.join(self.base_test_path,
48
                                                "non_clones"))]
49
50
        self.check_clone_detection_bear(self.non_clone_files,
51
                                        lambda results, msg:
52
                                        self.assertEqual(results, [], msg))
53
54
    def test_clones(self):
55
        self.clone_files = [
56
            os.path.join(self.base_test_path, "clones", elem)
57
            for elem in os.listdir(os.path.join(self.base_test_path,
58
                                                "clones"))]
59
60
        self.check_clone_detection_bear(self.clone_files,
61
                                        lambda results, msg:
62
                                        self.assertNotEqual(results, [], msg))
63
64
    def check_clone_detection_bear(self, files, result_check_function):
65
        """
66
        Checks the results of the CloneDetectionBear with the given function.
67
68
        :param files:                 The files to check. Each will be checked
69
                                      on its own.
70
        :param result_check_function: A function yielding an exception if the
71
                                      results are invalid.
72
        """
73
        for file in files:
74
            difference_results = ClangFunctionDifferenceBear(
75
                {file: ""},
76
                self.section,
77
                Queue()).run_bear_from_section([], {})
78
            uut = ClangCloneDetectionBear(
79
                {file: ""},
80
                self.section,
81
                Queue())
82
            arg_dict = {"dependency_results":
83
                        {ClangFunctionDifferenceBear.__name__:
84
                         list(difference_results)}}
85
86
            result_check_function(
87
                list(uut.run_bear_from_section([], arg_dict)),
88
                "while analyzing "+file)
89
90
91
def skip_test():
92
    try:
93
        Index.create()
94
        return False
95
    except LibclangError as error:
96
        return str(error)
97
98
99
if __name__ == '__main__':
100
    unittest.main(verbosity=2)
101