github.com/gohugoio/hugo@v0.88.1/hugofs/glob/glob_test.go (about)

     1  // Copyright 2019 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 glob
    15  
    16  import (
    17  	"path/filepath"
    18  	"testing"
    19  
    20  	qt "github.com/frankban/quicktest"
    21  )
    22  
    23  func TestResolveRootDir(t *testing.T) {
    24  	c := qt.New(t)
    25  
    26  	for _, test := range []struct {
    27  		input    string
    28  		expected string
    29  	}{
    30  		{"data/foo.json", "data"},
    31  		{"a/b/**/foo.json", "a/b"},
    32  		{"dat?a/foo.json", ""},
    33  		{"a/b[a-c]/foo.json", "a"},
    34  	} {
    35  		c.Assert(ResolveRootDir(test.input), qt.Equals, test.expected)
    36  	}
    37  }
    38  
    39  func TestFilterGlobParts(t *testing.T) {
    40  	c := qt.New(t)
    41  
    42  	for _, test := range []struct {
    43  		input    []string
    44  		expected []string
    45  	}{
    46  		{[]string{"a", "*", "c"}, []string{"a", "c"}},
    47  	} {
    48  		c.Assert(FilterGlobParts(test.input), qt.DeepEquals, test.expected)
    49  	}
    50  }
    51  
    52  func TestNormalizePath(t *testing.T) {
    53  	c := qt.New(t)
    54  
    55  	for _, test := range []struct {
    56  		input    string
    57  		expected string
    58  	}{
    59  		{filepath.FromSlash("data/FOO.json"), "data/foo.json"},
    60  		{filepath.FromSlash("/data/FOO.json"), "data/foo.json"},
    61  		{filepath.FromSlash("./FOO.json"), "foo.json"},
    62  		{"//", ""},
    63  	} {
    64  		c.Assert(NormalizePath(test.input), qt.Equals, test.expected)
    65  	}
    66  }
    67  
    68  func TestGetGlob(t *testing.T) {
    69  	c := qt.New(t)
    70  	g, err := GetGlob("**.JSON")
    71  	c.Assert(err, qt.IsNil)
    72  	c.Assert(g.Match("data/my.json"), qt.Equals, true)
    73  }
    74  
    75  func BenchmarkGetGlob(b *testing.B) {
    76  	for i := 0; i < b.N; i++ {
    77  		_, err := GetGlob("**/foo")
    78  		if err != nil {
    79  			b.Fatal(err)
    80  		}
    81  	}
    82  }