| 1 |
// Copyright (c) 2020, 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 flat_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/maxatome/go-testdeep/internal/flat" |
| 13 |
"github.com/maxatome/go-testdeep/internal/test" |
| 14 |
) |
| 15 |
|
| 16 |
func TestLen(t *testing.T) { |
| 17 |
num, flattened := flat.Len([]any{1, 2, 3, 4}) |
| 18 |
test.EqualInt(t, num, 4) |
| 19 |
test.IsTrue(t, flattened) |
| 20 |
|
| 21 |
num, flattened = flat.Len([]any{ |
| 22 |
1, 2, |
| 23 |
flat.Slice{Slice: []int{3, 4, 5, 6}}, |
| 24 |
flat.Slice{Slice: map[int]int{-1: -2, -3: -4}}, |
| 25 |
7, |
| 26 |
flat.Slice{ |
| 27 |
Slice: []any{ |
| 28 |
flat.Slice{Slice: []int{8, 9}}, |
| 29 |
flat.Slice{Slice: []int{10, 11}}, |
| 30 |
flat.Slice{Slice: map[int]any{ |
| 31 |
-5: -6, |
| 32 |
-7: flat.Slice{Slice: []int{-8, -9, -10}}, |
| 33 |
}}, |
| 34 |
flat.Slice{Slice: 8}, |
| 35 |
}, |
| 36 |
}, |
| 37 |
12, |
| 38 |
flat.Slice{Slice: map[any]any{ |
| 39 |
-11: flat.Slice{Slice: []int{-12, -13}}, |
| 40 |
10: flat.Slice{Slice: 3}, |
| 41 |
}}, |
| 42 |
}) |
| 43 |
test.EqualInt(t, num, 2+4+4+1+(2+2+6+8)+1+7) |
| 44 |
test.IsFalse(t, flattened) |
| 45 |
} |
| 46 |
|
| 47 |
func TestValues(t *testing.T) { |
| 48 |
sv := flat.Values(nil) |
| 49 |
test.IsTrue(t, sv != nil) |
| 50 |
test.EqualInt(t, len(sv), 0) |
| 51 |
|
| 52 |
sv = flat.Values([]any{1, 2}) |
| 53 |
if test.EqualInt(t, len(sv), 2) { |
| 54 |
test.EqualInt(t, int(sv[0].Int()), 1) |
| 55 |
test.EqualInt(t, int(sv[1].Int()), 2) |
| 56 |
} |
| 57 |
|
| 58 |
sv = flat.Values([]any{ |
| 59 |
flat.Slice{Slice: 3}, |
| 60 |
flat.Slice{Slice: []int{3, 4, 5, 6}}, |
| 61 |
7, |
| 62 |
flat.Slice{ |
| 63 |
Slice: []any{ |
| 64 |
flat.Slice{Slice: []int{8, 9}}, |
| 65 |
flat.Slice{Slice: []any{10, 11}}, |
| 66 |
12, |
| 67 |
13, |
| 68 |
flat.Slice{Slice: 0}, |
| 69 |
}, |
| 70 |
}, |
| 71 |
14, |
| 72 |
flat.Slice{ |
| 73 |
Slice: map[int]any{ |
| 74 |
15: flat.Slice{Slice: map[int]int{16: 17}}, |
| 75 |
}, |
| 76 |
}, |
| 77 |
}) |
| 78 |
if test.EqualInt(t, len(sv), 18) { |
| 79 |
for i, v := range sv { |
| 80 |
test.EqualInt(t, int(v.Int()), i) |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func TestInterfaces(t *testing.T) { |
| 86 |
si := flat.Interfaces() |
| 87 |
test.IsTrue(t, si == nil) |
| 88 |
|
| 89 |
si = flat.Interfaces(1, 2) |
| 90 |
if test.EqualInt(t, len(si), 2) { |
| 91 |
test.EqualInt(t, si[0].(int), 1) |
| 92 |
test.EqualInt(t, si[1].(int), 2) |
| 93 |
} |
| 94 |
|
| 95 |
si = flat.Interfaces( |
| 96 |
flat.Slice{Slice: 3}, |
| 97 |
flat.Slice{Slice: []int{3, 4, 5, 6}}, |
| 98 |
7, |
| 99 |
flat.Slice{ |
| 100 |
Slice: []any{ |
| 101 |
flat.Slice{Slice: []int{8, 9}}, |
| 102 |
flat.Slice{Slice: []any{10, 11}}, |
| 103 |
12, |
| 104 |
13, |
| 105 |
flat.Slice{Slice: 0}, |
| 106 |
}, |
| 107 |
}, |
| 108 |
14, |
| 109 |
flat.Slice{ |
| 110 |
Slice: map[int]any{ |
| 111 |
15: flat.Slice{Slice: map[int]int{16: 17}}, |
| 112 |
}, |
| 113 |
}, |
| 114 |
) |
| 115 |
if test.EqualInt(t, len(si), 18) { |
| 116 |
for i, iface := range si { |
| 117 |
test.EqualInt(t, iface.(int), i) |
| 118 |
} |
| 119 |
} |
| 120 |
} |