github.com/neohugo/neohugo@v0.123.8/common/hugio/hasBytesWriter_test.go (about)

     1  // Copyright 2024 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 hugio
    15  
    16  import (
    17  	"bytes"
    18  	"fmt"
    19  	"io"
    20  	"math/rand"
    21  	"strings"
    22  	"testing"
    23  	"time"
    24  
    25  	qt "github.com/frankban/quicktest"
    26  )
    27  
    28  func TestHasBytesWriter(t *testing.T) {
    29  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
    30  
    31  	c := qt.New((t))
    32  
    33  	neww := func() (*HasBytesWriter, io.Writer) {
    34  		var b bytes.Buffer
    35  
    36  		h := &HasBytesWriter{
    37  			Pattern: []byte("__foo"),
    38  		}
    39  		return h, io.MultiWriter(&b, h)
    40  	}
    41  
    42  	rndStr := func() string {
    43  		return strings.Repeat("ab cfo", r.Intn(33))
    44  	}
    45  
    46  	for i := 0; i < 22; i++ {
    47  		h, w := neww()
    48  		fmt.Fprintf(w, rndStr()+"abc __foobar"+rndStr())
    49  		c.Assert(h.Match, qt.Equals, true)
    50  
    51  		h, w = neww()
    52  		fmt.Fprintf(w, rndStr()+"abc __f")
    53  		fmt.Fprintf(w, "oo bar"+rndStr())
    54  		c.Assert(h.Match, qt.Equals, true)
    55  
    56  		h, w = neww()
    57  		fmt.Fprintf(w, rndStr()+"abc __moo bar")
    58  		c.Assert(h.Match, qt.Equals, false)
    59  	}
    60  
    61  	h, w := neww()
    62  	fmt.Fprintf(w, "__foo")
    63  	c.Assert(h.Match, qt.Equals, true)
    64  }