Completed
Pull Request — master (#2201)
by Lasse
01:47
created

ShowPatchActionTest.test_apply_renaming_only()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
1
import unittest
2
3
from coalib.misc.ContextManagers import retrieve_stdout
4
from coalib.results.Diff import Diff
5
from coalib.results.Result import Result
6
from coalib.results.result_actions.ShowPatchAction import ShowPatchAction
7
from coalib.settings.Section import Section, Setting
8
9
10
class ShowPatchActionTest(unittest.TestCase):
11
12
    def setUp(self):
13
        self.uut = ShowPatchAction()
14
        self.file_dict = {"a": ["a\n", "b\n", "c\n"], "b": ["old_first\n"]}
15
        self.diff_dict = {"a": Diff(self.file_dict['a']),
16
                          "b": Diff(self.file_dict['b'])}
17
        self.diff_dict["a"].add_lines(1, ["test\n"])
18
        self.diff_dict["a"].delete_line(3)
19
        self.diff_dict["b"].add_lines(0, ["first\n"])
20
21
        self.test_result = Result("origin", "message", diffs=self.diff_dict)
22
        self.section = Section("name")
23
        self.section.append(Setting("colored", "false"))
24
25
    def test_is_applicable(self):
26
        self.assertFalse(self.uut.is_applicable(1, None, None))
27
        self.assertFalse(self.uut.is_applicable(Result("o", "m"), None, None))
28
        self.assertTrue(self.uut.is_applicable(self.test_result, {}, {}))
29
        self.assertFalse(self.uut.is_applicable(self.test_result, {},
30
                                                self.diff_dict))
31
32
    def test_apply(self):
33
        with retrieve_stdout() as stdout:
34
            self.assertEqual(self.uut.apply_from_section(self.test_result,
35
                                                         self.file_dict,
36
                                                         {},
37
                                                         self.section),
38
                             {})
39
            self.assertEqual(stdout.getvalue(),
40
                             "|----|    | a\n"
41
                             "|    |++++| a\n"
42
                             "|   1|   1| a\n"
43
                             "|    |   2|+test\n"
44
                             "|   2|   3| b\n"
45
                             "|   3|    |-c\n"
46
                             "|----|    | b\n"
47
                             "|    |++++| b\n"
48
                             "|    |   1|+first\n"
49
                             "|   1|   2| old_first\n")
50
51
    def test_apply_renaming_only(self):
52
        with retrieve_stdout() as stdout:
53
            test_result = Result("origin", "message",
54
                                 diffs={'a': Diff([], rename='b')})
55
            file_dict = {'a': []}
56
            self.assertEqual(self.uut.apply_from_section(test_result,
57
                                                         file_dict,
58
                                                         {},
59
                                                         self.section),
60
                             {})
61
            self.assertEqual(stdout.getvalue(),
62
                             '|----|    | a/a\n'
63
                             '|    |++++| b/b\n')
64
65
    def test_apply_with_previous_patches(self):
66
        with retrieve_stdout() as stdout:
67
            previous_diffs = {"a": Diff(self.file_dict['a'])}
68
            previous_diffs["a"].change_line(2, "b\n", "b_changed\n")
69
            self.assertEqual(self.uut.apply_from_section(self.test_result,
70
                                                         self.file_dict,
71
                                                         previous_diffs,
72
                                                         self.section),
73
                             previous_diffs)
74
            self.assertEqual(stdout.getvalue(),
75
                             "|----|    | a\n"
76
                             "|    |++++| a\n"
77
                             "|   1|   1| a\n"
78
                             "|    |   2|+test\n"
79
                             "|   2|   3| b_changed\n"
80
                             "|   3|    |-c\n"
81
                             "|----|    | b\n"
82
                             "|    |++++| b\n"
83
                             "|    |   1|+first\n"
84
                             "|   1|   2| old_first\n")
85
86
    def test_apply_with_rename(self):
87
        with retrieve_stdout() as stdout:
88
            previous_diffs = {"a": Diff(self.file_dict['a'])}
89
            previous_diffs["a"].change_line(2, "b\n", "b_changed\n")
90
91
            diff_dict = {"a": Diff(self.file_dict['a'], rename="a.rename"),
92
                         "b": Diff(self.file_dict['b'], delete=True)}
93
            diff_dict["a"].add_lines(1, ["test\n"])
94
            diff_dict["a"].delete_line(3)
95
            diff_dict["b"].add_lines(0, ["first\n"])
96
97
            test_result = Result("origin", "message", diffs=diff_dict)
98
99
            self.assertEqual(self.uut.apply_from_section(test_result,
100
                                                         self.file_dict,
101
                                                         previous_diffs,
102
                                                         self.section),
103
                             previous_diffs)
104
            self.assertEqual(stdout.getvalue(),
105
                             "|----|    | a\n"
106
                             "|    |++++| a.rename\n"
107
                             "|   1|   1| a\n"
108
                             "|    |   2|+test\n"
109
                             "|   2|   3| b_changed\n"
110
                             "|   3|    |-c\n"
111
                             "|----|    | b\n"
112
                             "|    |++++| /dev/null\n"
113
                             "|   1|    |-old_first\n")
114