Source file src/cmd/compile/internal/syntax/printer_test.go

     1  // Copyright 2016 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 syntax
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"io/ioutil"
    11  	"os"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func TestPrint(t *testing.T) {
    17  	if testing.Short() {
    18  		t.Skip("skipping test in short mode")
    19  	}
    20  
    21  	ast, _ := ParseFile(*src_, func(err error) { t.Error(err) }, nil, AllowGenerics)
    22  
    23  	if ast != nil {
    24  		Fprint(testOut(), ast, LineForm)
    25  		fmt.Println()
    26  	}
    27  }
    28  
    29  type shortBuffer struct {
    30  	buf []byte
    31  }
    32  
    33  func (w *shortBuffer) Write(data []byte) (n int, err error) {
    34  	w.buf = append(w.buf, data...)
    35  	n = len(data)
    36  	if len(w.buf) > 10 {
    37  		err = io.ErrShortBuffer
    38  	}
    39  	return
    40  }
    41  
    42  func TestPrintError(t *testing.T) {
    43  	const src = "package p; var x int"
    44  	ast, err := Parse(nil, strings.NewReader(src), nil, nil, 0)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	var buf shortBuffer
    50  	_, err = Fprint(&buf, ast, 0)
    51  	if err == nil || err != io.ErrShortBuffer {
    52  		t.Errorf("got err = %s, want %s", err, io.ErrShortBuffer)
    53  	}
    54  }
    55  
    56  var stringTests = [][2]string{
    57  	dup("package p"),
    58  	dup("package p; type _ int; type T1 = struct{}; type ( _ *struct{}; T2 = float32 )"),
    59  
    60  	// generic type declarations
    61  	dup("package p; type _[T any] struct{}"),
    62  	dup("package p; type _[A, B, C interface{m()}] struct{}"),
    63  	dup("package p; type _[T any, A, B, C interface{m()}, X, Y, Z interface{~int}] struct{}"),
    64  
    65  	dup("package p; type _[P *T,] struct{}"),
    66  	dup("package p; type _[P *T, _ any] struct{}"),
    67  	{"package p; type _[P (*T),] struct{}", "package p; type _[P *T,] struct{}"},
    68  	{"package p; type _[P (*T), _ any] struct{}", "package p; type _[P *T, _ any] struct{}"},
    69  	{"package p; type _[P (T),] struct{}", "package p; type _[P T] struct{}"},
    70  	{"package p; type _[P (T), _ any] struct{}", "package p; type _[P T, _ any] struct{}"},
    71  
    72  	dup("package p; type _[P *struct{}] struct{}"),
    73  	{"package p; type _[P (*struct{})] struct{}", "package p; type _[P *struct{}] struct{}"},
    74  	{"package p; type _[P ([]int)] struct{}", "package p; type _[P []int] struct{}"},
    75  
    76  	dup("package p; type _ [P(T)]struct{}"),
    77  	dup("package p; type _ [P((T))]struct{}"),
    78  	dup("package p; type _ [P * *T]struct{}"),
    79  	dup("package p; type _ [P * T]struct{}"),
    80  	dup("package p; type _ [P(*T)]struct{}"),
    81  	dup("package p; type _ [P(**T)]struct{}"),
    82  	dup("package p; type _ [P * T - T]struct{}"),
    83  
    84  	// array type declarations
    85  	dup("package p; type _ [P * T]struct{}"),
    86  	dup("package p; type _ [P * T - T]struct{}"),
    87  
    88  	// generic function declarations
    89  	dup("package p; func _[T any]()"),
    90  	dup("package p; func _[A, B, C interface{m()}]()"),
    91  	dup("package p; func _[T any, A, B, C interface{m()}, X, Y, Z interface{~int}]()"),
    92  
    93  	// methods with generic receiver types
    94  	dup("package p; func (R[T]) _()"),
    95  	dup("package p; func (*R[A, B, C]) _()"),
    96  	dup("package p; func (_ *R[A, B, C]) _()"),
    97  
    98  	// type constraint literals with elided interfaces
    99  	dup("package p; func _[P ~int, Q int | string]() {}"),
   100  	dup("package p; func _[P struct{f int}, Q *P]() {}"),
   101  
   102  	// channels
   103  	dup("package p; type _ chan chan int"),
   104  	dup("package p; type _ chan (<-chan int)"),
   105  	dup("package p; type _ chan chan<- int"),
   106  
   107  	dup("package p; type _ <-chan chan int"),
   108  	dup("package p; type _ <-chan <-chan int"),
   109  	dup("package p; type _ <-chan chan<- int"),
   110  
   111  	dup("package p; type _ chan<- chan int"),
   112  	dup("package p; type _ chan<- <-chan int"),
   113  	dup("package p; type _ chan<- chan<- int"),
   114  
   115  	// TODO(gri) expand
   116  }
   117  
   118  func TestPrintString(t *testing.T) {
   119  	for _, test := range stringTests {
   120  		ast, err := Parse(nil, strings.NewReader(test[0]), nil, nil, AllowGenerics)
   121  		if err != nil {
   122  			t.Error(err)
   123  			continue
   124  		}
   125  		if got := String(ast); got != test[1] {
   126  			t.Errorf("%q: got %q", test[1], got)
   127  		}
   128  	}
   129  }
   130  
   131  func testOut() io.Writer {
   132  	if testing.Verbose() {
   133  		return os.Stdout
   134  	}
   135  	return ioutil.Discard
   136  }
   137  
   138  func dup(s string) [2]string { return [2]string{s, s} }
   139  
   140  var exprTests = [][2]string{
   141  	// basic type literals
   142  	dup("x"),
   143  	dup("true"),
   144  	dup("42"),
   145  	dup("3.1415"),
   146  	dup("2.71828i"),
   147  	dup(`'a'`),
   148  	dup(`"foo"`),
   149  	dup("`bar`"),
   150  
   151  	// func and composite literals
   152  	dup("func() {}"),
   153  	dup("[]int{}"),
   154  	{"func(x int) complex128 { return 0 }", "func(x int) complex128 {…}"},
   155  	{"[]int{1, 2, 3}", "[]int{…}"},
   156  
   157  	// type expressions
   158  	dup("[1 << 10]byte"),
   159  	dup("[]int"),
   160  	dup("*int"),
   161  	dup("struct{x int}"),
   162  	dup("func()"),
   163  	dup("func(int, float32) string"),
   164  	dup("interface{m()}"),
   165  	dup("interface{m() string; n(x int)}"),
   166  	dup("interface{~int}"),
   167  	dup("interface{~int | ~float64 | ~string}"),
   168  	dup("interface{~int; m()}"),
   169  	dup("interface{~int | ~float64 | ~string; m() string; n(x int)}"),
   170  	dup("map[string]int"),
   171  	dup("chan E"),
   172  	dup("<-chan E"),
   173  	dup("chan<- E"),
   174  
   175  	// new interfaces
   176  	dup("interface{int}"),
   177  	dup("interface{~int}"),
   178  	dup("interface{~int}"),
   179  	dup("interface{int | string}"),
   180  	dup("interface{~int | ~string; float64; m()}"),
   181  	dup("interface{~a | ~b | ~c; ~int | ~string; float64; m()}"),
   182  	dup("interface{~T[int, string] | string}"),
   183  
   184  	// non-type expressions
   185  	dup("(x)"),
   186  	dup("x.f"),
   187  	dup("a[i]"),
   188  
   189  	dup("s[:]"),
   190  	dup("s[i:]"),
   191  	dup("s[:j]"),
   192  	dup("s[i:j]"),
   193  	dup("s[:j:k]"),
   194  	dup("s[i:j:k]"),
   195  
   196  	dup("x.(T)"),
   197  
   198  	dup("x.([10]int)"),
   199  	dup("x.([...]int)"),
   200  
   201  	dup("x.(struct{})"),
   202  	dup("x.(struct{x int; y, z float32; E})"),
   203  
   204  	dup("x.(func())"),
   205  	dup("x.(func(x int))"),
   206  	dup("x.(func() int)"),
   207  	dup("x.(func(x, y int, z float32) (r int))"),
   208  	dup("x.(func(a, b, c int))"),
   209  	dup("x.(func(x ...T))"),
   210  
   211  	dup("x.(interface{})"),
   212  	dup("x.(interface{m(); n(x int); E})"),
   213  	dup("x.(interface{m(); n(x int) T; E; F})"),
   214  
   215  	dup("x.(map[K]V)"),
   216  
   217  	dup("x.(chan E)"),
   218  	dup("x.(<-chan E)"),
   219  	dup("x.(chan<- chan int)"),
   220  	dup("x.(chan<- <-chan int)"),
   221  	dup("x.(<-chan chan int)"),
   222  	dup("x.(chan (<-chan int))"),
   223  
   224  	dup("f()"),
   225  	dup("f(x)"),
   226  	dup("int(x)"),
   227  	dup("f(x, x + y)"),
   228  	dup("f(s...)"),
   229  	dup("f(a, s...)"),
   230  
   231  	dup("*x"),
   232  	dup("&x"),
   233  	dup("x + y"),
   234  	dup("x + y << (2 * s)"),
   235  }
   236  
   237  func TestShortString(t *testing.T) {
   238  	for _, test := range exprTests {
   239  		src := "package p; var _ = " + test[0]
   240  		ast, err := Parse(nil, strings.NewReader(src), nil, nil, AllowGenerics)
   241  		if err != nil {
   242  			t.Errorf("%s: %s", test[0], err)
   243  			continue
   244  		}
   245  		x := ast.DeclList[0].(*VarDecl).Values
   246  		if got := String(x); got != test[1] {
   247  			t.Errorf("%s: got %s, want %s", test[0], got, test[1])
   248  		}
   249  	}
   250  }
   251  

View as plain text