github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/tpl/collections/complement_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  	qt "github.com/frankban/quicktest"
    21  )
    22  
    23  type StructWithSlice struct {
    24  	A string
    25  	B []string
    26  }
    27  
    28  type StructWithSlicePointers []*StructWithSlice
    29  
    30  func TestComplement(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	c := qt.New(t)
    34  
    35  	ns := newNs()
    36  
    37  	s1 := []TstX{{A: "a"}, {A: "b"}, {A: "d"}, {A: "e"}}
    38  	s2 := []TstX{{A: "b"}, {A: "e"}}
    39  
    40  	xa, xb, xd, xe := &StructWithSlice{A: "a"}, &StructWithSlice{A: "b"}, &StructWithSlice{A: "d"}, &StructWithSlice{A: "e"}
    41  
    42  	sp1 := []*StructWithSlice{xa, xb, xd, xe}
    43  	sp2 := []*StructWithSlice{xb, xe}
    44  
    45  	sp1_2 := StructWithSlicePointers{xa, xb, xd, xe}
    46  	sp2_2 := StructWithSlicePointers{xb, xe}
    47  
    48  	for i, test := range []struct {
    49  		s        any
    50  		t        []any
    51  		expected any
    52  	}{
    53  		{[]string{"a", "b", "c"}, []any{[]string{"c", "d"}}, []string{"a", "b"}},
    54  		{[]string{"a", "b", "c"}, []any{[]string{"c", "d"}, []string{"a", "b"}}, []string{}},
    55  		{[]any{"a", "b", nil}, []any{[]string{"a", "d"}}, []any{"b", nil}},
    56  		{[]int{1, 2, 3, 4, 5}, []any{[]int{1, 3}, []string{"a", "b"}, []int{1, 2}}, []int{4, 5}},
    57  		{[]int{1, 2, 3, 4, 5}, []any{[]int64{1, 3}}, []int{2, 4, 5}},
    58  		{s1, []any{s2}, []TstX{{A: "a"}, {A: "d"}}},
    59  		{sp1, []any{sp2}, []*StructWithSlice{xa, xd}},
    60  		{sp1_2, []any{sp2_2}, StructWithSlicePointers{xa, xd}},
    61  
    62  		// Errors
    63  		{[]string{"a", "b", "c"}, []any{"error"}, false},
    64  		{"error", []any{[]string{"c", "d"}, []string{"a", "b"}}, false},
    65  		{[]string{"a", "b", "c"}, []any{[][]string{{"c", "d"}}}, false},
    66  		{
    67  			[]any{[][]string{{"c", "d"}}},
    68  			[]any{[]string{"c", "d"}, []string{"a", "b"}},
    69  			[]any{[][]string{{"c", "d"}}},
    70  		},
    71  	} {
    72  
    73  		errMsg := qt.Commentf("[%d]", i)
    74  
    75  		args := append(test.t, test.s)
    76  
    77  		result, err := ns.Complement(args...)
    78  
    79  		if b, ok := test.expected.(bool); ok && !b {
    80  			c.Assert(err, qt.Not(qt.IsNil), errMsg)
    81  			continue
    82  		}
    83  
    84  		c.Assert(err, qt.IsNil, errMsg)
    85  
    86  		if !reflect.DeepEqual(test.expected, result) {
    87  			t.Fatalf("%s got\n%T: %v\nexpected\n%T: %v", errMsg, result, result, test.expected, test.expected)
    88  		}
    89  	}
    90  
    91  	_, err := ns.Complement()
    92  	c.Assert(err, qt.Not(qt.IsNil))
    93  	_, err = ns.Complement([]string{"a", "b"})
    94  	c.Assert(err, qt.Not(qt.IsNil))
    95  }