github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/config/namespace_test.go (about)

     1  // Copyright 2023 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 config
    15  
    16  import (
    17  	"strings"
    18  	"testing"
    19  
    20  	qt "github.com/frankban/quicktest"
    21  	"github.com/gohugoio/hugo/common/maps"
    22  	"github.com/mitchellh/mapstructure"
    23  )
    24  
    25  func TestNamespace(t *testing.T) {
    26  	c := qt.New(t)
    27  	c.Assert(true, qt.Equals, true)
    28  
    29  	//ns, err := config.DecodeNamespace[map[string]DocsMediaTypeConfig](in, defaultMediaTypesConfig, buildConfig)
    30  
    31  	ns, err := DecodeNamespace[[]*tstNsExt](
    32  		map[string]interface{}{"foo": "bar"},
    33  		func(v any) (*tstNsExt, any, error) {
    34  			t := &tstNsExt{}
    35  			m, err := maps.ToStringMapE(v)
    36  			if err != nil {
    37  				return nil, nil, err
    38  			}
    39  			return t, nil, mapstructure.WeakDecode(m, t)
    40  		},
    41  	)
    42  
    43  	c.Assert(err, qt.IsNil)
    44  	c.Assert(ns, qt.Not(qt.IsNil))
    45  	c.Assert(ns.SourceStructure, qt.DeepEquals, map[string]interface{}{"foo": "bar"})
    46  	c.Assert(ns.SourceHash, qt.Equals, "14368731254619220105")
    47  	c.Assert(ns.Config, qt.DeepEquals, &tstNsExt{Foo: "bar"})
    48  	c.Assert(ns.Signature(), qt.DeepEquals, []*tstNsExt(nil))
    49  
    50  }
    51  
    52  type (
    53  	tstNsExt struct {
    54  		Foo string
    55  	}
    56  	tstNsInt struct {
    57  		Foo string
    58  	}
    59  )
    60  
    61  func (t *tstNsExt) Init() error {
    62  	t.Foo = strings.ToUpper(t.Foo)
    63  	return nil
    64  }
    65  func (t *tstNsInt) Compile(ext *tstNsExt) error {
    66  	t.Foo = ext.Foo + " qux"
    67  	return nil
    68  }