-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror_test.go
49 lines (43 loc) · 1.24 KB
/
mirror_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
// mirror - check ability to convert two types using unsafe
//
// Copyright 2013 Arne Hormann. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
package mirror
import (
"reflect"
"testing"
)
// TODO:
// - each possible type, first with single-type structs
// - interfaces in struct
// - recursion
// the current state is more of a smoke test, but it's also tested by usage in the other packages
type test struct {
name string
ok bool
a interface{}
b interface{}
}
func TestMirror(t *testing.T) {
tests := []test{
test{"empty", true, struct{}{}, struct{}{}},
test{"uint8", true, struct{ a uint8 }{}, struct{ a uint8 }{}},
test{"wrong name", false, struct{ a uint8 }{}, struct{ b uint8 }{}},
test{"wrong type", false, struct{ a uint8 }{}, struct{ a *uint8 }{}},
}
for _, x := range tests {
ta, tb := reflect.TypeOf(x.a), reflect.TypeOf(x.b)
if x.ok != CanConvert(ta, tb) {
if x.ok {
t.Errorf("%s: could convert [%v] to [%v]\n",
x.name, ta, tb)
} else {
t.Errorf("%s: could not convert [%v] to [%v]\n",
x.name, ta, tb)
}
}
}
}