summaryrefslogtreecommitdiff
path: root/internal/regex/regex_test.go
blob: a5e7fafe4f70bc96a1065c88b789a7fbb0fac1db (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
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)
	}

	r2, err := Deserialize(r.Serialize())
	if err != nil {
		t.Errorf("unable to serialize deserialized regex: %v: %v\n", r.Serialize(), 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)
	}

	r2, err = Deserialize(r.Serialize())
	if err != nil {
		t.Errorf("unable to serialize deserialized regex: %v: %v\n", r.Serialize(), 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())
	}
}