1package regexp2
2
3import (
4	"strconv"
5	"testing"
6)
7
8func TestReplace_Basic(t *testing.T) {
9	re := MustCompile(`test`, 0)
10	str, err := re.Replace("this is a test", "unit", -1, -1)
11	if err != nil {
12		t.Fatalf("Unexpected err: %v", err)
13	}
14	if want, got := "this is a unit", str; want != got {
15		t.Fatalf("Replace failed, wanted %v, got %v", want, got)
16	}
17}
18
19func TestReplace_NamedGroup(t *testing.T) {
20	re := MustCompile(`[^ ]+\s(?<time>)`, 0)
21	str, err := re.Replace("08/10/99 16:00", "${time}", -1, -1)
22	if err != nil {
23		t.Fatalf("Unexpected err: %v", err)
24	}
25	if want, got := "16:00", str; want != got {
26		t.Fatalf("Replace failed, wanted %v, got %v", want, got)
27	}
28}
29
30func TestReplace_IgnoreCaseUpper(t *testing.T) {
31	re := MustCompile(`dog`, IgnoreCase)
32	str, err := re.Replace("my dog has fleas", "CAT", -1, -1)
33	if err != nil {
34		t.Fatalf("Unexpected err: %v", err)
35	}
36	if want, got := "my CAT has fleas", str; want != got {
37		t.Fatalf("Replace failed, wanted %v, got %v", want, got)
38	}
39}
40
41func TestReplace_IgnoreCaseLower(t *testing.T) {
42	re := MustCompile(`olang`, IgnoreCase)
43	str, err := re.Replace("GoLAnG", "olang", -1, -1)
44	if err != nil {
45		t.Fatalf("Unexpected err: %v", err)
46	}
47	if want, got := "Golang", str; want != got {
48		t.Fatalf("Replace failed, wanted %v, got %v", want, got)
49	}
50}
51
52func TestReplace_NumberGroup(t *testing.T) {
53	re := MustCompile(`D\.(.+)`, None)
54	str, err := re.Replace("D.Bau", "David $1", -1, -1)
55	if err != nil {
56		t.Fatalf("Unexpected err: %v", err)
57	}
58	if want, got := "David Bau", str; want != got {
59		t.Fatalf("Replace failed, wanted %v, got %v", want, got)
60	}
61}
62
63func TestReplace_LimitCount(t *testing.T) {
64	re := MustCompile(`a`, None)
65	str, err := re.Replace("aaaaa", "b", 0, 2)
66	if err != nil {
67		t.Fatalf("Unexpected err: %v", err)
68	}
69	if want, got := "bbaaa", str; want != got {
70		t.Fatalf("Replace failed, wanted %v, got %v", want, got)
71	}
72}
73
74func TestReplace_LimitCountSlice(t *testing.T) {
75	re := MustCompile(`a`, None)
76	myStr := "aaaaa"
77	str, err := re.Replace(myStr, "b", 3, 2)
78	if err != nil {
79		t.Fatalf("Unexpected err: %v", err)
80	}
81	if want, got := "aaabb", str; want != got {
82		t.Fatalf("Replace failed, wanted %v, got %v", want, got)
83	}
84}
85
86func TestReplace_BeginBeforeAfterEnd(t *testing.T) {
87	re := MustCompile(`a`, None)
88	myStr := "a test a blah and a"
89	str, err := re.Replace(myStr, "stuff", -1, -1)
90	if err != nil {
91		t.Fatalf("Unexpected err: %v", err)
92	}
93	if want, got := "stuff test stuff blstuffh stuffnd stuff", str; want != got {
94		t.Fatalf("Replace failed, wanted %v, got %v", want, got)
95	}
96}
97
98func TestReplace_BadSyntax(t *testing.T) {
99	re := MustCompile(`a`, None)
100	myStr := "this is a test"
101	_, err := re.Replace(myStr, `$5000000000`, -1, -1)
102	if err == nil {
103		t.Fatalf("Expected err")
104	}
105}
106
107func TestReplaceFunc_Basic(t *testing.T) {
108	re := MustCompile(`test`, None)
109	str, err := re.ReplaceFunc("this is a test", func(m Match) string { return "unit" }, -1, -1)
110	if err != nil {
111		t.Fatalf("Unexpected err: %v", err)
112	}
113	if want, got := "this is a unit", str; want != got {
114		t.Fatalf("Replace failed, wanted %v, got %v", want, got)
115	}
116}
117
118func TestReplaceFunc_Multiple(t *testing.T) {
119	re := MustCompile(`test`, None)
120	count := 0
121	str, err := re.ReplaceFunc("This test is another test for stuff", func(m Match) string {
122		count++
123		return strconv.Itoa(count)
124	}, -1, -1)
125	if err != nil {
126		t.Fatalf("Unexpected err: %v", err)
127	}
128	if want, got := "This 1 is another 2 for stuff", str; want != got {
129		t.Fatalf("Replace failed, wanted %v, got %v", want, got)
130	}
131}
132
133func TestReplaceFunc_Groups(t *testing.T) {
134	re := MustCompile(`test(?<sub>ing)?`, None)
135	count := 0
136	str, err := re.ReplaceFunc("This testing is another test testingly junk", func(m Match) string {
137		count++
138		if m.GroupByName("sub").Length > 0 {
139			// we have an "ing", make it negative
140			return strconv.Itoa(count * -1)
141		}
142		return strconv.Itoa(count)
143	}, -1, -1)
144	if err != nil {
145		t.Fatalf("Unexpected err: %v", err)
146	}
147	if want, got := "This -1 is another 2 -3ly junk", str; want != got {
148		t.Fatalf("Replace failed, wanted %v, got %v", want, got)
149	}
150}
151
152func TestReplace_RefNumsDollarAmbiguous(t *testing.T) {
153	re := MustCompile("(123)hello(789)", None)
154	res, err := re.Replace("123hello789", "$1456$2", -1, -1)
155	if err != nil {
156		t.Fatal(err)
157	}
158	if want, got := "$1456789", res; want != got {
159		t.Fatalf("Wrong result: %s", got)
160	}
161}
162
163func TestReplace_NestedGroups(t *testing.T) {
164	re := MustCompile(`(\p{Sc}\s?)?(\d+\.?((?<=\.)\d+)?)(?(1)|\s?\p{Sc})?`, None)
165	res, err := re.Replace("$17.43  €2 16.33  £0.98  0.43   £43   12€  17", "$2", -1, -1)
166	if err != nil {
167		t.Fatal(err)
168	}
169	if want, got := "17.43  2 16.33  0.98  0.43   43   12  17", res; want != got {
170		t.Fatalf("Wrong result: %s", got)
171	}
172}
173