github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/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  }
    39  
    40  func TestValid(t *testing.T) {
    41  	for _, src := range valids {
    42  		checkErrors(t, src, src)
    43  	}
    44  }
    45  
    46  var invalids = []string{
    47  	`foo /* ERROR "expected 'package'" */ !`,
    48  	`package p; func f() { if { /* ERROR "expected operand" */ } };`,
    49  	`package p; func f() { if ; { /* ERROR "expected operand" */ } };`,
    50  	`package p; func f() { if f(); { /* ERROR "expected operand" */ } };`,
    51  	`package p; func f() { if _ /* ERROR "expected condition" */ = range x; true {} };`,
    52  	`package p; func f() { switch _ /* ERROR "expected condition" */ = range x; true {} };`,
    53  	`package p; func f() { for _ = range x ; /* ERROR "expected '{'" */ ; {} };`,
    54  	`package p; func f() { for ; ; _ = range /* ERROR "expected operand" */ x {} };`,
    55  	`package p; func f() { for ; _ /* ERROR "expected condition" */ = range x ; {} };`,
    56  	`package p; func f() { switch t /* ERROR "expected condition" */ = t.(type) {} };`,
    57  	`package p; func f() { switch t /* ERROR "expected condition" */ , t = t.(type) {} };`,
    58  	`package p; func f() { switch t /* ERROR "expected condition" */ = t.(type), t {} };`,
    59  	`package p; var a = [ /* ERROR "expected expression" */ 1]int;`,
    60  	`package p; var a = [ /* ERROR "expected expression" */ ...]int;`,
    61  	`package p; var a = struct /* ERROR "expected expression" */ {}`,
    62  	`package p; var a = func /* ERROR "expected expression" */ ();`,
    63  	`package p; var a = interface /* ERROR "expected expression" */ {}`,
    64  	`package p; var a = [ /* ERROR "expected expression" */ ]int`,
    65  	`package p; var a = map /* ERROR "expected expression" */ [int]int`,
    66  	`package p; var a = chan /* ERROR "expected expression" */ int;`,
    67  	`package p; var a = []int{[ /* ERROR "expected expression" */ ]int};`,
    68  	`package p; var a = ( /* ERROR "expected expression" */ []int);`,
    69  	`package p; var a = a[[ /* ERROR "expected expression" */ ]int:[]int];`,
    70  	`package p; var a = <- /* ERROR "expected expression" */ chan int;`,
    71  	`package p; func f() { select { case _ <- chan /* ERROR "expected expression" */ int: } };`,
    72  	`package p; func f() { _ = (<-<- /* ERROR "expected 'chan'" */ chan int)(nil) };`,
    73  	`package p; func f() { _ = (<-chan<-chan<-chan<-chan<-chan<- /* ERROR "expected channel type" */ int)(nil) };`,
    74  	`package p; func f() { var t []int; t /* ERROR "expected identifier on left side of :=" */ [0] := 0 };`,
    75  	`package p; func f() { if x := g(); x = /* ERROR "expected '=='" */ 0 {}};`,
    76  	`package p; func f() { _ = x = /* ERROR "expected '=='" */ 0 {}};`,
    77  	`package p; func f() { _ = 1 == func()int { var x bool; x = x = /* ERROR "expected '=='" */ true; return x }() };`,
    78  	`package p; func f() { var s []int; _ = s[] /* ERROR "expected operand" */ };`,
    79  	`package p; func f() { var s []int; _ = s[::: /* ERROR "expected ']'" */ ] };`,
    80  	`package p; func f() { var s []int; _ = s[i:j:k: /* ERROR "expected ']'" */ l] };`,
    81  }
    82  
    83  func TestInvalid(t *testing.T) {
    84  	for _, src := range invalids {
    85  		checkErrors(t, src, src)
    86  	}
    87  }