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