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

ShowPatchActionTest.test_apply_empty()   A

Complexity

Conditions 2

Size

Total Lines 11

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