Debian Codegraph @

internal/runtime/std_package_test.go

raw
1 package runtime
2
3 import (
4 "math/big"
5 "net/netip"
6 "reflect"
7 "testing"
8 "time"
9 )
10
11 type promotedTime struct{ time.Time }
12
13 type promotedTimeAfterField struct {
14 N int64
15 time.Time
16 }
17
18 type promotedTimePointer struct{ *time.Time }
19
20 type promotedTimeNested struct {
21 A, B int64
22 promotedTimeAfterField
23 }
24
25 type promotedTimeThroughPointer struct {
26 N int64
27 *promotedTimeAfterField
28 }
29
30 // promotedThroughEmbeddedPointer embeds a pointer to a type which embeds time.Time.
31 type promotedThroughEmbeddedPointer struct{ *promotedTime }
32
33 type promotedBigInt struct{ big.Int }
34
35 type declaredOnValue struct{ time.Time }
36
37 func (declaredOnValue) MarshalJSON() ([]byte, error) { return nil, nil }
38
39 type declaredOnPointer struct{ time.Time }
40
41 func (*declaredOnPointer) MarshalJSON() ([]byte, error) { return nil, nil }
42
43 type declaredUnmarshal struct{ time.Time }
44
45 func (*declaredUnmarshal) UnmarshalJSON([]byte) error { return nil }
46
47 type userMarshaler struct{}
48
49 func (userMarshaler) MarshalJSON() ([]byte, error) { return nil, nil }
50
51 type promotedUser struct{ userMarshaler }
52
53 // promotedTwice has the MarshalJSON of time.Time, and no MarshalText: both embedded types have one.
54 type promotedTwice struct {
55 time.Time
56 netip.Addr
57 }
58
59 type promotedNestedDeclared struct{ declaredOnValue }
60
61 // A method which a type has only by embedding a type of the standard library is found, with the place of the
62 // embedded value; a method which the type, or a type between, declares is not, whatever its receiver.
63 func TestPromotedStdMethod(t *testing.T) {
64 timeType := reflect.TypeOf(time.Time{})
65 tests := []struct {
66 typ reflect.Type
67 name string
68 ok bool
69 origin reflect.Type
70 offset uintptr
71 inline bool
72 }{
73 {reflect.TypeOf(promotedTime{}), "MarshalJSON", true, timeType, 0, true},
74 {reflect.TypeOf(&promotedTime{}), "MarshalJSON", true, timeType, 0, true},
75 {reflect.TypeOf(promotedTime{}), "MarshalText", true, timeType, 0, true},
76 {reflect.TypeOf(&promotedTime{}), "UnmarshalJSON", true, timeType, 0, true},
77 {reflect.TypeOf(struct{ time.Time }{}), "MarshalJSON", true, timeType, 0, true},
78 {reflect.TypeOf(promotedTimeAfterField{}), "MarshalJSON", true, timeType, 8, true},
79 {reflect.TypeOf(promotedTimeNested{}), "MarshalJSON", true, timeType, 24, true},
80 {reflect.TypeOf(promotedTimePointer{}), "MarshalJSON", true, reflect.PointerTo(timeType), 0, false},
81 {reflect.TypeOf(promotedTimeThroughPointer{}), "MarshalJSON", true, timeType, 16, false},
82 {reflect.TypeOf(promotedThroughEmbeddedPointer{}), "MarshalJSON", true, timeType, 0, false},
83 {reflect.TypeOf(&promotedThroughEmbeddedPointer{}), "MarshalJSON", true, timeType, 0, false},
84 {reflect.TypeOf(&promotedBigInt{}), "MarshalJSON", true, reflect.TypeOf(big.Int{}), 0, true},
85 {reflect.TypeOf(declaredOnValue{}), "MarshalJSON", false, nil, 0, false},
86 {reflect.TypeOf(&declaredOnValue{}), "MarshalJSON", false, nil, 0, false},
87 {reflect.TypeOf(&declaredOnPointer{}), "MarshalJSON", false, nil, 0, false},
88 {reflect.TypeOf(&declaredUnmarshal{}), "UnmarshalJSON", false, nil, 0, false},
89 {reflect.TypeOf(&declaredUnmarshal{}), "MarshalJSON", true, timeType, 0, true},
90 {reflect.TypeOf(promotedUser{}), "MarshalJSON", false, nil, 0, false},
91 {reflect.TypeOf(promotedTwice{}), "MarshalJSON", true, timeType, 0, true},
92 {reflect.TypeOf(promotedTwice{}), "MarshalText", false, nil, 0, false},
93 {reflect.TypeOf(promotedNestedDeclared{}), "MarshalJSON", false, nil, 0, false},
94 {timeType, "MarshalJSON", false, nil, 0, false},
95 {reflect.TypeOf(0), "MarshalJSON", false, nil, 0, false},
96 }
97 lookups := map[string]MethodLookup{
98 "MarshalJSON": MarshalJSONMethod, "MarshalText": MarshalTextMethod, "UnmarshalJSON": UnmarshalJSONMethod,
99 }
100 for _, test := range tests {
101 origin, offset, inline, ok := PromotedStdMethod(test.typ, lookups[test.name])
102 if ok != test.ok || origin != test.origin || offset != test.offset || inline != test.inline {
103 t.Errorf("%v.%s: got (%v, %d, %v, %v), want (%v, %d, %v, %v)", test.typ, test.name,
104 origin, offset, inline, ok, test.origin, test.offset, test.inline, test.ok)
105 }
106 }
107 }
108
109 // The wrapper of a promoted method is told from a declared method by the file of its code: it would be told no
110 // more if the compiler named the file otherwise, and the methods would be called and checked again.
111 func TestIsGeneratedCode(t *testing.T) {
112 promoted, _ := reflect.TypeOf(promotedTime{}).MethodByName("MarshalJSON")
113 declared, _ := reflect.TypeOf(declaredOnValue{}).MethodByName("MarshalJSON")
114 if !isGeneratedCode(promoted.Func.Pointer()) {
115 t.Fatal("the wrapper of a promoted method is not found as generated code")
116 }
117 if isGeneratedCode(declared.Func.Pointer()) {
118 t.Fatal("a declared method is found as generated code")
119 }
120 if isGeneratedCode(0) {
121 t.Fatal("no code is found as generated code")
122 }
123 }