github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/tpl/collections/symdiff_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 collections
    15  
    16  import (
    17  	"reflect"
    18  	"testing"
    19  
    20  	"github.com/gohugoio/hugo/config"
    21  	"github.com/gohugoio/hugo/deps"
    22  	"github.com/gohugoio/hugo/langs"
    23  
    24  	qt "github.com/frankban/quicktest"
    25  )
    26  
    27  func TestSymDiff(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	c := qt.New(t)
    31  
    32  	ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())})
    33  
    34  	s1 := []TstX{{A: "a"}, {A: "b"}}
    35  	s2 := []TstX{{A: "a"}, {A: "e"}}
    36  
    37  	xa, xb, xd, xe := &StructWithSlice{A: "a"}, &StructWithSlice{A: "b"}, &StructWithSlice{A: "d"}, &StructWithSlice{A: "e"}
    38  
    39  	sp1 := []*StructWithSlice{xa, xb, xd, xe}
    40  	sp2 := []*StructWithSlice{xb, xe}
    41  
    42  	for i, test := range []struct {
    43  		s1       any
    44  		s2       any
    45  		expected any
    46  	}{
    47  		{[]string{"a", "x", "b", "c"}, []string{"a", "b", "y", "c"}, []string{"x", "y"}},
    48  		{[]string{"a", "b", "c"}, []string{"a", "b", "c"}, []string{}},
    49  		{[]any{"a", "b", nil}, []any{"a"}, []any{"b", nil}},
    50  		{[]int{1, 2, 3}, []int{3, 4}, []int{1, 2, 4}},
    51  		{[]int{1, 2, 3}, []int64{3, 4}, []int{1, 2, 4}},
    52  		{s1, s2, []TstX{{A: "b"}, {A: "e"}}},
    53  		{sp1, sp2, []*StructWithSlice{xa, xd}},
    54  
    55  		// Errors
    56  		{"error", "error", false},
    57  		{[]int{1, 2, 3}, []string{"3", "4"}, false},
    58  	} {
    59  
    60  		errMsg := qt.Commentf("[%d]", i)
    61  
    62  		result, err := ns.SymDiff(test.s2, test.s1)
    63  
    64  		if b, ok := test.expected.(bool); ok && !b {
    65  			c.Assert(err, qt.Not(qt.IsNil), errMsg)
    66  			continue
    67  		}
    68  
    69  		c.Assert(err, qt.IsNil, errMsg)
    70  
    71  		if !reflect.DeepEqual(test.expected, result) {
    72  			t.Fatalf("%s got\n%T: %v\nexpected\n%T: %v", errMsg, result, result, test.expected, test.expected)
    73  		}
    74  	}
    75  
    76  	_, err := ns.Complement()
    77  	c.Assert(err, qt.Not(qt.IsNil))
    78  	_, err = ns.Complement([]string{"a", "b"})
    79  	c.Assert(err, qt.Not(qt.IsNil))
    80  }