Debian Codegraph @

tpl/strings/regexp_test.go

raw
1 // Copyright 2017 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package strings
15
16 import (
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 )
21
22 func TestFindRE(t *testing.T) {
23 t.Parallel()
24 c := qt.New(t)
25
26 for _, test := range []struct {
27 expr string
28 content any
29 limit any
30 expect any
31 }{
32 {"[G|g]o", "Hugo is a static site generator written in Go.", 2, []string{"go", "Go"}},
33 {"[G|g]o", "Hugo is a static site generator written in Go.", -1, []string{"go", "Go"}},
34 {"[G|g]o", "Hugo is a static site generator written in Go.", 1, []string{"go"}},
35 {"[G|g]o", "Hugo is a static site generator written in Go.", "1", []string{"go"}},
36 {"[G|g]o", "Hugo is a static site generator written in Go.", nil, []string(nil)},
37 // errors
38 {"[G|go", "Hugo is a static site generator written in Go.", nil, false},
39 {"[G|g]o", t, nil, false},
40 } {
41 result, err := ns.FindRE(test.expr, test.content, test.limit)
42
43 if b, ok := test.expect.(bool); ok && !b {
44 c.Assert(err, qt.Not(qt.IsNil))
45 continue
46 }
47
48 c.Assert(err, qt.IsNil)
49 c.Check(result, qt.DeepEquals, test.expect)
50 }
51 }
52
53 func TestFindRESubmatch(t *testing.T) {
54 t.Parallel()
55 c := qt.New(t)
56
57 for _, test := range []struct {
58 expr string
59 content any
60 limit any
61 expect any
62 }{
63 {`<a\s*href="(.+?)">(.+?)</a>`, `<li><a href="#foo">Foo</a></li><li><a href="#bar">Bar</a></li>`, -1, [][]string{
64 {"<a href=\"#foo\">Foo</a>", "#foo", "Foo"},
65 {"<a href=\"#bar\">Bar</a>", "#bar", "Bar"},
66 }},
67 // Some simple cases.
68 {"([G|g]o)", "Hugo is a static site generator written in Go.", -1, [][]string{{"go", "go"}, {"Go", "Go"}}},
69 {"([G|g]o)", "Hugo is a static site generator written in Go.", 1, [][]string{{"go", "go"}}},
70
71 // errors
72 {"([G|go", "Hugo is a static site generator written in Go.", nil, false},
73 {"([G|g]o)", t, nil, false},
74 } {
75 result, err := ns.FindRESubmatch(test.expr, test.content, test.limit)
76
77 if b, ok := test.expect.(bool); ok && !b {
78 c.Assert(err, qt.Not(qt.IsNil))
79 continue
80 }
81
82 c.Assert(err, qt.IsNil)
83 c.Check(result, qt.DeepEquals, test.expect)
84 }
85 }
86
87 func TestReplaceRE(t *testing.T) {
88 t.Parallel()
89 c := qt.New(t)
90
91 for _, test := range []struct {
92 pattern any
93 repl any
94 s any
95 n []any
96 expect any
97 }{
98 {"^https?://([^/]+).*", "$1", "http://gohugo.io/docs", nil, "gohugo.io"},
99 {"^https?://([^/]+).*", "$2", "http://gohugo.io/docs", nil, ""},
100 {"(ab)", "AB", "aabbaab", nil, "aABbaAB"},
101 {"(ab)", "AB", "aabbaab", []any{1}, "aABbaab"},
102 // A zero-width assertion needs the surrounding text to match.
103 {`\Bcd`, "-", "abcdef", nil, "ab-ef"},
104 {`\b`, "|", "foo bar", nil, "|foo| |bar|"},
105 {`\Bb`, "X", "foobar bar bab", nil, "fooXar bar baX"},
106 {`\Bb`, "X", "foobar bar bab", []any{1}, "fooXar bar bab"},
107 // errors
108 {"(ab", "AB", "aabb", nil, false}, // invalid re
109 {tstNoStringer{}, "$2", "http://gohugo.io/docs", nil, false},
110 {"^https?://([^/]+).*", tstNoStringer{}, "http://gohugo.io/docs", nil, false},
111 {"^https?://([^/]+).*", "$2", tstNoStringer{}, nil, false},
112 } {
113
114 var (
115 result string
116 err error
117 )
118 if len(test.n) > 0 {
119 result, err = ns.ReplaceRE(test.pattern, test.repl, test.s, test.n...)
120 } else {
121 result, err = ns.ReplaceRE(test.pattern, test.repl, test.s)
122 }
123
124 if b, ok := test.expect.(bool); ok && !b {
125 c.Assert(err, qt.Not(qt.IsNil))
126 continue
127 }
128
129 c.Assert(err, qt.IsNil)
130 c.Check(result, qt.Equals, test.expect)
131 }
132 }