forked from asottile/babi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreg_test.py
74 lines (53 loc) · 2.41 KB
/
reg_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import onigurumacffi
import pytest
from babi.reg import _Reg
from babi.reg import _RegSet
def test_reg_first_line():
reg = _Reg(r'\Ahello')
assert reg.match('hello', 0, first_line=True, boundary=True)
assert reg.search('hello', 0, first_line=True, boundary=True)
assert not reg.match('hello', 0, first_line=False, boundary=True)
assert not reg.search('hello', 0, first_line=False, boundary=True)
def test_reg_boundary():
reg = _Reg(r'\Ghello')
assert reg.search('ohello', 1, first_line=True, boundary=True)
assert reg.match('ohello', 1, first_line=True, boundary=True)
assert not reg.search('ohello', 1, first_line=True, boundary=False)
assert not reg.match('ohello', 1, first_line=True, boundary=False)
def test_reg_neither():
reg = _Reg(r'(\A|\G)hello')
assert not reg.search('hello', 0, first_line=False, boundary=False)
assert not reg.search('ohello', 1, first_line=False, boundary=False)
def test_reg_other_escapes_left_untouched():
reg = _Reg(r'(^|\A|\G)\w\s\w')
assert reg.match('a b', 0, first_line=False, boundary=False)
def test_reg_not_out_of_bounds_at_end():
# the only way this is triggerable is with an illegal regex, we'd rather
# produce an error about the regex being wrong than an IndexError
reg = _Reg('\\A\\')
with pytest.raises(onigurumacffi.OnigError) as excinfo:
reg.search('\\', 0, first_line=False, boundary=False)
msg, = excinfo.value.args
assert msg == 'end pattern at escape'
def test_reg_repr():
assert repr(_Reg(r'\A123')) == r"_Reg('\\A123')"
def test_regset_first_line():
regset = _RegSet(r'\Ahello', 'hello')
idx, _ = regset.search('hello', 0, first_line=True, boundary=True)
assert idx == 0
idx, _ = regset.search('hello', 0, first_line=False, boundary=True)
assert idx == 1
def test_regset_boundary():
regset = _RegSet(r'\Ghello', 'hello')
idx, _ = regset.search('ohello', 1, first_line=True, boundary=True)
assert idx == 0
idx, _ = regset.search('ohello', 1, first_line=True, boundary=False)
assert idx == 1
def test_regset_neither():
regset = _RegSet(r'\Ahello', r'\Ghello', 'hello')
idx, _ = regset.search('hello', 0, first_line=False, boundary=False)
assert idx == 2
idx, _ = regset.search('ohello', 1, first_line=False, boundary=False)
assert idx == 2
def test_regset_repr():
assert repr(_RegSet('ohai', r'\Aworld')) == r"_RegSet('ohai', '\\Aworld')"