|
1
|
|
|
import sys |
|
2
|
|
|
import unittest |
|
3
|
|
|
|
|
4
|
|
|
sys.path.insert(0, ".") |
|
5
|
|
|
from coalib.results.TextPosition import TextPosition |
|
6
|
|
|
from coalib.results.TextRange import TextRange |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class TextRangeTest(unittest.TestCase): |
|
10
|
|
|
def test_fail_instantation(self): |
|
11
|
|
|
with self.assertRaises(ValueError): |
|
12
|
|
|
TextRange(TextPosition(3, 4), TextPosition(2, 8)) |
|
13
|
|
|
|
|
14
|
|
|
with self.assertRaises(ValueError): |
|
15
|
|
|
TextRange(TextPosition(0, 10), TextPosition(0, 7)) |
|
16
|
|
|
|
|
17
|
|
|
with self.assertRaises(TypeError): |
|
18
|
|
|
TextRange(None, TextPosition(20, 80)) |
|
19
|
|
|
|
|
20
|
|
|
with self.assertRaises(TypeError): |
|
21
|
|
|
TextRange("string", TextPosition(200, 800)) |
|
22
|
|
|
|
|
23
|
|
|
with self.assertRaises(TypeError): |
|
24
|
|
|
TextRange(TextPosition(5, 0), "schtring") |
|
25
|
|
|
|
|
26
|
|
|
def test_properties(self): |
|
27
|
|
|
uut = TextRange(TextPosition(7, 2), TextPosition(7, 3)) |
|
28
|
|
|
self.assertEqual(uut.start, TextPosition(7, 2)) |
|
29
|
|
|
self.assertEqual(uut.end, TextPosition(7, 3)) |
|
30
|
|
|
|
|
31
|
|
|
uut = TextRange(TextPosition(70, 20), None) |
|
32
|
|
|
self.assertEqual(uut.start, TextPosition(70, 20)) |
|
33
|
|
|
self.assertEqual(uut.end, TextPosition(70, 20)) |
|
34
|
|
|
self.assertIs(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
|
|
|
|
|
59
|
|
|
if __name__ == '__main__': |
|
60
|
|
|
unittest.main(verbosity=2) |
|
61
|
|
|
|