-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcast_test.go
101 lines (76 loc) · 1.82 KB
/
cast_test.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package jsii
import (
"reflect"
"testing"
"github.com/aws/jsii-runtime-go/internal/api"
"github.com/aws/jsii-runtime-go/internal/kernel"
)
type MockInterfaceABase interface {
MockMethodABase(_ float64)
}
type mockABase struct {
_ int // padding
}
func (m *mockABase) MockMethodABase(_ float64) {}
type MockInterfaceA interface {
MockInterfaceABase
MockMethodA(_ string)
}
func NewMockInterfaceA() MockInterfaceA {
return &mockA{mockABase{}}
}
type mockA struct {
mockABase
}
func (m *mockA) MockMethodA(_ string) {}
type MockInterfaceB interface {
MockMethodB(_ int)
}
func NewMockInterfaceB() MockInterfaceB {
return &mockB{}
}
type mockB struct {
_ int // Padding
}
func (m *mockB) MockMethodB(_ int) {}
func TestNilSource(t *testing.T) {
// Make "into" not nil to ensure the cast function overwrites it.
into := NewMockInterfaceB()
UnsafeCast(nil, &into)
if into != nil {
t.Fail()
}
}
func TestSourceAndTargetAreTheSame(t *testing.T) {
into := NewMockInterfaceB()
original := into
UnsafeCast(into, &into)
if into != original {
t.Fail()
}
}
func TestTargetIsSubclassOfSource(t *testing.T) {
from := NewMockInterfaceA()
var into MockInterfaceABase
UnsafeCast(from, &into)
if into != from {
t.Fail()
}
}
func TestRegistersAlias(t *testing.T) {
client := kernel.GetClient()
objid := api.ObjectRef{InstanceID: "Object@1337#42"}
from := NewMockInterfaceA()
client.RegisterInstance(reflect.ValueOf(from), objid)
var into MockInterfaceB
client.Types().RegisterInterface(api.FQN("mock.InterfaceB"), reflect.TypeOf(&into).Elem(), []api.Override{}, func() interface{} { return NewMockInterfaceB() })
UnsafeCast(from, &into)
if into == nil {
t.Fail()
}
if refid, found := client.FindObjectRef(reflect.ValueOf(into)); !found {
t.Fail()
} else if refid.InstanceID != objid.InstanceID {
t.Fail()
}
}