github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/tpl/path/path_test.go (about)

     1  // Copyright 2018 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 path
    15  
    16  import (
    17  	"path/filepath"
    18  	"testing"
    19  
    20  	qt "github.com/frankban/quicktest"
    21  	"github.com/gohugoio/hugo/config"
    22  	"github.com/gohugoio/hugo/deps"
    23  )
    24  
    25  var ns = New(&deps.Deps{Cfg: config.New()})
    26  
    27  type tstNoStringer struct{}
    28  
    29  func TestBase(t *testing.T) {
    30  	t.Parallel()
    31  	c := qt.New(t)
    32  
    33  	for _, test := range []struct {
    34  		path   interface{}
    35  		expect interface{}
    36  	}{
    37  		{filepath.FromSlash(`foo/bar.txt`), `bar.txt`},
    38  		{filepath.FromSlash(`foo/bar/txt `), `txt `},
    39  		{filepath.FromSlash(`foo/bar.t`), `bar.t`},
    40  		{`foo.bar.txt`, `foo.bar.txt`},
    41  		{`.x`, `.x`},
    42  		{``, `.`},
    43  		// errors
    44  		{tstNoStringer{}, false},
    45  	} {
    46  
    47  		result, err := ns.Base(test.path)
    48  
    49  		if b, ok := test.expect.(bool); ok && !b {
    50  			c.Assert(err, qt.Not(qt.IsNil))
    51  			continue
    52  		}
    53  
    54  		c.Assert(err, qt.IsNil)
    55  		c.Assert(result, qt.Equals, test.expect)
    56  	}
    57  }
    58  
    59  func TestDir(t *testing.T) {
    60  	t.Parallel()
    61  	c := qt.New(t)
    62  
    63  	for _, test := range []struct {
    64  		path   interface{}
    65  		expect interface{}
    66  	}{
    67  		{filepath.FromSlash(`foo/bar.txt`), `foo`},
    68  		{filepath.FromSlash(`foo/bar/txt `), `foo/bar`},
    69  		{filepath.FromSlash(`foo/bar.t`), `foo`},
    70  		{`foo.bar.txt`, `.`},
    71  		{`.x`, `.`},
    72  		{``, `.`},
    73  		// errors
    74  		{tstNoStringer{}, false},
    75  	} {
    76  
    77  		result, err := ns.Dir(test.path)
    78  
    79  		if b, ok := test.expect.(bool); ok && !b {
    80  			c.Assert(err, qt.Not(qt.IsNil))
    81  			continue
    82  		}
    83  
    84  		c.Assert(err, qt.IsNil)
    85  		c.Assert(result, qt.Equals, test.expect)
    86  	}
    87  }
    88  
    89  func TestExt(t *testing.T) {
    90  	t.Parallel()
    91  	c := qt.New(t)
    92  
    93  	for _, test := range []struct {
    94  		path   interface{}
    95  		expect interface{}
    96  	}{
    97  		{filepath.FromSlash(`foo/bar.json`), `.json`},
    98  		{`foo.bar.txt `, `.txt `},
    99  		{``, ``},
   100  		{`.x`, `.x`},
   101  		// errors
   102  		{tstNoStringer{}, false},
   103  	} {
   104  
   105  		result, err := ns.Ext(test.path)
   106  
   107  		if b, ok := test.expect.(bool); ok && !b {
   108  			c.Assert(err, qt.Not(qt.IsNil))
   109  			continue
   110  		}
   111  
   112  		c.Assert(err, qt.IsNil)
   113  		c.Assert(result, qt.Equals, test.expect)
   114  	}
   115  }
   116  
   117  func TestJoin(t *testing.T) {
   118  	t.Parallel()
   119  	c := qt.New(t)
   120  
   121  	for _, test := range []struct {
   122  		elements interface{}
   123  		expect   interface{}
   124  	}{
   125  		{
   126  			[]string{"", "baz", filepath.FromSlash(`foo/bar.txt`)},
   127  			`baz/foo/bar.txt`,
   128  		},
   129  		{
   130  			[]interface{}{"", "baz", DirFile{"big", "john"}, filepath.FromSlash(`foo/bar.txt`)},
   131  			`baz/big|john/foo/bar.txt`,
   132  		},
   133  		{nil, ""},
   134  		// errors
   135  		{tstNoStringer{}, false},
   136  		{[]interface{}{"", tstNoStringer{}}, false},
   137  	} {
   138  
   139  		result, err := ns.Join(test.elements)
   140  
   141  		if b, ok := test.expect.(bool); ok && !b {
   142  			c.Assert(err, qt.Not(qt.IsNil))
   143  			continue
   144  		}
   145  
   146  		c.Assert(err, qt.IsNil)
   147  		c.Assert(result, qt.Equals, test.expect)
   148  	}
   149  }
   150  
   151  func TestSplit(t *testing.T) {
   152  	t.Parallel()
   153  	c := qt.New(t)
   154  
   155  	for _, test := range []struct {
   156  		path   interface{}
   157  		expect interface{}
   158  	}{
   159  		{filepath.FromSlash(`foo/bar.txt`), DirFile{`foo/`, `bar.txt`}},
   160  		{filepath.FromSlash(`foo/bar/txt `), DirFile{`foo/bar/`, `txt `}},
   161  		{`foo.bar.txt`, DirFile{``, `foo.bar.txt`}},
   162  		{``, DirFile{``, ``}},
   163  		// errors
   164  		{tstNoStringer{}, false},
   165  	} {
   166  
   167  		result, err := ns.Split(test.path)
   168  
   169  		if b, ok := test.expect.(bool); ok && !b {
   170  			c.Assert(err, qt.Not(qt.IsNil))
   171  			continue
   172  		}
   173  
   174  		c.Assert(err, qt.IsNil)
   175  		c.Assert(result, qt.Equals, test.expect)
   176  	}
   177  }
   178  
   179  func TestClean(t *testing.T) {
   180  	t.Parallel()
   181  	c := qt.New(t)
   182  
   183  	for _, test := range []struct {
   184  		path   interface{}
   185  		expect interface{}
   186  	}{
   187  		{filepath.FromSlash(`foo/bar.txt`), `foo/bar.txt`},
   188  		{filepath.FromSlash(`foo/bar/txt`), `foo/bar/txt`},
   189  		{filepath.FromSlash(`foo/bar`), `foo/bar`},
   190  		{filepath.FromSlash(`foo/bar.t`), `foo/bar.t`},
   191  		{``, `.`},
   192  		// errors
   193  		{tstNoStringer{}, false},
   194  	} {
   195  
   196  		result, err := ns.Clean(test.path)
   197  
   198  		if b, ok := test.expect.(bool); ok && !b {
   199  			c.Assert(err, qt.Not(qt.IsNil))
   200  			continue
   201  		}
   202  
   203  		c.Assert(err, qt.IsNil)
   204  		c.Assert(result, qt.Equals, test.expect)
   205  	}
   206  }