-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtestsuite_test.go
156 lines (134 loc) · 3.22 KB
/
testsuite_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package denada
import "os"
import "log"
import "fmt"
import "path"
import "testing"
import "strings"
import . "github.com/smartystreets/goconvey/convey"
func ReparseFile(name string) error {
filename := path.Join("testsuite", name)
elems, err := ParseFile(filename)
if err != nil {
return err
}
str := Unparse(elems, false)
relems, err := ParseString(str)
if err != nil {
return fmt.Errorf("Error in unparsed code: %v", err)
}
err = elems.Equals(relems)
if err != nil {
return fmt.Errorf("Inequality in reparsing of %s: %v", filename, err)
}
return nil
}
func CheckFile(name string) error {
filename := path.Join("testsuite", name)
elems, err := ParseFile(filename)
if err != nil {
return err
}
if len(elems) == 0 {
return fmt.Errorf("Empty file")
}
props := elems[0]
declsv, exists := props.Modifications["declarations"]
var edecls int = 0
if exists {
edecls = declsv.MustInt(0)
}
defsv, exists := props.Modifications["definitions"]
var edefs int = 0
if exists {
edefs = defsv.MustInt(0)
}
var adecls int = 0
var adefs int = 0
for _, e := range elems {
if e.IsDeclaration() {
adecls++
}
if e.IsDefinition() {
adefs++
}
}
if adecls != edecls {
return fmt.Errorf("Expected %d declarations, found %d", edecls, adecls)
}
if adefs != edefs {
return fmt.Errorf("Expected %d definitions, found %d", edefs, adefs)
}
grmv, exists := props.Modifications["grammar"]
if exists {
gfile := grmv.MustString()
g, err := ParseFile(path.Join("testsuite", gfile))
if err != nil {
return err
}
err = Check(elems, g, false)
// Check if descriptions on input elements matche expected rule names
if err == nil {
for _, e := range elems.AllElements() {
if e.Description == "" {
err = fmt.Errorf("Input element %v in %s didn't have a description",
e, name)
return err
} else if e.RulePath() == "" {
err = fmt.Errorf("Input element %v in %s didn't seem to match anything",
e, name)
return err
} else {
if e.RulePath() != e.Description {
err = fmt.Errorf("Input element %v matched rule %s but description implies a match with %s", e, e.RulePath(), e.Description)
return err
}
}
}
}
return err
}
return nil
}
func CheckError(name string) {
err := CheckFile(name)
So(err, ShouldBeNil)
}
func Test_TestSuite(t *testing.T) {
Convey("Running TestSuite", t, func() {
cur, err := os.Open("testsuite")
So(err, ShouldBeNil)
files, err := cur.Readdir(0)
So(err, ShouldBeNil)
for _, f := range files {
name := f.Name()
if !strings.HasSuffix(name, ".dnd") {
continue
}
Convey("Processing "+name, func() {
if strings.HasPrefix(name, "case") {
err := CheckFile(name)
if err != nil {
log.Printf("Case %s: Failed: %v", name, err)
}
So(err, ShouldBeNil)
err = ReparseFile(name)
if err != nil {
log.Printf("Case %s: Reparse failed: %v", name, err)
}
So(err, ShouldBeNil)
return
}
if strings.HasPrefix(name, "ecase") {
err := CheckFile(name)
if err == nil {
log.Printf("Error Case %s: FAILED", name)
}
So(err, ShouldNotBeNil)
return
}
log.Printf("Unrecognized file type in test suite: %s", name)
})
}
})
}