hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

regexp_test.go (2717B)

    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 TestReplaceRE(t *testing.T) {
   54 	t.Parallel()
   55 	c := qt.New(t)
   56 
   57 	for _, test := range []struct {
   58 		pattern any
   59 		repl    any
   60 		s       any
   61 		n       []any
   62 		expect  any
   63 	}{
   64 		{"^https?://([^/]+).*", "$1", "http://gohugo.io/docs", nil, "gohugo.io"},
   65 		{"^https?://([^/]+).*", "$2", "http://gohugo.io/docs", nil, ""},
   66 		{"(ab)", "AB", "aabbaab", nil, "aABbaAB"},
   67 		{"(ab)", "AB", "aabbaab", []any{1}, "aABbaab"},
   68 		// errors
   69 		{"(ab", "AB", "aabb", nil, false}, // invalid re
   70 		{tstNoStringer{}, "$2", "http://gohugo.io/docs", nil, false},
   71 		{"^https?://([^/]+).*", tstNoStringer{}, "http://gohugo.io/docs", nil, false},
   72 		{"^https?://([^/]+).*", "$2", tstNoStringer{}, nil, false},
   73 	} {
   74 
   75 		var (
   76 			result string
   77 			err    error
   78 		)
   79 		if len(test.n) > 0 {
   80 			result, err = ns.ReplaceRE(test.pattern, test.repl, test.s, test.n...)
   81 		} else {
   82 			result, err = ns.ReplaceRE(test.pattern, test.repl, test.s)
   83 		}
   84 
   85 		if b, ok := test.expect.(bool); ok && !b {
   86 			c.Assert(err, qt.Not(qt.IsNil))
   87 			continue
   88 		}
   89 
   90 		c.Assert(err, qt.IsNil)
   91 		c.Check(result, qt.Equals, test.expect)
   92 	}
   93 }