...
1
2
3
4
5 package ebnf
6
7 import (
8 "bytes"
9 "testing"
10 )
11
12 var goodGrammars = []string{
13 `Program = .`,
14
15 `Program = foo .
16 foo = "foo" .`,
17
18 `Program = "a" | "b" "c" .`,
19
20 `Program = "a" … "z" .`,
21
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" .`,
33
34 "Program = `\"` .",
35 }
36
37 var badGrammars = []string{
38 `Program = | .`,
39 `Program = | b .`,
40 `Program = a … b .`,
41 `Program = "a" … .`,
42 `Program = … "b" .`,
43 `Program = () .`,
44 `Program = [] .`,
45 `Program = {} .`,
46 }
47
48 func checkGood(t *testing.T, src string) {
49 grammar, err := Parse("", bytes.NewBuffer([]byte(src)))
50 if err != nil {
51 t.Errorf("Parse(%s) failed: %v", src, err)
52 return
53 }
54 if err = Verify(grammar, "Program"); err != nil {
55 t.Errorf("Verify(%s) failed: %v", src, err)
56 }
57 }
58
59 func checkBad(t *testing.T, src string) {
60 _, err := Parse("", bytes.NewBuffer([]byte(src)))
61 if err == nil {
62 t.Errorf("Parse(%s) should have failed", src)
63 }
64 }
65
66 func TestGrammars(t *testing.T) {
67 for _, src := range goodGrammars {
68 checkGood(t, src)
69 }
70 for _, src := range badGrammars {
71 checkBad(t, src)
72 }
73 }
74
View as plain text