Backport revision 203977 from trunk.
[official-gcc.git] / main / libgo / go / exp / ebnf / ebnf_test.go
blob8cfd6b9c37037ca1753df4580ac71581f540766a
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.
5 package ebnf
7 import (
8 "bytes"
9 "testing"
12 var goodGrammars = []string{
13 `Program = .`,
15 `Program = foo .
16 foo = "foo" .`,
18 `Program = "a" | "b" "c" .`,
20 `Program = "a" … "z" .`,
22 `Program = Song .
23 Song = { Note } .
24 Note = Do | (Re | Mi | Fa | So | La) | Ti .
25 Do = "c" .
26 Re = "d" .
27 Mi = "e" .
28 Fa = "f" .
29 So = "g" .
30 La = "a" .
31 Ti = ti .
32 ti = "b" .`,
35 var badGrammars = []string{
36 `Program = | .`,
37 `Program = | b .`,
38 `Program = a … b .`,
39 `Program = "a" … .`,
40 `Program = … "b" .`,
41 `Program = () .`,
42 `Program = [] .`,
43 `Program = {} .`,
46 func checkGood(t *testing.T, src string) {
47 grammar, err := Parse("", bytes.NewBuffer([]byte(src)))
48 if err != nil {
49 t.Errorf("Parse(%s) failed: %v", src, err)
50 return
52 if err = Verify(grammar, "Program"); err != nil {
53 t.Errorf("Verify(%s) failed: %v", src, err)
57 func checkBad(t *testing.T, src string) {
58 _, err := Parse("", bytes.NewBuffer([]byte(src)))
59 if err == nil {
60 t.Errorf("Parse(%s) should have failed", src)
64 func TestGrammars(t *testing.T) {
65 for _, src := range goodGrammars {
66 checkGood(t, src)
68 for _, src := range badGrammars {
69 checkBad(t, src)