v / vlib / regex / regex_anchor_test.v
44 lines · 38 sloc · 1.01 KB · 775fd657b38becbbbec2cf71f7d91618b2dabab2
Raw
1import regex
2
3fn test_anchor_start() {
4 mut re := regex.regex_opt(r'^\w+') or { panic(err) }
5 start, end := re.find('id.')
6 assert start == 0
7 assert end == 2
8}
9
10fn test_anchor_end() {
11 mut re := regex.regex_opt(r'\w+$') or { panic(err) }
12 start, end := re.find('(id')
13 assert start == 1
14 assert end == 3
15}
16
17fn test_anchor_both() {
18 mut re := regex.regex_opt(r'^\w+$') or { panic(err) }
19 start, end := re.find('(id)')
20 assert start == -1
21 assert end == -1
22}
23
24fn test_anchor_both_find_multiline() {
25 text := 'TITLE\n\nThis is a test.'
26 mut re := regex.regex_opt(r'^\w+$') or { panic(err) }
27 start, end := re.find(text)
28 assert start == 0
29 assert end == 5
30}
31
32fn test_anchor_both_find_all_multiline() {
33 text := 'TITLE\n\nThis is a test.'
34 mut re := regex.regex_opt(r'^\w+$') or { panic(err) }
35 res := re.find_all(text)
36 assert res == [0, 5]
37}
38
39fn test_anchor_both_find_all_str_multiline() {
40 text := 'TITLE\n\nThis is a test.'
41 mut re := regex.regex_opt(r'^\w+$') or { panic(err) }
42 res := re.find_all_str(text)
43 assert res == ['TITLE']
44}
45