| 1 |
// Copyright (c) 2018-2025, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td |
| 8 |
|
| 9 |
// summary(Set): compares the contents of an array or a slice ignoring |
| 10 |
// duplicates and without taking care of the order of items |
| 11 |
// input(Set): array,slice,ptr(ptr on array/slice) |
| 12 |
|
| 13 |
// Set operator compares the contents of an array or a slice (or a |
| 14 |
// pointer on array/slice) ignoring duplicates and without taking care |
| 15 |
// of the order of items. |
| 16 |
// |
| 17 |
// During a match, each expected item should match in the compared |
| 18 |
// array/slice, and each array/slice item should be matched by an |
| 19 |
// expected item to succeed. |
| 20 |
// |
| 21 |
// td.Cmp(t, []int{1, 1, 2}, td.Set(1, 2)) // succeeds |
| 22 |
// td.Cmp(t, []int{1, 1, 2}, td.Set(2, 1)) // succeeds |
| 23 |
// td.Cmp(t, []int{1, 1, 2}, td.Set(1, 2, 3)) // fails, 3 is missing |
| 24 |
// |
| 25 |
// // works with slices/arrays of any type |
| 26 |
// td.Cmp(t, personSlice, td.Set( |
| 27 |
// Person{Name: "Bob", Age: 32}, |
| 28 |
// Person{Name: "Alice", Age: 26}, |
| 29 |
// )) |
| 30 |
// |
| 31 |
// To flatten a non-[]any slice/array, use [Flatten] function |
| 32 |
// and so avoid boring and inefficient copies: |
| 33 |
// |
| 34 |
// expected := []int{2, 1} |
| 35 |
// td.Cmp(t, []int{1, 1, 2}, td.Set(td.Flatten(expected))) // succeeds |
| 36 |
// // = td.Cmp(t, []int{1, 1, 2}, td.Set(2, 1)) |
| 37 |
// |
| 38 |
// exp1 := []int{2, 1} |
| 39 |
// exp2 := []int{5, 8} |
| 40 |
// td.Cmp(t, []int{1, 5, 1, 2, 8, 3, 3}, |
| 41 |
// td.Set(td.Flatten(exp1), 3, td.Flatten(exp2))) // succeeds |
| 42 |
// // = td.Cmp(t, []int{1, 5, 1, 2, 8, 3, 3}, td.Set(2, 1, 3, 5, 8)) |
| 43 |
// |
| 44 |
// TypeBehind method can return a non-nil [reflect.Type] if all items |
| 45 |
// known non-interface types are equal, or if only interface types |
| 46 |
// are found (mostly issued from [Isa]) and they are equal. |
| 47 |
// |
| 48 |
// See also [NotAny], [SubSetOf], [SuperSetOf], [Bag] and [List]. |
| 49 |
func Set(expectedItems ...any) TestDeep { |
| 50 |
return newSetBase(allSet, true, expectedItems) |
| 51 |
} |
| 52 |
|
| 53 |
// summary(SubSetOf): compares the contents of an array or a slice |
| 54 |
// ignoring duplicates and without taking care of the order of items |
| 55 |
// but with potentially some exclusions |
| 56 |
// input(SubSetOf): array,slice,ptr(ptr on array/slice) |
| 57 |
|
| 58 |
// SubSetOf operator compares the contents of an array or a slice (or a |
| 59 |
// pointer on array/slice) ignoring duplicates and without taking care |
| 60 |
// of the order of items. |
| 61 |
// |
| 62 |
// During a match, each array/slice item should be matched by an |
| 63 |
// expected item to succeed. But some expected items can be missing |
| 64 |
// from the compared array/slice. |
| 65 |
// |
| 66 |
// td.Cmp(t, []int{1, 1}, td.SubSetOf(1, 2)) // succeeds |
| 67 |
// td.Cmp(t, []int{1, 1, 2}, td.SubSetOf(1, 3)) // fails, 2 is an extra item |
| 68 |
// |
| 69 |
// // works with slices/arrays of any type |
| 70 |
// td.Cmp(t, personSlice, td.SubSetOf( |
| 71 |
// Person{Name: "Bob", Age: 32}, |
| 72 |
// Person{Name: "Alice", Age: 26}, |
| 73 |
// )) |
| 74 |
// |
| 75 |
// To flatten a non-[]any slice/array, use [Flatten] function |
| 76 |
// and so avoid boring and inefficient copies: |
| 77 |
// |
| 78 |
// expected := []int{2, 1} |
| 79 |
// td.Cmp(t, []int{1, 1}, td.SubSetOf(td.Flatten(expected))) // succeeds |
| 80 |
// // = td.Cmp(t, []int{1, 1}, td.SubSetOf(2, 1)) |
| 81 |
// |
| 82 |
// exp1 := []int{2, 1} |
| 83 |
// exp2 := []int{5, 8} |
| 84 |
// td.Cmp(t, []int{1, 5, 1, 3, 3}, |
| 85 |
// td.SubSetOf(td.Flatten(exp1), 3, td.Flatten(exp2))) // succeeds |
| 86 |
// // = td.Cmp(t, []int{1, 5, 1, 3, 3}, td.SubSetOf(2, 1, 3, 5, 8)) |
| 87 |
// |
| 88 |
// TypeBehind method can return a non-nil [reflect.Type] if all items |
| 89 |
// known non-interface types are equal, or if only interface types |
| 90 |
// are found (mostly issued from [Isa]) and they are equal. |
| 91 |
// |
| 92 |
// See also [NotAny], [Set] and [SuperSetOf]. |
| 93 |
func SubSetOf(expectedItems ...any) TestDeep { |
| 94 |
return newSetBase(subSet, true, expectedItems) |
| 95 |
} |
| 96 |
|
| 97 |
// summary(SuperSetOf): compares the contents of an array or a slice |
| 98 |
// ignoring duplicates and without taking care of the order of items |
| 99 |
// but with potentially some extra items |
| 100 |
// input(SuperSetOf): array,slice,ptr(ptr on array/slice) |
| 101 |
|
| 102 |
// SuperSetOf operator compares the contents of an array or a slice (or |
| 103 |
// a pointer on array/slice) ignoring duplicates and without taking |
| 104 |
// care of the order of items. |
| 105 |
// |
| 106 |
// During a match, each expected item should match in the compared |
| 107 |
// array/slice. But some items in the compared array/slice may not be |
| 108 |
// expected. |
| 109 |
// |
| 110 |
// td.Cmp(t, []int{1, 1, 2}, td.SuperSetOf(1)) // succeeds |
| 111 |
// td.Cmp(t, []int{1, 1, 2}, td.SuperSetOf(1, 3)) // fails, 3 is missing |
| 112 |
// |
| 113 |
// // works with slices/arrays of any type |
| 114 |
// td.Cmp(t, personSlice, td.SuperSetOf( |
| 115 |
// Person{Name: "Bob", Age: 32}, |
| 116 |
// Person{Name: "Alice", Age: 26}, |
| 117 |
// )) |
| 118 |
// |
| 119 |
// To flatten a non-[]any slice/array, use [Flatten] function |
| 120 |
// and so avoid boring and inefficient copies: |
| 121 |
// |
| 122 |
// expected := []int{2, 1} |
| 123 |
// td.Cmp(t, []int{1, 1, 2, 8}, td.SuperSetOf(td.Flatten(expected))) // succeeds |
| 124 |
// // = td.Cmp(t, []int{1, 1, 2, 8}, td.SubSetOf(2, 1)) |
| 125 |
// |
| 126 |
// exp1 := []int{2, 1} |
| 127 |
// exp2 := []int{5, 8} |
| 128 |
// td.Cmp(t, []int{1, 5, 1, 8, 42, 3, 3}, |
| 129 |
// td.SuperSetOf(td.Flatten(exp1), 3, td.Flatten(exp2))) // succeeds |
| 130 |
// // = td.Cmp(t, []int{1, 5, 1, 8, 42, 3, 3}, td.SuperSetOf(2, 1, 3, 5, 8)) |
| 131 |
// |
| 132 |
// TypeBehind method can return a non-nil [reflect.Type] if all items |
| 133 |
// known non-interface types are equal, or if only interface types |
| 134 |
// are found (mostly issued from [Isa]) and they are equal. |
| 135 |
// |
| 136 |
// See also [NotAny], [Set] and [SubSetOf]. |
| 137 |
func SuperSetOf(expectedItems ...any) TestDeep { |
| 138 |
return newSetBase(superSet, true, expectedItems) |
| 139 |
} |
| 140 |
|
| 141 |
// summary(NotAny): compares the contents of an array or a slice, no |
| 142 |
// values have to match |
| 143 |
// input(NotAny): array,slice,ptr(ptr on array/slice) |
| 144 |
|
| 145 |
// NotAny operator checks that the contents of an array or a slice (or |
| 146 |
// a pointer on array/slice) does not contain any of "notExpectedItems". |
| 147 |
// |
| 148 |
// td.Cmp(t, []int{1}, td.NotAny(1, 2, 3)) // fails |
| 149 |
// td.Cmp(t, []int{5}, td.NotAny(1, 2, 3)) // succeeds |
| 150 |
// |
| 151 |
// // works with slices/arrays of any type |
| 152 |
// td.Cmp(t, personSlice, td.NotAny( |
| 153 |
// Person{Name: "Bob", Age: 32}, |
| 154 |
// Person{Name: "Alice", Age: 26}, |
| 155 |
// )) |
| 156 |
// |
| 157 |
// To flatten a non-[]any slice/array, use [Flatten] function |
| 158 |
// and so avoid boring and inefficient copies: |
| 159 |
// |
| 160 |
// notExpected := []int{2, 1} |
| 161 |
// td.Cmp(t, []int{4, 4, 3, 8}, td.NotAny(td.Flatten(notExpected))) // succeeds |
| 162 |
// // = td.Cmp(t, []int{4, 4, 3, 8}, td.NotAny(2, 1)) |
| 163 |
// |
| 164 |
// notExp1 := []int{2, 1} |
| 165 |
// notExp2 := []int{5, 8} |
| 166 |
// td.Cmp(t, []int{4, 4, 42, 8}, |
| 167 |
// td.NotAny(td.Flatten(notExp1), 3, td.Flatten(notExp2))) // succeeds |
| 168 |
// // = td.Cmp(t, []int{4, 4, 42, 8}, td.NotAny(2, 1, 3, 5, 8)) |
| 169 |
// |
| 170 |
// Beware that NotAny(…) is not equivalent to Not(Any(…)) but is like |
| 171 |
// Not(SuperSet(…)). |
| 172 |
// |
| 173 |
// TypeBehind method can return a non-nil [reflect.Type] if all items |
| 174 |
// known non-interface types are equal, or if only interface types |
| 175 |
// are found (mostly issued from [Isa]) and they are equal. |
| 176 |
// |
| 177 |
// See also [Set], [SubSetOf] and [SuperSetOf]. |
| 178 |
func NotAny(notExpectedItems ...any) TestDeep { |
| 179 |
return newSetBase(noneSet, true, notExpectedItems) |
| 180 |
} |