-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathreflect_test.go
More file actions
64 lines (48 loc) · 1.45 KB
/
reflect_test.go
File metadata and controls
64 lines (48 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.25
package formatter
import (
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
type dummy struct{}
func (*dummy) Func1() string {
return "Func1"
}
func (*dummy) func2() string { //nolint:unused
return "func2(should not be marshalled)"
}
func (*dummy) Func3() (string, int) {
return "Func3(should not be marshalled)", -42
}
func (*dummy) Func4() int {
return 4
}
type dummyType string
func (*dummy) Func5() dummyType {
return "Func5"
}
func (*dummy) FullHeader() string {
return "FullHeader(should not be marshalled)"
}
var dummyExpected = map[string]any{
"Func1": "Func1",
"Func4": 4,
"Func5": dummyType("Func5"),
}
func TestMarshalMap(t *testing.T) {
d := dummy{}
m, err := marshalMap(&d)
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(m, dummyExpected))
}
func TestMarshalMapBad(t *testing.T) {
_, err := marshalMap(nil)
assert.Check(t, is.Error(err, "expected a pointer to a struct, got invalid"), "expected an error (argument is nil)")
_, err = marshalMap(dummy{})
assert.Check(t, is.Error(err, "expected a pointer to a struct, got struct"), "expected an error (argument is non-pointer)")
x := 42
_, err = marshalMap(&x)
assert.Check(t, is.Error(err, "expected a pointer to a struct, got a pointer to int"), "expected an error (argument is a pointer to non-struct)")
}