2017-10-27 17:25:20 +02:00
|
|
|
package treerack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testItem struct {
|
|
|
|
title string
|
|
|
|
text string
|
|
|
|
fail bool
|
|
|
|
node *Node
|
|
|
|
nodes []*Node
|
|
|
|
ignorePosition bool
|
|
|
|
}
|
|
|
|
|
2017-11-04 22:08:15 +01:00
|
|
|
func runTestsGetSyntax(t *testing.T, getSyntax func(t *testing.T) *Syntax, tests []testItem) {
|
|
|
|
var s *Syntax
|
2017-10-27 17:25:20 +02:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.title, func(t *testing.T) {
|
2017-11-04 22:08:15 +01:00
|
|
|
if s == nil {
|
|
|
|
s = getSyntax(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Failed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-27 17:25:20 +02:00
|
|
|
b := bytes.NewBufferString(test.text)
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
n, err := s.Parse(b)
|
|
|
|
t.Log("parse duration:", time.Now().Sub(start))
|
|
|
|
|
|
|
|
if test.fail && err == nil {
|
|
|
|
t.Error("failed to fail")
|
|
|
|
return
|
|
|
|
} else if !test.fail && err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
} else if test.fail {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.node != nil {
|
|
|
|
checkNode(t, test.ignorePosition, n, test.node)
|
|
|
|
} else {
|
|
|
|
checkNodes(t, test.ignorePosition, n.Nodes, test.nodes)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 22:08:15 +01:00
|
|
|
func runTestsSyntax(t *testing.T, s *Syntax, tests []testItem) {
|
|
|
|
runTestsGetSyntax(t, func(*testing.T) *Syntax { return s }, tests)
|
|
|
|
}
|
|
|
|
|
2017-10-27 17:25:20 +02:00
|
|
|
func runTests(t *testing.T, syntax string, tests []testItem) {
|
2017-11-04 22:08:15 +01:00
|
|
|
getSyntax := func(t *testing.T) *Syntax {
|
|
|
|
s, err := openSyntaxString(syntax)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
2017-10-27 17:25:20 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 22:08:15 +01:00
|
|
|
runTestsGetSyntax(t, getSyntax, tests)
|
2017-10-27 17:25:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func runTestsFile(t *testing.T, file string, tests []testItem) {
|
2017-11-04 22:08:15 +01:00
|
|
|
getSyntax := func(t *testing.T) *Syntax {
|
|
|
|
s, err := openSyntaxFile(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
2017-10-27 17:25:20 +02:00
|
|
|
}
|
|
|
|
|
2017-11-04 22:08:15 +01:00
|
|
|
runTestsGetSyntax(t, getSyntax, tests)
|
2017-10-27 17:25:20 +02:00
|
|
|
}
|