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