forked from pointlander/peg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeg_test.go
113 lines (99 loc) · 1.98 KB
/
peg_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"bytes"
"io/ioutil"
"testing"
)
func TestCorrect(t *testing.T) {
buffer := `package p
type T Peg {}
Grammar <- !.
`
p := &Peg{Tree: New(false, false), Buffer: buffer}
p.Init()
err := p.Parse()
if err != nil {
t.Error(err)
}
}
func TestNoSpacePackage(t *testing.T) {
buffer := `packagenospace
type T Peg {}
Grammar <- !.
`
p := &Peg{Tree: New(false, false), Buffer: buffer}
p.Init()
err := p.Parse()
if err == nil {
t.Error("packagenospace was parsed without error")
}
}
func TestNoSpaceType(t *testing.T) {
buffer := `
package p
typenospace Peg {}
Grammar <- !.
`
p := &Peg{Tree: New(false, false), Buffer: buffer}
p.Init()
err := p.Parse()
if err == nil {
t.Error("typenospace was parsed without error")
}
}
func TestSame(t *testing.T) {
buffer, err := ioutil.ReadFile("peg.peg")
if err != nil {
t.Error(err)
}
p := &Peg{Tree: New(true, true), Buffer: string(buffer)}
p.Init()
if err := p.Parse(); err != nil {
t.Error(err)
}
p.Execute()
out := &bytes.Buffer{}
p.Compile("peg.peg.go", out)
bootstrap, err := ioutil.ReadFile("bootstrap.peg.go")
if err != nil {
t.Error(err)
}
if len(out.Bytes()) != len(bootstrap) {
t.Error("code generated from peg.peg is not the same as bootstrap.peg.go")
return
}
for i, v := range out.Bytes() {
if v != bootstrap[i] {
t.Error("code generated from peg.peg is not the same as bootstrap.peg.go")
return
}
}
}
func BenchmarkParse(b *testing.B) {
files := [...]string{
"peg.peg",
"grammars/c/c.peg",
"grammars/calculator/calculator.peg",
"grammars/fexl/fexl.peg",
"grammars/java/java_1_7.peg",
}
pegs := make([]*Peg, len(files))
for i, file := range files {
input, err := ioutil.ReadFile(file)
if err != nil {
b.Error(err)
}
p := &Peg{Tree: New(true, true), Buffer: string(input)}
p.Init()
pegs[i] = p
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, peg := range pegs {
peg.Reset()
if err := peg.Parse(); err != nil {
b.Error(err)
}
}
}
}