| 1 |
// Copyright 2013 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package types_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/parser" |
| 9 |
"testing" |
| 10 |
|
| 11 |
. "go/types" |
| 12 |
) |
| 13 |
|
| 14 |
var testExprs = []testEntry{ |
| 15 |
// basic type literals |
| 16 |
dup("x"), |
| 17 |
dup("true"), |
| 18 |
dup("42"), |
| 19 |
dup("3.1415"), |
| 20 |
dup("2.71828i"), |
| 21 |
dup(`'a'`), |
| 22 |
dup(`"foo"`), |
| 23 |
dup("`bar`"), |
| 24 |
dup("any"), |
| 25 |
|
| 26 |
// func and composite literals |
| 27 |
{"func(){}", "(func() literal)"}, |
| 28 |
{"func(x int) complex128 {}", "(func(x int) complex128 literal)"}, |
| 29 |
{"[]int{1, 2, 3}", "[]int{…}"}, |
| 30 |
|
| 31 |
// type expressions |
| 32 |
dup("[1 << 10]byte"), |
| 33 |
dup("[]int"), |
| 34 |
dup("*int"), |
| 35 |
dup("struct{x int}"), |
| 36 |
dup("func()"), |
| 37 |
dup("func(int, float32) string"), |
| 38 |
dup("interface{m()}"), |
| 39 |
dup("interface{m() string; n(x int)}"), |
| 40 |
dup("interface{~int}"), |
| 41 |
|
| 42 |
dup("map[string]int"), |
| 43 |
dup("chan E"), |
| 44 |
dup("<-chan E"), |
| 45 |
dup("chan<- E"), |
| 46 |
|
| 47 |
// new interfaces |
| 48 |
dup("interface{int}"), |
| 49 |
dup("interface{~int}"), |
| 50 |
|
| 51 |
// generic constraints |
| 52 |
dup("interface{~a | ~b | ~c; ~int | ~string; float64; m()}"), |
| 53 |
dup("interface{int | string}"), |
| 54 |
dup("interface{~int | ~string; float64; m()}"), |
| 55 |
dup("interface{~T[int, string] | string}"), |
| 56 |
|
| 57 |
// generic types |
| 58 |
dup("x[T]"), |
| 59 |
dup("x[N | A | S]"), |
| 60 |
dup("x[N, A]"), |
| 61 |
|
| 62 |
// non-type expressions |
| 63 |
dup("(x)"), |
| 64 |
dup("x.f"), |
| 65 |
dup("a[i]"), |
| 66 |
|
| 67 |
dup("s[:]"), |
| 68 |
dup("s[i:]"), |
| 69 |
dup("s[:j]"), |
| 70 |
dup("s[i:j]"), |
| 71 |
dup("s[:j:k]"), |
| 72 |
dup("s[i:j:k]"), |
| 73 |
|
| 74 |
dup("x.(T)"), |
| 75 |
|
| 76 |
dup("x.([10]int)"), |
| 77 |
dup("x.([...]int)"), |
| 78 |
|
| 79 |
dup("x.(struct{})"), |
| 80 |
dup("x.(struct{x int; y, z float32; E})"), |
| 81 |
|
| 82 |
dup("x.(func())"), |
| 83 |
dup("x.(func(x int))"), |
| 84 |
dup("x.(func() int)"), |
| 85 |
dup("x.(func(x, y int, z float32) (r int))"), |
| 86 |
dup("x.(func(a, b, c int))"), |
| 87 |
dup("x.(func(x ...T))"), |
| 88 |
|
| 89 |
dup("x.(interface{})"), |
| 90 |
dup("x.(interface{m(); n(x int); E})"), |
| 91 |
dup("x.(interface{m(); n(x int) T; E; F})"), |
| 92 |
|
| 93 |
dup("x.(map[K]V)"), |
| 94 |
|
| 95 |
dup("x.(chan E)"), |
| 96 |
dup("x.(<-chan E)"), |
| 97 |
dup("x.(chan<- chan int)"), |
| 98 |
dup("x.(chan<- <-chan int)"), |
| 99 |
dup("x.(<-chan chan int)"), |
| 100 |
dup("x.(chan (<-chan int))"), |
| 101 |
|
| 102 |
dup("f()"), |
| 103 |
dup("f(x)"), |
| 104 |
dup("int(x)"), |
| 105 |
dup("f(x, x + y)"), |
| 106 |
dup("f(s...)"), |
| 107 |
dup("f(a, s...)"), |
| 108 |
|
| 109 |
// generic functions |
| 110 |
dup("f[T]()"), |
| 111 |
dup("f[T](T)"), |
| 112 |
dup("f[T, T1]()"), |
| 113 |
dup("f[T, T1](T, T1)"), |
| 114 |
|
| 115 |
dup("*x"), |
| 116 |
dup("&x"), |
| 117 |
dup("x + y"), |
| 118 |
dup("x + y << (2 * s)"), |
| 119 |
} |
| 120 |
|
| 121 |
func TestExprString(t *testing.T) { |
| 122 |
for _, test := range testExprs { |
| 123 |
x, err := parser.ParseExpr(test.src) |
| 124 |
if err != nil { |
| 125 |
t.Errorf("%s: %s", test.src, err) |
| 126 |
continue |
| 127 |
} |
| 128 |
if got := ExprString(x); got != test.str { |
| 129 |
t.Errorf("%s: got %s, want %s", test.src, got, test.str) |
| 130 |
} |
| 131 |
} |
| 132 |
} |