Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:11
created

TextRangeJoinTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %
Metric Value
dl 0
loc 40
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A test_join() 0 18 1
A setUp() 0 6 1
A test_fails() 0 12 4
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):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unittest does not seem to be defined.
Loading history...
8
9
    def test_fail_instantation(self):
10
        with self.assertRaises(ValueError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
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):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TypeError does not seem to be defined.
Loading history...
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))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut does not seem to be defined.
Loading history...
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):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
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))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut does not seem to be defined.
Loading history...
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))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut2 does not seem to be defined.
Loading history...
62
        self.assertFalse(uut2.overlaps(uut1))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut1 does not seem to be defined.
Loading history...
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))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut2 does not seem to be defined.
Loading history...
73
        self.assertTrue(uut2.overlaps(uut1))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable uut1 does not seem to be defined.
Loading history...
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
91
class TextRangeJoinTest(unittest.TestCase):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unittest does not seem to be defined.
Loading history...
92
93
    def setUp(self):
94
        self.pos = [TextPosition(1, 1),
95
                    TextPosition(3, 1),
96
                    TextPosition(3, 3),
97
                    TextPosition(4, 3),
98
                    TextPosition(5, 3)]
99
100
    def test_fails(self):
101
        # need to pass ranges
102
        with self.assertRaises(TypeError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TypeError does not seem to be defined.
Loading history...
103
            TextRange.join(self.pos[0], self.pos[1])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
104
105
        with self.assertRaises(TypeError):
106
            TextRange.join(TextRange(self.pos[0], self.pos[1]), self.pos[1])
107
108
        # ranges must overlap
109
        with self.assertRaises(ValueError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
110
            TextRange.join(TextRange(self.pos[0], self.pos[1]),
111
                           TextRange(self.pos[3], self.pos[4]))
112
113
    def test_join(self):
114
        # overlap
115
        self.assertEqual(TextRange.join(TextRange(self.pos[0], self.pos[2]),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
116
                                        TextRange(self.pos[1], self.pos[3])),
117
                         TextRange(self.pos[0], self.pos[3]))
118
119
        self.assertEqual(TextRange.join(TextRange(self.pos[1], self.pos[3]),
120
                                        TextRange(self.pos[2], self.pos[4])),
121
                         TextRange(self.pos[1], self.pos[4]))
122
        # embrace
123
        self.assertEqual(TextRange.join(TextRange(self.pos[0], self.pos[3]),
124
                                        TextRange(self.pos[1], self.pos[2])),
125
                         TextRange(self.pos[0], self.pos[3]))
126
127
        # touch
128
        self.assertEqual(TextRange.join(TextRange(self.pos[1], self.pos[2]),
129
                                        TextRange(self.pos[2], self.pos[3])),
130
                         TextRange(self.pos[1], self.pos[3]))
131
132
133
class TextRangeExpandTest(unittest.TestCase):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unittest does not seem to be defined.
Loading history...
134
135
    def test_expand_full(self):
136
        empty_position = TextPosition()
137
        file = ["abc\n", "def\n", "ghi\n"]
138
        empty_range = TextRange(empty_position, empty_position)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable empty_position does not seem to be defined.
Loading history...
139
        full_range = TextRange.from_values(1, 1, 3, 4)
140
        self.assertEqual(empty_range.expand(file), full_range)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable full_range does not seem to be defined.
Loading history...
141
142
    def test_expand_none(self):
143
        start_position = TextPosition(2, 2)
144
        end_position = TextPosition(3, 2)
145
        file = ["abc\n", "def\n", "ghi\n"]
146
        text_range = TextRange(start_position, end_position)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable end_position does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable start_position does not seem to be defined.
Loading history...
147
        self.assertEqual(text_range.expand(file), text_range)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable text_range does not seem to be defined.
Loading history...
148
149
    def test_expand_semi(self):
150
        file = ["abc\n", "defg\n", "hijkl\n", "mnopqr\n"]
151
        semi_range = TextRange.from_values(2, None, 3, None)
152
        full_range = TextRange.from_values(2, 1, 3, 6)
153
        self.assertEqual(semi_range.expand(file), full_range)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable full_range does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable file does not seem to be defined.
Loading history...
154