github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/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 func TestReplaceRE(t *testing.T) { 87 t.Parallel() 88 c := qt.New(t) 89 90 for _, test := range []struct { 91 pattern any 92 repl any 93 s any 94 n []any 95 expect any 96 }{ 97 {"^https?://([^/]+).*", "$1", "http://gohugo.io/docs", nil, "gohugo.io"}, 98 {"^https?://([^/]+).*", "$2", "http://gohugo.io/docs", nil, ""}, 99 {"(ab)", "AB", "aabbaab", nil, "aABbaAB"}, 100 {"(ab)", "AB", "aabbaab", []any{1}, "aABbaab"}, 101 // errors 102 {"(ab", "AB", "aabb", nil, false}, // invalid re 103 {tstNoStringer{}, "$2", "http://gohugo.io/docs", nil, false}, 104 {"^https?://([^/]+).*", tstNoStringer{}, "http://gohugo.io/docs", nil, false}, 105 {"^https?://([^/]+).*", "$2", tstNoStringer{}, nil, false}, 106 } { 107 108 var ( 109 result string 110 err error 111 ) 112 if len(test.n) > 0 { 113 result, err = ns.ReplaceRE(test.pattern, test.repl, test.s, test.n...) 114 } else { 115 result, err = ns.ReplaceRE(test.pattern, test.repl, test.s) 116 } 117 118 if b, ok := test.expect.(bool); ok && !b { 119 c.Assert(err, qt.Not(qt.IsNil)) 120 continue 121 } 122 123 c.Assert(err, qt.IsNil) 124 c.Check(result, qt.Equals, test.expect) 125 } 126 }