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