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