Completed
Pull Request — master (#2186)
by Udayan
01:54
created

ShowPatchActionTest.test_apply_with_rename()   A

Complexity

Conditions 2

Size

Total Lines 20

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 20
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_with_previous_patches(self):
52
        with retrieve_stdout() as stdout:
53
            previous_diffs = {"a": Diff(self.file_dict['a'])}
54
            previous_diffs["a"].change_line(2, "b\n", "b_changed\n")
55
            self.assertEqual(self.uut.apply_from_section(self.test_result,
56
                                                         self.file_dict,
57
                                                         previous_diffs,
58
                                                         self.section),
59
                             previous_diffs)
60
            self.assertEqual(stdout.getvalue(),
61
                             "|----|    | a\n"
62
                             "|    |++++| a\n"
63
                             "|   1|   1| a\n"
64
                             "|    |   2|+test\n"
65
                             "|   2|   3| b_changed\n"
66
                             "|   3|    |-c\n"
67
                             "|----|    | b\n"
68
                             "|    |++++| b\n"
69
                             "|    |   1|+first\n"
70
                             "|   1|   2| old_first\n")
71
72
    def test_apply_with_rename(self):
73
        with retrieve_stdout() as stdout:
74
            previous_diffs = {"a": Diff(self.file_dict['a'])}
75
            previous_diffs["a"].change_line(2, "b\n", "b_changed\n")
76
77
            diff_dict = {"a": Diff(self.file_dict['a'], rename="a.rename"),
78
                         "b": Diff(self.file_dict['b'], delete=True)}
79
            diff_dict["a"].add_lines(1, ["test\n"])
80
            diff_dict["a"].delete_line(3)
81
            diff_dict["b"].add_lines(0, ["first\n"])
82
83
            test_result = Result("origin", "message", diffs=diff_dict)
84
85
            self.assertEqual(self.uut.apply_from_section(test_result,
86
                                                         self.file_dict,
87
                                                         previous_diffs,
88
                                                         self.section),
89
                             previous_diffs)
90
            self.assertEqual(stdout.getvalue(),
91
                             "|----|    | a\n"
92
                             "|    |++++| a.rename\n"
93
                             "|   1|   1| a\n"
94
                             "|    |   2|+test\n"
95
                             "|   2|   3| b_changed\n"
96
                             "|   3|    |-c\n"
97
                             "|----|    | b\n"
98
                             "|    |++++| /dev/null\n"
99
                             "|   1|    |-old_first\n")
100