github.com/goplus/gop@v1.2.6/x/typesutil/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 typesutil_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/goplus/gop/parser"
    11  	"github.com/goplus/gop/x/typesutil"
    12  )
    13  
    14  type testEntry struct {
    15  	src, str string
    16  }
    17  
    18  // dup returns a testEntry where both src and str are the same.
    19  func dup(s string) testEntry {
    20  	return testEntry{s, s}
    21  }
    22  
    23  var testExprs = []testEntry{
    24  	// basic type literals
    25  	dup("x"),
    26  	dup("true"),
    27  	dup("42"),
    28  	dup("3.1415"),
    29  	dup("2.71828i"),
    30  	dup(`'a'`),
    31  	dup(`"foo"`),
    32  	dup("`bar`"),
    33  
    34  	// func and composite literals
    35  	{"func(){}", "(func() literal)"},
    36  	{"func(x int) complex128 {}", "(func(x int) complex128 literal)"},
    37  	{"[]int{1, 2, 3}", "[]int{…}"},
    38  
    39  	// type expressions
    40  	dup("[1 << 10]byte"),
    41  	dup("[]int"),
    42  	dup("*int"),
    43  	dup("struct{x int}"),
    44  	dup("func()"),
    45  	dup("func(int, float32) string"),
    46  	dup("interface{m()}"),
    47  	dup("interface{m() string; n(x int)}"),
    48  
    49  	dup("map[string]int"),
    50  	dup("chan E"),
    51  	dup("<-chan E"),
    52  	dup("chan<- E"),
    53  
    54  	// non-type expressions
    55  	dup("(x)"),
    56  	dup("x.f"),
    57  	dup("a[i]"),
    58  
    59  	dup("s[:]"),
    60  	dup("s[i:]"),
    61  	dup("s[:j]"),
    62  	dup("s[i:j]"),
    63  	dup("s[:j:k]"),
    64  	dup("s[i:j:k]"),
    65  
    66  	dup("x.(T)"),
    67  
    68  	dup("x.([10]int)"),
    69  	dup("x.([...]int)"),
    70  
    71  	dup("x.(struct{})"),
    72  	dup("x.(struct{x int; y, z float32; E})"),
    73  
    74  	dup("x.(func())"),
    75  	dup("x.(func(x int))"),
    76  	dup("x.(func() int)"),
    77  	dup("x.(func(x, y int, z float32) (r int))"),
    78  	dup("x.(func(a, b, c int))"),
    79  	dup("x.(func(x ...T))"),
    80  
    81  	dup("x.(interface{})"),
    82  	dup("x.(interface{m(); n(x int); E})"),
    83  	dup("x.(interface{m(); n(x int) T; E; F})"),
    84  
    85  	dup("x.(map[K]V)"),
    86  
    87  	dup("x.(chan E)"),
    88  	dup("x.(<-chan E)"),
    89  	dup("x.(chan<- chan int)"),
    90  	dup("x.(chan<- <-chan int)"),
    91  	dup("x.(<-chan chan int)"),
    92  	dup("x.(chan (<-chan int))"),
    93  
    94  	dup("f()"),
    95  	dup("f(x)"),
    96  	dup("int(x)"),
    97  	dup("f(x, x + y)"),
    98  	dup("f(s...)"),
    99  	dup("f(a, s...)"),
   100  
   101  	dup("*x"),
   102  	dup("&x"),
   103  	dup("x + y"),
   104  	dup("x + y << (2 * s)"),
   105  }
   106  
   107  func TestExprString(t *testing.T) {
   108  	for _, test := range testExprs {
   109  		x, err := parser.ParseExpr(test.src)
   110  		if err != nil {
   111  			t.Errorf("%s: %s", test.src, err)
   112  			continue
   113  		}
   114  		if got := typesutil.ExprString(x); got != test.str {
   115  			t.Errorf("%s: got %s, want %s", test.src, got, test.str)
   116  		}
   117  	}
   118  }