Completed
Push — master ( 42cdfe...147541 )
by
unknown
02:00
created

TextRangeTest.test_no_overlap()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
import unittest
2
3
from coalib.results.TextPosition import TextPosition
4
from coalib.results.TextRange import TextRange
5
6
7
class TextRangeTest(unittest.TestCase):
8
9
    def test_fail_instantation(self):
10
        with self.assertRaises(ValueError):
11
            TextRange(TextPosition(3, 4), TextPosition(2, 8))
12
13
        with self.assertRaises(ValueError):
14
            TextRange(TextPosition(0, 10), TextPosition(0, 7))
15
16
        with self.assertRaises(TypeError):
17
            TextRange(None, TextPosition(20, 80))
18
19
        with self.assertRaises(TypeError):
20
            TextRange("string", TextPosition(200, 800))
21
22
        with self.assertRaises(TypeError):
23
            TextRange(TextPosition(5, 0), "schtring")
24
25
    def test_properties(self):
26
        uut = TextRange(TextPosition(7, 2), TextPosition(7, 3))
27
        self.assertEqual(uut.start, TextPosition(7, 2))
28
        self.assertEqual(uut.end, TextPosition(7, 3))
29
30
        uut = TextRange(TextPosition(70, 20), None)
31
        self.assertEqual(uut.start, TextPosition(70, 20))
32
        self.assertEqual(uut.end, TextPosition(70, 20))
33
        self.assertEqual(uut.start, uut.end)
34
        self.assertIsNot(uut.start, uut.end)
35
36
    def test_from_values(self):
37
        # Check if invalid ranges still fail.
38
        with self.assertRaises(ValueError):
39
            TextRange.from_values(0, 10, 0, 7)
40
41
        uut = TextRange.from_values(1, 0, 7, 3)
42
        self.assertEqual(uut.start, TextPosition(1, 0))
43
        self.assertEqual(uut.end, TextPosition(7, 3))
44
45
        uut = TextRange.from_values(1, 0, None, 88)
46
        self.assertEqual(uut.start, TextPosition(1, 0))
47
        self.assertEqual(uut.end, TextPosition(1, 0))
48
49
        uut = TextRange.from_values(1, 0, 7, None)
50
        self.assertEqual(uut.start, TextPosition(1, 0))
51
        self.assertEqual(uut.end, TextPosition(7, None))
52
53
        # Test defaults.
54
        uut = TextRange.from_values()
55
        self.assertEqual(uut.start, TextPosition(None, None))
56
        self.assertEqual(uut.end, TextPosition(None, None))
57
58
    def test_no_overlap(self):
59
        uut1 = TextRange.from_values(2, None, 3)
60
        uut2 = TextRange.from_values(4, None, 5)
61
        self.assertFalse(uut1.overlaps(uut2))
62
        self.assertFalse(uut2.overlaps(uut1))
63
64
        uut1 = TextRange.from_values(2, None, 3, 6)
65
        uut2 = TextRange.from_values(3, 7, 5)
66
        self.assertFalse(uut1.overlaps(uut2))
67
        self.assertFalse(uut2.overlaps(uut1))
68
69
    def test_overlap(self):
70
        uut1 = TextRange.from_values(2, None, 3)
71
        uut2 = TextRange.from_values(3, None, 5)
72
        self.assertTrue(uut1.overlaps(uut2))
73
        self.assertTrue(uut2.overlaps(uut1))
74
75
        uut1 = TextRange.from_values(2, None, 3, 6)
76
        uut2 = TextRange.from_values(3, 6, 5)
77
        self.assertTrue(uut1.overlaps(uut2))
78
        self.assertTrue(uut2.overlaps(uut1))
79
80
        uut1 = TextRange.from_values(2, None, 7)
81
        uut2 = TextRange.from_values(3, None, 5)
82
        self.assertTrue(uut1.overlaps(uut2))
83
        self.assertTrue(uut2.overlaps(uut1))
84
85
        uut1 = TextRange.from_values(5, None, 7)
86
        uut2 = TextRange.from_values(3, None, 6)
