github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/go/types/exprstring_test.go (about)

     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  
    25  	// func and composite literals
    26  	{"func(){}", "(func() literal)"},
    27  	{"func(x int) complex128 {}", "(func(x int) complex128 literal)"},
    28  	{"[]int{1, 2, 3}", "([]int literal)"},
    29  
    30  	// non-type expressions
    31  	dup("(x)"),
    32  	dup("x.f"),
    33  	dup("a[i]"),
    34  
    35  	dup("s[:]"),
    36  	dup("s[i:]"),
    37  	dup("s[:j]"),
    38  	dup("s[i:j]"),
    39  	dup("s[:j:k]"),
    40  	dup("s[i:j:k]"),
    41  
    42  	dup("x.(T)"),
    43  
    44  	dup("x.([10]int)"),
    45  	dup("x.([...]int)"),
    46  
    47  	dup("x.(struct{})"),
    48  	dup("x.(struct{x int; y, z float32; E})"),
    49  
    50  	dup("x.(func())"),
    51  	dup("x.(func(x int))"),
    52  	dup("x.(func() int)"),
    53  	dup("x.(func(x, y int, z float32) (r int))"),
    54  	dup("x.(func(a, b, c int))"),
    55  	dup("x.(func(x ...T))"),
    56  
    57  	dup("x.(interface{})"),
    58  	dup("x.(interface{m(); n(x int); E})"),
    59  	dup("x.(interface{m(); n(x int) T; E; F})"),
    60  
    61  	dup("x.(map[K]V)"),
    62  
    63  	dup("x.(chan E)"),
    64  	dup("x.(<-chan E)"),
    65  	dup("x.(chan<- chan int)"),
    66  	dup("x.(chan<- <-chan int)"),
    67  	dup("x.(<-chan chan int)"),
    68  	dup("x.(chan (<-chan int))"),
    69  
    70  	dup("f()"),
    71  	dup("f(x)"),
    72  	dup("int(x)"),
    73  	dup("f(x, x + y)"),
    74  	dup("f(s...)"),
    75  	dup("f(a, s...)"),
    76  
    77  	dup("*x"),
    78  	dup("&x"),
    79  	dup("x + y"),
    80  	dup("x + y << (2 * s)"),
    81  }
    82  
    83  func TestExprString(t *testing.T) {
    84  	for _, test := range testExprs {
    85  		x, err := parser.ParseExpr(test.src)
    86  		if err != nil {
    87  			t.Errorf("%s: %s", test.src, err)
    88  			continue
    89  		}
    90  		if got := ExprString(x); got != test.str {
    91  			t.Errorf("%s: got %s, want %s", test.src, got, test.str)
    92  		}
    93  	}
    94  }