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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package reflect_test
import (
"bytes"
"go/ast"
"io"
. "reflect"
"testing"
"unsafe"
)
type MyBuffer bytes.Buffer
func TestImplicitMapConversion(t *testing.T) {
// Test implicit conversions in MapIndex and SetMapIndex.
{
// direct
m := make(map[int]int)
mv := ValueOf(m)
mv.SetMapIndex(ValueOf(1), ValueOf(2))
x, ok := m[1]
if x != 2 {
t.Errorf("#1 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
}
if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
t.Errorf("#1 MapIndex(1) = %d", n)
}
}
{
// convert interface key
m := make(map[interface{}]int)
mv := ValueOf(m)
mv.SetMapIndex(ValueOf(1), ValueOf(2))
x, ok := m[1]
if x != 2 {
t.Errorf("#2 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
}
if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
t.Errorf("#2 MapIndex(1) = %d", n)
}
}
{
// convert interface value
m := make(map[int]interface{})
mv := ValueOf(m)
mv.SetMapIndex(ValueOf(1), ValueOf(2))
x, ok := m[1]
if x != 2 {
t.Errorf("#3 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
}
if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
t.Errorf("#3 MapIndex(1) = %d", n)
}
}
{
// convert both interface key and interface value
m := make(map[interface{}]interface{})
mv := ValueOf(m)
mv.SetMapIndex(ValueOf(1), ValueOf(2))
x, ok := m[1]
if x != 2 {
t.Errorf("#4 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
}
if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
t.Errorf("#4 MapIndex(1) = %d", n)
}
}
{
// convert both, with non-empty interfaces
m := make(map[io.Reader]io.Writer)
mv := ValueOf(m)
b1 := new(bytes.Buffer)
b2 := new(bytes.Buffer)
mv.SetMapIndex(ValueOf(b1), ValueOf(b2))
x, ok := m[b1]
if x != b2 {
t.Errorf("#5 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x, b2, ok, m)
}
if p := mv.MapIndex(ValueOf(b1)).Elem().Pointer(); p != uintptr(unsafe.Pointer(b2)) {
t.Errorf("#5 MapIndex(b1) = %#x want %p", p, b2)
}
}
{
// convert channel direction
m := make(map[<-chan int]chan int)
mv := ValueOf(m)
c1 := make(chan int)
c2 := make(chan int)
mv.SetMapIndex(ValueOf(c1), ValueOf(c2))
x, ok := m[c1]
if x != c2 {
t.Errorf("#6 after SetMapIndex(c1, c2): %p (!= %p), %t (map=%v)", x, c2, ok, m)
}
if p := mv.MapIndex(ValueOf(c1)).Pointer(); p != ValueOf(c2).Pointer() {
t.Errorf("#6 MapIndex(c1) = %#x want %p", p, c2)
}
}
{
// convert identical underlying types
// TODO(rsc): Should be able to define MyBuffer here.
// 6l prints very strange messages about .this.Bytes etc
// when we do that though, so MyBuffer is defined
// at top level.
m := make(map[*MyBuffer]*bytes.Buffer)
mv := ValueOf(m)
b1 := new(MyBuffer)
b2 := new(bytes.Buffer)
mv.SetMapIndex(ValueOf(b1), ValueOf(b2))
x, ok := m[b1]
if x != b2 {
t.Errorf("#7 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x, b2, ok, m)
}
if p := mv.MapIndex(ValueOf(b1)).Pointer(); p != uintptr(unsafe.Pointer(b2)) {
t.Errorf("#7 MapIndex(b1) = %#x want %p", p, b2)
}
}
}
func TestImplicitSetConversion(t *testing.T) {
// Assume TestImplicitMapConversion covered the basics.
// Just make sure conversions are being applied at all.
var r io.Reader
b := new(bytes.Buffer)
rv := ValueOf(&r).Elem()
rv.Set(ValueOf(b))
if r != b {
t.Errorf("after Set: r=%T(%v)", r, r)
}
}
func TestImplicitSendConversion(t *testing.T) {
c := make(chan io.Reader, 10)
b := new(bytes.Buffer)
ValueOf(c).Send(ValueOf(b))
if bb := <-c; bb != b {
t.Errorf("Received %p != %p", bb, b)
}
}
func TestImplicitCallConversion(t *testing.T) {
// Arguments must be assignable to parameter types.
fv := ValueOf(io.WriteString)
b := new(bytes.Buffer)
fv.Call([]Value{ValueOf(b), ValueOf("hello world")})
if b.String() != "hello world" {
t.Errorf("After call: string=%q want %q", b.String(), "hello world")
}
}
func TestImplicitAppendConversion(t *testing.T) {
// Arguments must be assignable to the slice's element type.
s := []io.Reader{}
sv := ValueOf(&s).Elem()
b := new(bytes.Buffer)
sv.Set(Append(sv, ValueOf(b)))
if len(s) != 1 || s[0] != b {
t.Errorf("after append: s=%v want [%p]", s, b)
}
}
var implementsTests = []struct {
x interface{}
t interface{}
b bool
}{
{new(*bytes.Buffer), new(io.Reader), true},
{new(bytes.Buffer), new(io.Reader), false},
{new(*bytes.Buffer), new(io.ReaderAt), false},
{new(*ast.Ident), new(ast.Expr), true},
}
func TestImplements(t *testing.T) {
for _, tt := range implementsTests {
xv := TypeOf(tt.x).Elem()
xt := TypeOf(tt.t).Elem()
if b := xv.Implements(xt); b != tt.b {
t.Errorf("(%s).Implements(%s) = %v, want %v", xv.String(), xt.String(), b, tt.b)
}
}
}
var assignableTests = []struct {
x interface{}
t interface{}
b bool
}{
{new(chan int), new(<-chan int), true},
{new(<-chan int), new(chan int), false},
{new(*int), new(IntPtr), true},
{new(IntPtr), new(*int), true},
{new(IntPtr), new(IntPtr1), false},
// test runs implementsTests too
}
type IntPtr *int
type IntPtr1 *int
func TestAssignableTo(t *testing.T) {
for _, tt := range append(assignableTests, implementsTests...) {
xv := TypeOf(tt.x).Elem()
xt := TypeOf(tt.t).Elem()
if b := xv.AssignableTo(xt); b != tt.b {
t.Errorf("(%s).AssignableTo(%s) = %v, want %v", xv.String(), xt.String(), b, tt.b)
}
}
}
|