87
        self.assertTrue(uut1.overlaps(uut2))
88
        self.assertTrue(uut2.overlaps(uut1))
89
90
    def test_contains(self):
91
        range_a = TextRange.from_values(1, 1, 1, 19)
92
        range_b = TextRange.from_values(1, 1, 1, 20)
93
94
        self.assertIn(range_a, range_b)
95
96
        range_a = TextRange.from_values(1, 1, 1, 21)
97
        range_b = TextRange.from_values(1, 1, 1, 20)
98
99
        self.assertNotIn(range_a, range_b)
100
101
        range_a = TextRange.from_values(1, 5, 1, 5)
102
        range_b = TextRange.from_values(1, 1, 1, 20)
103
104
        self.assertIn(range_a, range_b)
105
106
        range_a = TextRange.from_values(1, 1, 1, 18)
107
        range_b = TextRange.from_values(1, 14, 1, 20)
108
109
        self.assertNotIn(range_a, range_b)
110
111
        range_a = TextRange.from_values(1, 1, 1, 20)
112
        range_b = TextRange.from_values(1, 1, 1, 20)
113
114
        self.assertIn(range_a, range_b)
115
116
117
class TextRangeJoinTest(unittest.TestCase):
118
119
    def setUp(self):
120
        self.pos = [TextPosition(1, 1),
121
                    TextPosition(3, 1),
122
                    TextPosition(3, 3),
123
                    TextPosition(4, 3),
124
                    TextPosition(5, 3)]
125
126
    def test_fails(self):
127
        # need to pass ranges
128
        with self.assertRaises(TypeError):
129
            TextRange.join(self.pos[0], self.pos[1])
130
131
        with self.assertRaises(TypeError):
132
            TextRange.join(TextRange(self.pos[0], self.pos[1]), self.pos[1])
133
134
        # ranges must overlap
135
        with self.assertRaises(ValueError):
136
            TextRange.join(TextRange(self.pos[0], self.pos[1]),
137
                           TextRange(self.pos[3], self.pos[4]))
138
139
    def test_join(self):
140
        # overlap
141
        self.assertEqual(TextRange.join(TextRange(self.pos[0], self.pos[2]),
142
                                        TextRange(self.pos[1], self.pos[3])),
143
                         TextRange(self.pos[0], self.pos[3]))
144
145
        self.assertEqual(TextRange.join(TextRange(self.pos[1], self.pos[3]),
146
                                        TextRange(self.pos[2], self.pos[4])),
147
                         TextRange(self.pos[1], self.pos[4]))
148
        # embrace
149
        self.assertEqual(TextRange.join(TextRange(self.pos[0], self.pos[3]),
150
                                        TextRange(self.pos[1], self.pos[2])),
151
                         TextRange(self.pos[0], self.pos[3]))
152
153
        # touch
154
        self.assertEqual(TextRange.join(TextRange(self.pos[1], self.pos[2]),
155
                                        TextRange(self.pos[2], self.pos[3])),
156
                         TextRange(self.pos[1], self.pos[3]))
157
158
159
class TextRangeExpandTest(unittest.TestCase):
160
161
    def test_expand_full(self):
162
        empty_position = TextPosition()
163
        file = ["abc\n", "def\n", "ghi\n"]
164
        empty_range = TextRange(empty_position, empty_position)
165
        full_range = TextRange.from_values(1, 1, 3, 4)
166
        self.assertEqual(empty_range.expand(file), full_range)
167
168
    def test_expand_none(self):
169
        start_position = TextPosition(2, 2)
170
        end_position = TextPosition(3, 2)
171
        file = ["abc\n", "def\n", "ghi\n"]
172
        text_range = TextRange(start_position, end_position)
173
        self.assertEqual(text_range.expand(file), text_range)
174
175
    def test_expand_semi(self):
176
        file = ["abc\n", "defg\n", "hijkl\n", "mnopqr\n"]
177
        semi_range = TextRange.from_values(2, None, 3, None)
178
        full_range = TextRange.from_values(2, 1, 3, 6)
179
        self.assertEqual(semi_range.expand(file), full_range)
180