Source file src/go/types/exprstring_test.go

     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  	// type expressions
    31  	dup("[1 << 10]byte"),
    32  	dup("[]int"),
    33  	dup("*int"),
    34  	dup("struct{x int}"),
    35  	dup("func()"),
    36  	dup("func(int, float32) string"),
    37  	dup("interface{m()}"),
    38  	dup("interface{m() string; n(x int)}"),
    39  	dup("interface{~int}"),
    40  
    41  	dup("map[string]int"),
    42  	dup("chan E"),
    43  	dup("<-chan E"),
    44  	dup("chan<- E"),
    45  
    46  	// new interfaces
    47  	dup("interface{int}"),
    48  	dup("interface{~int}"),
    49  	dup("interface{~int}"),
    50  	dup("interface{int | string}"),
    51  	dup("interface{~int | ~string; float64; m()}"),
    52  
    53  	// See above.
    54  	// dup("interface{type a, b, c; ~int | ~string; float64; m()}"),
    55  	dup("interface{~T[int, string] | string}"),
    56  
    57  	// non-type expressions
    58  	dup("(x)"),
    59  	dup("x.f"),
    60  	dup("a[i]"),
    61  
    62  	dup("s[:]"),
    63  	dup("s[i:]"),
    64  	dup("s[:j]"),
    65  	dup("s[i:j]"),
    66  	dup("s[:j:k]"),
    67  	dup("s[i:j:k]"),
    68  
    69  	dup("x.(T)"),
    70  
    71  	dup("x.([10]int)"),
    72  	dup("x.([...]int)"),
    73  
    74  	dup("x.(struct{})"),
    75  	dup("x.(struct{x int; y, z float32; E})"),
    76  
    77  	dup("x.(func())"),
    78  	dup("x.(func(x int))"),
    79  	dup("x.(func() int)"),
    80  	dup("x.(func(x, y int, z float32) (r int))"),
    81  	dup("x.(func(a, b, c int))"),
    82  	dup("x.(func(x ...T))"),
    83  
    84  	dup("x.(interface{})"),
    85  	dup("x.(interface{m(); n(x int); E})"),
    86  	dup("x.(interface{m(); n(x int) T; E; F})"),
    87  
    88  	dup("x.(map[K]V)"),
    89  
    90  	dup("x.(chan E)"),
    91  	dup("x.(<-chan E)"),
    92  	dup("x.(chan<- chan int)"),
    93  	dup("x.(chan<- <-chan int)"),
    94  	dup("x.(<-chan chan int)"),
    95  	dup("x.(chan (<-chan int))"),
    96  
    97  	dup("f()"),
    98  	dup("f(x)"),
    99  	dup("int(x)"),
   100  	dup("f(x, x + y)"),
   101  	dup("f(s...)"),
   102  	dup("f(a, s...)"),
   103  
   104  	dup("*x"),
   105  	dup("&x"),
   106  	dup("x + y"),
   107  	dup("x + y << (2 * s)"),
   108  }
   109  
   110  func TestExprString(t *testing.T) {
   111  	for _, test := range testExprs {
   112  		x, err := parser.ParseExpr(test.src)
   113  		if err != nil {
   114  			t.Errorf("%s: %s", test.src, err)
   115  			continue
   116  		}
   117  		if got := ExprString(x); got != test.str {
   118  			t.Errorf("%s: got %s, want %s", test.src, got, test.str)
   119  		}
   120  	}
   121  }
   122  

View as plain text