Debian Codegraph @

td/td_list_test.go

raw
1 // Copyright (c) 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_test
8
9 import (
10 "fmt"
11 "reflect"
12 "testing"
13
14 "github.com/maxatome/go-testdeep/internal/test"
15 "github.com/maxatome/go-testdeep/td"
16 )
17
18 func TestList(t *testing.T) {
19 type MyArray [5]int
20 type MySlice []int
21
22 for idx, got := range []any{
23 []int{1, 3, 4, 4, 5},
24 [...]int{1, 3, 4, 4, 5},
25 MySlice{1, 3, 4, 4, 5},
26 MyArray{1, 3, 4, 4, 5},
27 &MySlice{1, 3, 4, 4, 5},
28 &MyArray{1, 3, 4, 4, 5},
29 } {
30 testName := fmt.Sprintf("Test #%d → %v", idx, got)
31
32 checkOK(t, got, td.List(1, 3, 4, 4, 5), testName)
33
34 typ := reflect.TypeOf(got)
35 if typ.Kind() == reflect.Ptr {
36 typ = typ.Elem()
37 }
38
39 checkError(t, got, td.List(1, 3, 4),
40 expectedError{
41 Message: mustBe(fmt.Sprintf("comparing %s, from index #3", typ.Kind())),
42 Path: mustBe("DATA"),
43 Summary: mustBe("Extra 2 items: (4,\n 5)"),
44 },
45 testName)
46
47 checkError(t, got, td.List(1, 3, 4, 4, 5, 666),
48 expectedError{
49 Message: mustBe(fmt.Sprintf("comparing %s, from index #5", typ.Kind())),
50 Path: mustBe("DATA"),
51 Summary: mustBe("Missing item: (666)"),
52 },
53 testName)
54
55 checkError(t, got, td.List(1, 3, 666, 4, 5),
56 expectedError{
57 Message: mustBe("values differ"),
58 Path: mustBe("DATA[2]"),
59 Got: mustBe("4"),
60 Expected: mustBe("666"),
61 },
62 testName)
63
64 // Lax
65 checkOK(t, got, td.Lax(td.Bag(float64(1), 3, 4, 4, uint8(5))), testName)
66 }
67
68 var nilSlice MySlice
69 for idx, got := range []any{([]int)(nil), &nilSlice} {
70 checkOK(t, got, td.List(), "Test #%d", idx)
71 }
72
73 checkError(t, 123, td.List(123),
74 expectedError{
75 Message: mustBe("bad kind"),
76 Path: mustBe("DATA"),
77 Got: mustBe("int"),
78 Expected: mustBe("slice OR array OR *slice OR *array"),
79 })
80
81 num := 123
82 checkError(t, &num, td.List(123),
83 expectedError{
84 Message: mustBe("bad kind"),
85 Path: mustBe("DATA"),
86 Got: mustBe("*int"),
87 Expected: mustBe("slice OR array OR *slice OR *array"),
88 })
89
90 var list *MySlice
91 checkError(t, list, td.List(123),
92 expectedError{
93 Message: mustBe("nil pointer"),
94 Path: mustBe("DATA"),
95 Got: mustBe("nil *slice (*td_test.MySlice type)"),
96 Expected: mustBe("non-nil *slice OR *array"),
97 })
98
99 checkError(t, nil, td.List(123),
100 expectedError{
101 Message: mustBe("bad kind"),
102 Path: mustBe("DATA"),
103 Got: mustBe("nil"),
104 Expected: mustBe("slice OR array OR *slice OR *array"),
105 })
106
107 //
108 // String
109 test.EqualStr(t, td.List(1).String(), "List(1)")
110 test.EqualStr(t, td.List(1, 2).String(), "List(1,\n 2)")
111 }
112
113 func TestListTypeBehind(t *testing.T) {
114 equalTypes(t, td.List(6, 5), ([]int)(nil))
115 equalTypes(t, td.List(6, "foo"), nil)
116
117 // Always the same non-interface type (even if we encounter several
118 // interface types)
119 equalTypes(t,
120 td.List(
121 td.Empty(),
122 5,
123 td.Isa((*error)(nil)), // interface type (in fact pointer to ...)
124 td.All(6, 7),
125 td.Isa((*fmt.Stringer)(nil)), // interface type
126 8),
127 ([]int)(nil))
128
129 // Only one interface type
130 equalTypes(t,
131 td.List(
132 td.Isa((*error)(nil)),
133 td.Isa((*error)(nil)),
134 td.Isa((*error)(nil)),
135 ),
136 ([]*error)(nil))
137
138 // Several interface types, cannot be sure
139 equalTypes(t,
140 td.List(
141 td.Isa((*error)(nil)),
142 td.Isa((*fmt.Stringer)(nil)),
143 ),
144 nil)
145 }