summaryrefslogtreecommitdiff
path: root/internal/regex/regex_test.go
blob: 033a2861d5cf8028198a8a50c811a0ec9fb87ff4 (plain)
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
package regex

import (
	"testing"
)

func TestRegex(t *testing.T) {
	input := "hello"

	r := NewNoop()
	if !r.MatchString(input) {
		t.Errorf("expected to match string '%s' with noop regex '%v' but didn't\n",
			input, r)
	}

	r, err := New(".hello", Default)
	if err != nil {
		t.Errorf("unable to create regex: %v\n", err)
	}
	if r.MatchString(input) {
		t.Errorf("expected to match string '%s' with regex '%v' but didn't\n",
			input, r)
	}

	serialized, err := r.Serialize()
	if err != nil {
		t.Errorf("unable to serialize regex: %v: %v\n", serialized, err)
	}
	r2, err := Deserialize(serialized)
	if err != nil {
		t.Errorf("unable to serialize deserialized regex: %v: %v\n", serialized, err)
	}
	if r.String() != r2.String() {
		t.Errorf("regex should be the same after deserialize(serialize(..)), got "+
			"'%s' but expected '%s'.\n", r2.String(), r.String())
	}

	r, err = New(".hello", Invert)
	if err != nil {
		t.Errorf("unable to create regex: %v\n", err)
	}
	if !r.MatchString(input) {
		t.Errorf("expected to not match string '%s' with regex '%v' but matched\n",
			input, r)
	}

	serialized, err = r.Serialize()
	if err != nil {
		t.Errorf("unable to serialize regex: %v: %v\n", serialized, err)
	}
	r2, err = Deserialize(serialized)
	if err != nil {
		t.Errorf("unable to serialize deserialized regex: %v: %v\n", serialized, err)
	}
	if r.String() != r2.String() {
		t.Errorf("regex should be the same after deserialize(serialize(..)), got "+
			"'%s' but expected '%s'.\n", r2.String(), r.String())
	}
}