File: rtl_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 (52 lines) | stat: -rw-r--r-- 1,295 bytes parent folder | download | duplicates (2)
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
package regexp2

import "testing"

func TestRightToLeft_Basic(t *testing.T) {
	re := MustCompile(`foo\d+`, RightToLeft)
	s := "0123456789foo4567890foo1foo  0987"

	m, err := re.FindStringMatch(s)
	if err != nil {
		t.Fatalf("Unexpected err: %v", err)
	}
	if want, got := "foo1", m.String(); want != got {
		t.Fatalf("Match 0 failed, wanted %v, got %v", want, got)
	}

	m, err = re.FindNextMatch(m)
	if err != nil {
		t.Fatalf("Unexpected err: %v", err)
	}
	if want, got := "foo4567890", m.String(); want != got {
		t.Fatalf("Match 1 failed, wanted %v, got %v", want, got)
	}
}

func TestRightToLeft_StartAt(t *testing.T) {
	re := MustCompile(`\d`, RightToLeft)

	m, err := re.FindStringMatchStartingAt("0123", -1)
	if err != nil {
		t.Fatalf("Unexpected err: %v", err)
	}
	if m == nil {
		t.Fatalf("Expected match")
	}
	if want, got := "3", m.String(); want != got {
		t.Fatalf("Find failed, wanted '%v', got '%v'", want, got)
	}

}

func TestRightToLeft_Replace(t *testing.T) {
	re := MustCompile(`\d`, RightToLeft)
	s := "0123456789foo4567890foo         "
	str, err := re.Replace(s, "#", -1, 7)
	if err != nil {
		t.Fatalf("Unexpected err: %v", err)
	}
	if want, got := "0123456789foo#######foo         ", str; want != got {
		t.Fatalf("Replace failed, wanted '%v', got '%v'", want, got)
	}
}