File: regexp_re2_test.go

package info (click to toggle)
golang-github-dlclark-regexp2 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 560 kB
  • sloc: makefile: 3
file content (98 lines) | stat: -rw-r--r-- 2,312 bytes parent folder | download
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package regexp2

import "testing"

func TestRE2CompatCapture(t *testing.T) {
	r := MustCompile(`re(?P<a>2)`, RE2)
	if m, err := r.FindStringMatch("blahre2blah"); m == nil {
		t.Fatal("Expected match")
	} else if err != nil {
		t.Fatalf("Unexpected error: %v", err)
	} else {
		g := m.GroupByName("a")
		if want, got := "2", g.String(); want != got {
			t.Fatalf("Wanted %v got %v", want, got)
		}
	}
}

func TestRE2CompatCapture_Invalid(t *testing.T) {
	bogus := []string{
		`(?P<name>a`,
		`(?P<name>`,
		`(?P<name`,
		`(?P<x y>a)`,
		`(?P<>a)`,
	}
	for _, inp := range bogus {
		t.Run(inp, func(t *testing.T) {
			r, err := Compile(inp, RE2)
			if err == nil {
				t.Fatal("Expected failure to parse")
			}
			if r != nil {
				t.Fatal("expected regexp to be nil")
			}
		})
	}
}

func TestRE2NamedAscii(t *testing.T) {
	table := []struct {
		nm  string
		pos string
		neg string
	}{
		{nm: "alnum", pos: "1", neg: "!"},
		{nm: "alpha", pos: "g", neg: "0"},
		{nm: "blank", pos: " ", neg: "_"},
		{nm: "ascii", pos: "*", neg: "\x8f"},
		{nm: "cntrl", pos: "\t", neg: "A"},
		{nm: "graph", pos: "!", neg: " "},
		{nm: "lower", pos: "a", neg: "A"},
		{nm: "print", pos: " ", neg: "\r"},
		{nm: "punct", pos: "@", neg: "A"},
		{nm: "space", pos: " ", neg: "A"},
		{nm: "digit", pos: "1", neg: "A"},
		{nm: "upper", pos: "A", neg: "a"},
		{nm: "word", pos: "_", neg: "-"},
		{nm: "xdigit", pos: "A", neg: "G"},
	}

	for _, row := range table {
		t.Run(row.nm, func(t *testing.T) {
			r := MustCompile(`[[:`+row.nm+`:]]`, RE2)
			if m, _ := r.MatchString(row.pos); !m {
				t.Fatal("Expected match")
			}
			if m, _ := r.MatchString(row.neg); m {
				t.Fatal("Expected no match")
			}
		})
		t.Run(row.nm+" negate", func(t *testing.T) {
			r := MustCompile(`[[:^`+row.nm+`:]]`, RE2)
			if m, _ := r.MatchString(row.neg); !m {
				t.Fatal("Expected match")
			}
			if m, _ := r.MatchString(row.pos); m {
				t.Fatal("Expected no match")
			}
		})
	}

}
func TestRE2NamedAscii_Concat(t *testing.T) {
	r := MustCompile(`[[:digit:]a]`, RE2)
	if m, _ := r.MatchString("b"); m {
		t.Fatal("Expected no match")
	}
	if m, _ := r.MatchString("a"); !m {
		t.Fatal("Expected match")
	}
	if m, _ := r.MatchString("["); m {
		t.Fatal("Expected no match")
	}
	if m, _ := r.MatchString("5"); !m {
		t.Fatal("Expected match")
	}
}