Failed Conditions
Pull Request — master (#1182)
by Lasse
02:02 queued 26s
created

bears.tests.c_languages.codeclone_detection.skip_test()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

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