...

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

Documentation: cmd/compile/internal/syntax

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

View as plain text