github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/tpl/urls/urls_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 urls
    15  
    16  import (
    17  	"net/url"
    18  	"testing"
    19  
    20  	"github.com/gohugoio/hugo/config/testconfig"
    21  	"github.com/gohugoio/hugo/htesting/hqt"
    22  
    23  	qt "github.com/frankban/quicktest"
    24  )
    25  
    26  func newNs() *Namespace {
    27  	return New(testconfig.GetTestDeps(nil, nil))
    28  }
    29  
    30  type tstNoStringer struct{}
    31  
    32  func TestParse(t *testing.T) {
    33  	t.Parallel()
    34  	c := qt.New(t)
    35  	ns := newNs()
    36  
    37  	for _, test := range []struct {
    38  		rawurl any
    39  		expect any
    40  	}{
    41  		{
    42  			"http://www.google.com",
    43  			&url.URL{
    44  				Scheme: "http",
    45  				Host:   "www.google.com",
    46  			},
    47  		},
    48  		{
    49  			"http://j@ne:password@google.com",
    50  			&url.URL{
    51  				Scheme: "http",
    52  				User:   url.UserPassword("j@ne", "password"),
    53  				Host:   "google.com",
    54  			},
    55  		},
    56  		// errors
    57  		{tstNoStringer{}, false},
    58  	} {
    59  
    60  		result, err := ns.Parse(test.rawurl)
    61  
    62  		if b, ok := test.expect.(bool); ok && !b {
    63  			c.Assert(err, qt.Not(qt.IsNil))
    64  			continue
    65  		}
    66  
    67  		c.Assert(err, qt.IsNil)
    68  		c.Assert(result,
    69  			qt.CmpEquals(hqt.DeepAllowUnexported(&url.URL{}, url.Userinfo{})), test.expect)
    70  	}
    71  }
    72  
    73  func TestJoinPath(t *testing.T) {
    74  	t.Parallel()
    75  	c := qt.New(t)
    76  	ns := newNs()
    77  
    78  	for _, test := range []struct {
    79  		elements any
    80  		expect   any
    81  	}{
    82  		{"", `/`},
    83  		{"a", `a`},
    84  		{"/a/b", `/a/b`},
    85  		{"./../a/b", `a/b`},
    86  		{[]any{""}, `/`},
    87  		{[]any{"a"}, `a`},
    88  		{[]any{"/a", "b"}, `/a/b`},
    89  		{[]any{".", "..", "/a", "b"}, `a/b`},
    90  		{[]any{"https://example.org", "a"}, `https://example.org/a`},
    91  		{[]any{nil}, `/`},
    92  		// errors
    93  		{tstNoStringer{}, false},
    94  		{[]any{tstNoStringer{}}, false},
    95  	} {
    96  
    97  		result, err := ns.JoinPath(test.elements)
    98  
    99  		if b, ok := test.expect.(bool); ok && !b {
   100  			c.Assert(err, qt.Not(qt.IsNil))
   101  			continue
   102  		}
   103  
   104  		c.Assert(err, qt.IsNil)
   105  		c.Assert(result, qt.Equals, test.expect)
   106  	}
   107  }