github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/src/go/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  }
    47  
    48  func TestValid(t *testing.T) {
    49  	for _, src := range valids {
    50  		checkErrors(t, src, src)
    51  	}
    52  }
    53  
    54  var invalids = []string{
    55  	`foo /* ERROR "expected 'package'" */ !`,
    56  	`package p; func f() { if { /* ERROR "expected operand" */ } };`,
    57  	`package p; func f() { if ; { /* ERROR "expected operand" */ } };`,
    58  	`package p; func f() { if f(); { /* ERROR "expected operand" */ } };`,
    59  	`package p; func f() { if _ /* ERROR "expected boolean expression" */ = range x; true {} };`,
    60  	`package p; func f() { switch _ /* ERROR "expected switch expression" */ = range x; true {} };`,
    61  	`package p; func f() { for _ = range x ; /* ERROR "expected '{'" */ ; {} };`,
    62  	`package p; func f() { for ; ; _ = range /* ERROR "expected operand" */ x {} };`,
    63  	`package p; func f() { for ; _ /* ERROR "expected boolean or range expression" */ = range x ; {} };`,
    64  	`package p; func f() { switch t /* ERROR "expected switch expression" */ = t.(type) {} };`,
    65  	`package p; func f() { switch t /* ERROR "expected switch expression" */ , t = t.(type) {} };`,
    66  	`package p; func f() { switch t /* ERROR "expected switch expression" */ = t.(type), t {} };`,
    67  	`package p; var a = [ /* ERROR "expected expression" */ 1]int;`,
    68  	`package p; var a = [ /* ERROR "expected expression" */ ...]int;`,
    69  	`package p; var a = struct /* ERROR "expected expression" */ {}`,
    70  	`package p; var a = func /* ERROR "expected expression" */ ();`,
    71  	`package p; var a = interface /* ERROR "expected expression" */ {}`,
    72  	`package p; var a = [ /* ERROR "expected expression" */ ]int`,
    73  	`package p; var a = map /* ERROR "expected expression" */ [int]int`,
    74  	`package p; var a = chan /* ERROR "expected expression" */ int;`,
    75  	`package p; var a = []int{[ /* ERROR "expected expression" */ ]int};`,
    76  	`package p; var a = ( /* ERROR "expected expression" */ []int);`,
    77  	`package p; var a = a[[ /* ERROR "expected expression" */ ]int:[]int];`,
    78  	`package p; var a = <- /* ERROR "expected expression" */ chan int;`,
    79  	`package p; func f() { select { case _ <- chan /* ERROR "expected expression" */ int: } };`,
    80  	`package p; func f() { _ = (<-<- /* ERROR "expected 'chan'" */ chan int)(nil) };`,
    81  	`package p; func f() { _ = (<-chan<-chan<-chan<-chan<-chan<- /* ERROR "expected channel type" */ int)(nil) };`,
    82  	`package p; func f() { var t []int; t /* ERROR "expected identifier on left side of :=" */ [0] := 0 };`,
    83  	`package p; func f() { if x := g(); x = /* ERROR "expected '=='" */ 0 {}};`,
    84  	`package p; func f() { _ = x = /* ERROR "expected '=='" */ 0 {}};`,
    85  	`package p; func f() { _ = 1 == func()int { var x bool; x = x = /* ERROR "expected '=='" */ true; return x }() };`,
    86  	`package p; func f() { var s []int; _ = s[] /* ERROR "expected operand" */ };`,
    87  	`package p; func f() { var s []int; _ = s[i:j: /* ERROR "3rd index required" */ ] };`,
    88  	`package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :k] };`,
    89  	`package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :] };`,
    90  	`package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ :] };`,
    91  	`package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ ::] };`,
    92  	`package p; func f() { var s []int; _ = s[i:j:k: /* ERROR "expected ']'" */ l] };`,
    93  	`package p; func f() { for x /* ERROR "boolean or range expression" */ = []string {} }`,
    94  	`package p; func f() { for x /* ERROR "boolean or range expression" */ := []string {} }`,
    95  	`package p; func f() { for i /* ERROR "boolean or range expression" */ , x = []string {} }`,
    96  	`package p; func f() { for i /* ERROR "boolean or range expression" */ , x := []string {} }`,
    97  	`package p; func f() { go f /* ERROR HERE "function must be invoked" */ }`,
    98  	`package p; func f() { defer func() {} /* ERROR HERE "function must be invoked" */ }`,
    99  	`package p; func f() { go func() { func() { f(x func /* ERROR "expected '\)'" */ (){}) } } }`,
   100  	`package p; func f() (a b string /* ERROR "expected '\)'" */ , ok bool) // issue 8656`,
   101  }
   102  
   103  func TestInvalid(t *testing.T) {
   104  	for _, src := range invalids {
   105  		checkErrors(t, src, src)
   106  	}
   107  }