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