|
| 1 | +package be |
| 2 | + |
| 3 | +import ( |
| 4 | + "iter" |
| 5 | + "reflect" |
| 6 | + "slices" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// EqualLength calls t.Fatalf if seq has a length that is not exactly want. |
| 11 | +// The type of seq must be array, array pointer, slice, map, string, channel, [iter.Seq], or [iter.Seq2]. |
| 12 | +// For channels and iterators, the values are consumed to get the sequence length. |
| 13 | +func EqualLength(t testing.TB, want int, seq any) { |
| 14 | + t.Helper() |
| 15 | + getLen(t, seq, want, false) |
| 16 | +} |
| 17 | + |
| 18 | +func getLen(t testing.TB, seq any, n int, atLeast bool) { |
| 19 | + rv := reflect.ValueOf(seq) |
| 20 | + rt := rv.Type() |
| 21 | + kind := rt.Kind() |
| 22 | + switch { |
| 23 | + case slices.Contains([]reflect.Kind{ |
| 24 | + reflect.Array, reflect.Slice, reflect.Map, reflect.String, |
| 25 | + }, kind) || |
| 26 | + (kind == reflect.Pointer && rt.Elem().Kind() == reflect.Array): |
| 27 | + compareN(t, n, rv.Len(), atLeast) |
| 28 | + case kind == reflect.Chan: |
| 29 | + eqchan(t, rv, n, atLeast) |
| 30 | + case kind == reflect.Func && rt.CanSeq(): |
| 31 | + eqseq(t, rv, n, atLeast) |
| 32 | + case kind == reflect.Func && rt.CanSeq2(): |
| 33 | + eqseq2(t, rv, n, atLeast) |
| 34 | + default: |
| 35 | + panic("seq must be a non-integer rangeable type") |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func eqchan(t testing.TB, rv reflect.Value, n int, atLeast bool) { |
| 40 | + i := 0 |
| 41 | + for ; i <= n; i++ { |
| 42 | + chosen, _, recvOK := reflect.Select([]reflect.SelectCase{ |
| 43 | + {Dir: reflect.SelectRecv, Chan: rv}, |
| 44 | + {Dir: reflect.SelectDefault}, |
| 45 | + }) |
| 46 | + |
| 47 | + if chosen == 1 || !recvOK { // default case or channel closed |
| 48 | + break |
| 49 | + } |
| 50 | + } |
| 51 | + compareN(t, n, i, atLeast) |
| 52 | +} |
| 53 | + |
| 54 | +func eqseq(t testing.TB, rv reflect.Value, n int, atLeast bool) { |
| 55 | + next, stop := iter.Pull(rv.Seq()) |
| 56 | + defer stop() |
| 57 | + i := 0 |
| 58 | + for ; i <= n; i++ { |
| 59 | + if _, ok := next(); !ok { |
| 60 | + break |
| 61 | + } |
| 62 | + } |
| 63 | + compareN(t, n, i, atLeast) |
| 64 | +} |
| 65 | + |
| 66 | +func eqseq2(t testing.TB, rv reflect.Value, n int, atLeast bool) { |
| 67 | + next, stop := iter.Pull2(rv.Seq2()) |
| 68 | + defer stop() |
| 69 | + i := 0 |
| 70 | + for ; i <= n; i++ { |
| 71 | + if _, _, ok := next(); !ok { |
| 72 | + break |
| 73 | + } |
| 74 | + } |
| 75 | + compareN(t, n, i, atLeast) |
| 76 | +} |
| 77 | + |
| 78 | +func compareN(t testing.TB, want, got int, atLeast bool) { |
| 79 | + switch { |
| 80 | + case atLeast && want > got: |
| 81 | + t.Fatalf("want len(seq) >= %d; got %d", want, got) |
| 82 | + case !atLeast && want > got: |
| 83 | + t.Fatalf("want len(seq) == %d; got %d", want, got) |
| 84 | + case !atLeast && want < got: |
| 85 | + t.Fatalf("want len(seq) == %d; got at least %d", want, got) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// AtLeastLength calls t.Fatalf if seq has a length that is not at least want. |
| 90 | +// The type of seq must be array, array pointer, slice, map, string, channel, [iter.Seq], or [iter.Seq2]. |
| 91 | +// For channels and iterators, the values are consumed to get the sequence length. |
| 92 | +func AtLeastLength(t testing.TB, want int, seq any) { |
| 93 | + t.Helper() |
| 94 | + getLen(t, seq, want, true) |
| 95 | +} |
0 commit comments