github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/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 `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 `package p; type T = int`, 50 `package p; type (T = p.T; _ = struct{}; x = *T)`, 51 } 52 53 func TestValid(t *testing.T) { 54 for _, src := range valids { 55 checkErrors(t, src, src) 56 } 57 } 58 59 var invalids = []string{ 60 `foo /* ERROR "expected 'package'" */ !`, 61 `package p; func f() { if { /* ERROR "expected operand" */ } };`, 62 `package p; func f() { if ; { /* ERROR "expected operand" */ } };`, 63 `package p; func f() { if f(); { /* ERROR "expected operand" */ } };`, 64 `package p; func f() { if _ /* ERROR "expected boolean expression" */ = range x; true {} };`, 65 `package p; func f() { switch _ /* ERROR "expected switch expression" */ = range x; true {} };`, 66 `package p; func f() { for _ = range x ; /* ERROR "expected '{'" */ ; {} };`, 67 `package p; func f() { for ; ; _ = range /* ERROR "expected operand" */ x {} };`, 68 `package p; func f() { for ; _ /* ERROR "expected boolean or range expression" */ = range x ; {} };`, 69 `package p; func f() { switch t = /* ERROR "expected ':=', found '='" */ t.(type) {} };`, 70 `package p; func f() { switch t /* ERROR "expected switch expression" */ , t = t.(type) {} };`, 71 `package p; func f() { switch t /* ERROR "expected switch expression" */ = t.(type), t {} };`, 72 `package p; var a = [ /* ERROR "expected expression" */ 1]int;`, 73 `package p; var a = [ /* ERROR "expected expression" */ ...]int;`, 74 `package p; var a = struct /* ERROR "expected expression" */ {}`, 75 `package p; var a = func /* ERROR "expected expression" */ ();`, 76 `package p; var a = interface /* ERROR "expected expression" */ {}`, 77 `package p; var a = [ /* ERROR "expected expression" */ ]int`, 78 `package p; var a = map /* ERROR "expected expression" */ [int]int`, 79 `package p; var a = chan /* ERROR "expected expression" */ int;`, 80 `package p; var a = []int{[ /* ERROR "expected expression" */ ]int};`, 81 `package p; var a = ( /* ERROR "expected expression" */ []int);`, 82 `package p; var a = a[[ /* ERROR "expected expression" */ ]int:[]int];`, 83 `package p; var a = <- /* ERROR "expected expression" */ chan int;`, 84 `package p; func f() { select { case _ <- chan /* ERROR "expected expression" */ int: } };`, 85 `package p; func f() { _ = (<-<- /* ERROR "expected 'chan'" */ chan int)(nil) };`, 86 `package p; func f() { _ = (<-chan<-chan<-chan<-chan<-chan<- /* ERROR "expected channel type" */ int)(nil) };`, 87 `package p; func f() { var t []int; t /* ERROR "expected identifier on left side of :=" */ [0] := 0 };`, 88 `package p; func f() { if x := g(); x = /* ERROR "expected '=='" */ 0 {}};`, 89 `package p; func f() { _ = x = /* ERROR "expected '=='" */ 0 {}};`, 90 `package p; func f() { _ = 1 == func()int { var x bool; x = x = /* ERROR "expected '=='" */ true; return x }() };`, 91 `package p; func f() { var s []int; _ = s[] /* ERROR "expected operand" */ };`, 92 `package p; func f() { var s []int; _ = s[i:j: /* ERROR "3rd index required" */ ] };`, 93 `package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :k] };`, 94 `package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :] };`, 95 `package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ :] };`, 96 `package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ ::] };`, 97 `package p; func f() { var s []int; _ = s[i:j:k: /* ERROR "expected ']'" */ l] };`, 98 `package p; func f() { for x /* ERROR "boolean or range expression" */ = []string {} }`, 99 `package p; func f() { for x /* ERROR "boolean or range expression" */ := []string {} }`, 100 `package p; func f() { for i /* ERROR "boolean or range expression" */ , x = []string {} }`, 101 `package p; func f() { for i /* ERROR "boolean or range expression" */ , x := []string {} }`, 102 `package p; func f() { go f /* ERROR HERE "function must be invoked" */ }`, 103 `package p; func f() { defer func() {} /* ERROR HERE "function must be invoked" */ }`, 104 `package p; func f() { go func() { func() { f(x func /* ERROR "missing ','" */ (){}) } } }`, 105 `package p; func f(x func(), u v func /* ERROR "missing ','" */ ()){}`, 106 107 // issue 8656 108 `package p; func f() (a b string /* ERROR "missing ','" */ , ok bool)`, 109 110 // issue 9639 111 `package p; var x /* ERROR "missing variable type or initialization" */ , y, z;`, 112 `package p; const x /* ERROR "missing constant value" */ ;`, 113 `package p; const x /* ERROR "missing constant value" */ int;`, 114 `package p; const (x = 0; y; z /* ERROR "missing constant value" */ int);`, 115 116 // issue 12437 117 `package p; var _ = struct { x int, /* ERROR "expected ';', found ','" */ }{};`, 118 `package p; var _ = struct { x int, /* ERROR "expected ';', found ','" */ y float }{};`, 119 120 // issue 11611 121 `package p; type _ struct { int, } /* ERROR "expected type, found '}'" */ ;`, 122 `package p; type _ struct { int, float } /* ERROR "expected type, found '}'" */ ;`, 123 `package p; type _ struct { ( /* ERROR "expected anonymous field" */ int) };`, 124 `package p; func _()(x, y, z ... /* ERROR "expected '\)', found '...'" */ int){}`, 125 `package p; func _()(... /* ERROR "expected type, found '...'" */ int){}`, 126 127 // issue 13475 128 `package p; func f() { if true {} else ; /* ERROR "expected if statement or block" */ }`, 129 `package p; func f() { if true {} else defer /* ERROR "expected if statement or block" */ f() }`, 130 } 131 132 func TestInvalid(t *testing.T) { 133 for _, src := range invalids { 134 checkErrors(t, src, src) 135 } 136 }