github.com/neohugo/neohugo@v0.123.8/tpl/strings/regexp_test.go (about) 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 // errors 103 {"(ab", "AB", "aabb", nil, false}, // invalid re 104 {tstNoStringer{}, "$2", "http://gohugo.io/docs", nil, false}, 105 {"^https?://([^/]+).*", tstNoStringer{}, "http://gohugo.io/docs", nil, false}, 106 {"^https?://([^/]+).*", "$2", tstNoStringer{}, nil, false}, 107 } { 108 109 var ( 110 result string 111 err error 112 ) 113 if len(test.n) > 0 { 114 result, err = ns.ReplaceRE(test.pattern, test.repl, test.s, test.n...) 115 } else { 116 result, err = ns.ReplaceRE(test.pattern, test.repl, test.s) 117 } 118 119 if b, ok := test.expect.(bool); ok && !b { 120 c.Assert(err, qt.Not(qt.IsNil)) 121 continue 122 } 123 124 c.Assert(err, qt.IsNil) 125 c.Check(result, qt.Equals, test.expect) 126 } 127 }