| 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(Bag): compares the contents of an array or a slice without taking |
| 10 |
// care of the order of items |
| 11 |
// input(Bag): array,slice,ptr(ptr on array/slice) |
| 12 |
|
| 13 |
// Bag operator compares the contents of an array or a slice (or a |
| 14 |
// pointer on array/slice) without taking care of the order of items. |
| 15 |
// |
| 16 |
// During a match, each expected item should match in the compared |
| 17 |
// array/slice, and each array/slice item should be matched by an |
| 18 |
// expected item to succeed. |
| 19 |
// |
| 20 |
// td.Cmp(t, []int{1, 1, 2}, td.Bag(1, 1, 2)) // succeeds |
| 21 |
// td.Cmp(t, []int{1, 1, 2}, td.Bag(1, 2, 1)) // succeeds |
| 22 |
// td.Cmp(t, []int{1, 1, 2}, td.Bag(2, 1, 1)) // succeeds |
| 23 |
// td.Cmp(t, []int{1, 1, 2}, td.Bag(1, 2)) // fails, one 1 is missing |
| 24 |
// td.Cmp(t, []int{1, 1, 2}, td.Bag(1, 2, 1, 3)) // fails, 3 is missing |
| 25 |
// |
| 26 |
// // works with slices/arrays of any type |
| 27 |
// td.Cmp(t, personSlice, td.Bag( |
| 28 |
// Person{Name: "Bob", Age: 32}, |
| 29 |
// Person{Name: "Alice", Age: 26}, |
| 30 |
// )) |
| 31 |
// |
| 32 |
// To flatten a non-[]any slice/array, use [Flatten] function |
| 33 |
// and so avoid boring and inefficient copies: |
| 34 |
// |
| 35 |
// expected := []int{1, 2, 1} |
| 36 |
// td.Cmp(t, []int{1, 1, 2}, td.Bag(td.Flatten(expected))) // succeeds |
| 37 |
// // = td.Cmp(t, []int{1, 1, 2}, td.Bag(1, 2, 1)) |
| 38 |
// |
| 39 |
// exp1 := []int{5, 1, 1} |
| 40 |
// exp2 := []int{8, 42, 3} |
| 41 |
// td.Cmp(t, []int{1, 5, 1, 8, 42, 3, 3}, |
| 42 |
// td.Bag(td.Flatten(exp1), 3, td.Flatten(exp2))) // succeeds |
| 43 |
// // = td.Cmp(t, []int{1, 5, 1, 8, 42, 3, 3}, td.Bag(5, 1, 1, 3, 8, 42, 3)) |
| 44 |
// |
| 45 |
// TypeBehind method can return a non-nil [reflect.Type] if all items |
| 46 |
// known non-interface types are equal, or if only interface types |
| 47 |
// are found (mostly issued from Isa()) and they are equal. |
| 48 |
// |
| 49 |
// See also [SubBagOf], [SuperBagOf], [Set], [List] and [Sort]. |
| 50 |
func Bag(expectedItems ...any) TestDeep { |
| 51 |
return newSetBase(allSet, false, expectedItems) |
| 52 |
} |
| 53 |
|
| 54 |
// summary(SubBagOf): compares the contents of an array or a slice |
| 55 |
// without taking care of the order of items but with potentially some |
| 56 |
// exclusions |
| 57 |
// input(SubBagOf): array,slice,ptr(ptr on array/slice) |
| 58 |
|
| 59 |
// SubBagOf operator compares the contents of an array or a slice (or a |
| 60 |
// pointer on array/slice) without taking care 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}, td.SubBagOf(1, 1, 2)) // succeeds |
| 67 |
// td.Cmp(t, []int{1, 1, 1}, td.SubBagOf(1, 1, 2)) // fails, one 1 is an extra item |
| 68 |
// |
| 69 |
// // works with slices/arrays of any type |
| 70 |
// td.Cmp(t, personSlice, td.SubBagOf( |
| 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{1, 2, 1} |
| 79 |
// td.Cmp(t, []int{1}, td.SubBagOf(td.Flatten(expected))) // succeeds |
| 80 |
// // = td.Cmp(t, []int{1}, td.SubBagOf(1, 2, 1)) |
| 81 |
// |
| 82 |
// exp1 := []int{5, 1, 1} |
| 83 |
// exp2 := []int{8, 42, 3} |
| 84 |
// td.Cmp(t, []int{1, 42, 3}, |
| 85 |
// td.SubBagOf(td.Flatten(exp1), 3, td.Flatten(exp2))) // succeeds |
| 86 |
// // = td.Cmp(t, []int{1, 42, 3}, td.SubBagOf(5, 1, 1, 3, 8, 42, 3)) |
| 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 [Bag] and [SuperBagOf]. |
| 93 |
func SubBagOf(expectedItems ...any) TestDeep { |
| 94 |
return newSetBase(subSet, false, expectedItems) |
| 95 |
} |
| 96 |
|
| 97 |
// summary(SuperBagOf): compares the contents of an array or a slice |
| 98 |
// without taking care of the order of items but with potentially some |
| 99 |
// extra items |
| 100 |
// input(SuperBagOf): array,slice,ptr(ptr on array/slice) |
| 101 |
|
| 102 |
// SuperBagOf operator compares the contents of an array or a slice (or a |
| 103 |
// pointer on array/slice) without taking care of the order of items. |
| 104 |
// |
| 105 |
// During a match, each expected item should match in the compared |
| 106 |
// array/slice. But some items in the compared array/slice may not be |
| 107 |
// expected. |
| 108 |
// |
| 109 |
// td.Cmp(t, []int{1, 1, 2}, td.SuperBagOf(1)) // succeeds |
| 110 |
// td.Cmp(t, []int{1, 1, 2}, td.SuperBagOf(1, 1, 1)) // fails, one 1 is missing |
| 111 |
// |
| 112 |
// // works with slices/arrays of any type |
| 113 |
// td.Cmp(t, personSlice, td.SuperBagOf( |
| 114 |
// Person{Name: "Bob", Age: 32}, |
| 115 |
// Person{Name: "Alice", Age: 26}, |
| 116 |
// )) |
| 117 |
// |
| 118 |
// To flatten a non-[]any slice/array, use [Flatten] function |
| 119 |
// and so avoid boring and inefficient copies: |
| 120 |
// |
| 121 |
// expected := []int{1, 2, 1} |
| 122 |
// td.Cmp(t, []int{1}, td.SuperBagOf(td.Flatten(expected))) // succeeds |
| 123 |
// // = td.Cmp(t, []int{1}, td.SuperBagOf(1, 2, 1)) |
| 124 |
// |
| 125 |
// exp1 := []int{5, 1, 1} |
| 126 |
// exp2 := []int{8, 42} |
| 127 |
// td.Cmp(t, []int{1, 5, 1, 8, 42, 3, 3, 6}, |
| 128 |
// td.SuperBagOf(td.Flatten(exp1), 3, td.Flatten(exp2))) // succeeds |
| 129 |
// // = td.Cmp(t, []int{1, 5, 1, 8, 42, 3, 3, 6}, td.SuperBagOf(5, 1, 1, 3, 8, 42)) |
| 130 |
// |
| 131 |
// TypeBehind method can return a non-nil [reflect.Type] if all items |
| 132 |
// known non-interface types are equal, or if only interface types |
| 133 |
// are found (mostly issued from Isa()) and they are equal. |
| 134 |
// |
| 135 |
// See also [Bag] and [SubBagOf]. |
| 136 |
func SuperBagOf(expectedItems ...any) TestDeep { |
| 137 |
return newSetBase(superSet, false, expectedItems) |
| 138 |
} |