Debian Codegraph @

td/td_isa_test.go

raw
1 // Copyright (c) 2018, 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 "bytes"
11 "fmt"
12 "testing"
13
14 "github.com/maxatome/go-testdeep/internal/test"
15 "github.com/maxatome/go-testdeep/td"
16 )
17
18 func TestIsa(t *testing.T) {
19 gotStruct := MyStruct{
20 MyStructMid: MyStructMid{
21 MyStructBase: MyStructBase{
22 ValBool: true,
23 },
24 ValStr: "foobar",
25 },
26 ValInt: 123,
27 }
28
29 checkOK(t, &gotStruct, td.Isa(&MyStruct{}))
30 checkOK(t, (*MyStruct)(nil), td.Isa(&MyStruct{}))
31 checkOK(t, (*MyStruct)(nil), td.Isa((*MyStruct)(nil)))
32 checkOK(t, gotStruct, td.Isa(MyStruct{}))
33
34 checkOK(t, bytes.NewBufferString("foobar"),
35 td.Isa((*fmt.Stringer)(nil)),
36 "checks bytes.NewBufferString() implements fmt.Stringer")
37
38 // does bytes.NewBufferString("foobar") implements fmt.Stringer?
39 checkOK(t, bytes.NewBufferString("foobar"), td.Isa((*fmt.Stringer)(nil)))
40
41 checkError(t, &gotStruct, td.Isa(&MyStructBase{}),
42 expectedError{
43 Message: mustBe("type mismatch"),
44 Path: mustBe("DATA"),
45 Got: mustContain("*td_test.MyStruct"),
46 Expected: mustContain("*td_test.MyStructBase"),
47 })
48
49 checkError(t, (*MyStruct)(nil), td.Isa(&MyStructBase{}),
50 expectedError{
51 Message: mustBe("type mismatch"),
52 Path: mustBe("DATA"),
53 Got: mustContain("*td_test.MyStruct"),
54 Expected: mustContain("*td_test.MyStructBase"),
55 })
56
57 checkError(t, gotStruct, td.Isa(&MyStruct{}),
58 expectedError{
59 Message: mustBe("type mismatch"),
60 Path: mustBe("DATA"),
61 Got: mustContain("td_test.MyStruct"),
62 Expected: mustContain("*td_test.MyStruct"),
63 })
64
65 checkError(t, &gotStruct, td.Isa(MyStruct{}),
66 expectedError{
67 Message: mustBe("type mismatch"),
68 Path: mustBe("DATA"),
69 Got: mustContain("*td_test.MyStruct"),
70 Expected: mustContain("td_test.MyStruct"),
71 })
72
73 gotSlice := []int{1, 2, 3}
74 checkOK(t, gotSlice, td.Isa([]int{}))
75 checkOK(t, &gotSlice, td.Isa(((*[]int)(nil))))
76
77 checkError(t, &gotSlice, td.Isa([]int{}),
78 expectedError{
79 Message: mustBe("type mismatch"),
80 Path: mustBe("DATA"),
81 Got: mustContain("*[]int"),
82 Expected: mustContain("[]int"),
83 })
84
85 checkError(t, gotSlice, td.Isa((*[]int)(nil)),
86 expectedError{
87 Message: mustBe("type mismatch"),
88 Path: mustBe("DATA"),
89 Got: mustContain("[]int"),
90 Expected: mustContain("*[]int"),
91 })
92
93 checkError(t, gotSlice, td.Isa([1]int{2}),
94 expectedError{
95 Message: mustBe("type mismatch"),
96 Path: mustBe("DATA"),
97 Got: mustContain("[]int"),
98 Expected: mustContain("[1]int"),
99 })
100
101 //
102 // Bad usage
103 checkError(t, "never tested",
104 td.Isa(nil),
105 expectedError{
106 Message: mustBe("bad usage of Isa operator"),
107 Path: mustBe("DATA"),
108 Summary: mustBe("Isa(nil) is not allowed. To check an interface, try Isa((*fmt.Stringer)(nil)), for fmt.Stringer for example"),
109 })
110
111 //
112 // String
113 test.EqualStr(t, td.Isa((*MyStruct)(nil)).String(),
114 "*td_test.MyStruct")
115
116 // Erroneous op
117 test.EqualStr(t, td.Isa(nil).String(), "Isa(<ERROR>)")
118 }
119
120 func TestIsaTypeBehind(t *testing.T) {
121 equalTypes(t, td.Isa(([]int)(nil)), []int{})
122
123 equalTypes(t, td.Isa((*fmt.Stringer)(nil)), (*fmt.Stringer)(nil))
124
125 // Erroneous op
126 equalTypes(t, td.Isa(nil), nil)
127 }