github.com/yunabe/lgo@v0.0.0-20190709125917-42c42d410fdf/parser/short_test.go (about)

     1  // Copyright 2009 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  // This file contains test cases for short valid and invalid programs.
     6  
     7  package parser
     8  
     9  import "testing"
    10  
    11  var valids = []string{
    12  	"package p\n",
    13  	`package p;`,
    14  	`package p; import "fmt"; func f() { fmt.Println("Hello, World!") };`,
    15  	`package p; func f() { if f(T{}) {} };`,
    16  	`package p; func f() { _ = <-chan int(nil) };`,
    17  	`package p; func f() { _ = (<-chan int)(nil) };`,
    18  	`package p; func f() { _ = (<-chan <-chan int)(nil) };`,
    19  	`package p; func f() { _ = <-chan <-chan <-chan <-chan <-int(nil) };`,
    20  	`package p; func f(func() func() func());`,
    21  	`package p; func f(...T);`,
    22  	`package p; func f(float, ...int);`,
    23  	`package p; func f(x int, a ...int) { f(0, a...); f(1, a...,) };`,
    24  	`package p; func f(int,) {};`,
    25  	`package p; func f(...int,) {};`,
    26  	`package p; func f(x ...int,) {};`,
    27  	`package p; type T []int; var a []bool; func f() { if a[T{42}[0]] {} };`,
    28  	`package p; type T []int; func g(int) bool { return true }; func f() { if g(T{42}[0]) {} };`,
    29  	`package p; type T []int; func f() { for _ = range []int{T{42}[0]} {} };`,
    30  	`package p; var a = T{{1, 2}, {3, 4}}`,
    31  	`package p; func f() { select { case <- c: case c <- d: case c <- <- d: case <-c <- d: } };`,
    32  	`package p; func f() { select { case x := (<-c): } };`,
    33  	`package p; func f() { if ; true {} };`,
    34  	`package p; func f() { switch ; {} };`,
    35  	`package p; func f() { for _ = range "foo" + "bar" {} };`,
    36  	`package p; func f() { var s []int; g(s[:], s[i:], s[:j], s[i:j], s[i:j:k], s[:j:k]) };`,
    37  	`package p; var ( _ = (struct {*T}).m; _ = (interface {T}).m )`,
    38  	`package p; func ((T),) m() {}`,
    39  	`package p; func ((*T),) m() {}`,
    40  	`package p; func (*(T),) m() {}`,
    41  	`package p; func _(x []int) { for range x {} }`,
    42  	`package p; func _() { if [T{}.n]int{} {} }`,
    43  	`package p; func _() { map[int]int{}[0]++; map[int]int{}[0] += 1 }`,
    44  	`package p; func _(x interface{f()}) { interface{f()}(x).f() }`,
    45  	`package p; func _(x chan int) { chan int(x) <- 0 }`,
    46  	`package p; const (x = 0; y; z)`, // issue 9639
    47  	`package p; var _ = map[P]int{P{}:0, {}:1}`,
    48  	`package p; var _ = map[*P]int{&P{}:0, {}:1}`,
    49  }
    50  
    51  func TestValid(t *testing.T) {
    52  	for _, src := range valids {
    53  		checkErrors(t, src, src)
    54  	}
    55  }
    56  
    57  var invalids = []string{
    58  	`foo /* ERROR "expected 'package'" */ !`,
    59  	`package p; func f() { if { /* ERROR "expected operand" */ } };`,
    60  	`package p; func f() { if ; { /* ERROR "expected operand" */ } };`,
    61  	`package p; func f() { if f(); { /* ERROR "expected operand" */ } };`,
    62  	`package p; func f() { if _ /* ERROR "expected boolean expression" */ = range x; true {} };`,
    63  	`package p; func f() { switch _ /* ERROR "expected switch expression" */ = range x; true {} };`,
    64  	`package p; func f() { for _ = range x ; /* ERROR "expected '{'" */ ; {} };`,
    65  	`package p; func f() { for ; ; _ = range /* ERROR "expected operand" */ x {} };`,
    66  	`package p; func f() { for ; _ /* ERROR "expected boolean or range expression" */ = range x ; {} };`,
    67  	`package p; func f() { switch t = /* ERROR "expected ':=', found '='" */ t.(type) {} };`,
    68  	`package p; func f() { switch t /* ERROR "expected switch expression" */ , t = t.(type) {} };`,
    69  	`package p; func f() { switch t /* ERROR "expected switch expression" */ = t.(type), t {} };`,
    70  	`package p; var a = [ /* ERROR "expected expression" */ 1]int;`,
    71  	`package p; var a = [ /* ERROR "expected expression" */ ...]int;`,
    72  	`package p; var a = struct /* ERROR "expected expression" */ {}`,
    73  	`package p; var a = func /* ERROR "expected expression" */ ();`,
    74  	`package p; var a = interface /* ERROR "expected expression" */ {}`,
    75  	`package p; var a = [ /* ERROR "expected expression" */ ]int`,
    76  	`package p; var a = map /* ERROR "expected expression" */ [int]int`,
    77  	`package p; var a = chan /* ERROR "expected expression" */ int;`,
    78  	`package p; var a = []int{[ /* ERROR "expected expression" */ ]int};`,
    79  	`package p; var a = ( /* ERROR "expected expression" */ []int);`,
    80  	`package p; var a = a[[ /* ERROR "expected expression" */ ]int:[]int];`,
    81  	`package p; var a = <- /* ERROR "expected expression" */ chan int;`,
    82  	`package p; func f() { select { case _ <- chan /* ERROR "expected expression" */ int: } };`,
    83  	`package p; func f() { _ = (<-<- /* ERROR "expected 'chan'" */ chan int)(nil) };`,
    84  	`package p; func f() { _ = (<-chan<-chan<-chan<-chan<-chan<- /* ERROR "expected channel type" */ int)(nil) };`,
    85  	`package p; func f() { var t []int; t /* ERROR "expected identifier on left side of :=" */ [0] := 0 };`,
    86  	`package p; func f() { if x := g(); x = /* ERROR "expected '=='" */ 0 {}};`,
    87  	`package p; func f() { _ = x = /* ERROR "expected '=='" */ 0 {}};`,
    88  	`package p; func f() { _ = 1 == func()int { var x bool; x = x = /* ERROR "expected '=='" */ true; return x }() };`,
    89  	`package p; func f() { var s []int; _ = s[] /* ERROR "expected operand" */ };`,
    90  	`package p; func f() { var s []int; _ = s[i:j: /* ERROR "3rd index required" */ ] };`,
    91  	`package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :k] };`,
    92  	`package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :] };`,
    93  	`package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ :] };`,
    94  	`package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ ::] };`,
    95  	`package p; func f() { var s []int; _ = s[i:j:k: /* ERROR "expected ']'" */ l] };`,
    96  	`package p; func f() { for x /* ERROR "boolean or range expression" */ = []string {} }`,
    97  	`package p; func f() { for x /* ERROR "boolean or range expression" */ := []string {} }`,
    98  	`package p; func f() { for i /* ERROR "boolean or range expression" */ , x = []string {} }`,
    99  	`package p; func f() { for i /* ERROR "boolean or range expression" */ , x := []string {} }`,
   100  	`package p; func f() { go f /* ERROR HERE "function must be invoked" */ }`,
   101  	`package p; func f() { defer func() {} /* ERROR HERE "function must be invoked" */ }`,
   102  	`package p; func f() { go func() { func() { f(x func /* ERROR "missing ','" */ (){}) } } }`,
   103  	`package p; func f(x func(), u v func /* ERROR "missing ','" */ ()){}`,
   104  
   105  	// issue 8656
   106  	`package p; func f() (a b string /* ERROR "missing ','" */ , ok bool)`,
   107  
   108  	// issue 9639
   109  	`package p; var x /* ERROR "missing variable type or initialization" */ , y, z;`,
   110  	`package p; const x /* ERROR "missing constant value" */ ;`,
   111  	`package p; const x /* ERROR "missing constant value" */ int;`,
   112  	`package p; const (x = 0; y; z /* ERROR "missing constant value" */ int);`,
   113  
   114  	// issue 12437
   115  	`package p; var _ = struct { x int, /* ERROR "expected ';', found ','" */ }{};`,
   116  	`package p; var _ = struct { x int, /* ERROR "expected ';', found ','" */ y float }{};`,
   117  
   118  	// issue 11611
   119  	`package p; type _ struct { int, } /* ERROR "expected type, found '}'" */ ;`,
   120  	`package p; type _ struct { int, float } /* ERROR "expected type, found '}'" */ ;`,
   121  	`package p; type _ struct { ( /* ERROR "expected anonymous field" */ int) };`,
   122  	`package p; func _()(x, y, z ... /* ERROR "expected '\)', found '...'" */ int){}`,
   123  	`package p; func _()(... /* ERROR "expected type, found '...'" */ int){}`,
   124  
   125  	// issue 13475
   126  	`package p; func f() { if true {} else ; /* ERROR "expected if statement or block" */ }`,
   127  	`package p; func f() { if true {} else defer /* ERROR "expected if statement or block" */ f() }`,
   128  }
   129  
   130  func TestInvalid(t *testing.T) {
   131  	for _, src := range invalids {
   132  		checkErrors(t, src, src)
   133  	}
   134  }