Make use of the new feature to run without sanity check on the parser, to make the big testfiles-based test suite faster.

This commit is contained in:
Maurice Makaay 2019-06-30 01:07:13 +00:00
parent 9680c2b844
commit a28269800a
4 changed files with 22 additions and 9 deletions

View File

@ -254,8 +254,8 @@ func testAST(t *testing.T, code func() (error, *ast.Document), expectedError str
func Test_NewArray(t *testing.T) {
a := ast.NewArray()
if a.Type != "" {
t.Fatalf("New array unexpectedly has a Type set to %q", a.Type)
if a.ItemType != "" {
t.Fatalf("New array unexpectedly has a Type set to %q", a.ItemType)
}
if a.First != nil {
t.Fatalf("New array unexpectedly has the First item set")

View File

@ -2,6 +2,7 @@ package parse
import (
"git.makaay.nl/mauricem/go-parsekit/parse"
"git.makaay.nl/mauricem/go-toml/ast"
)
// The primary building block of a TOML document is the table.
@ -35,7 +36,7 @@ var (
)
func (t *parser) startKeyValuePair(p *parse.API) {
key, ok := t.parseKey(p, []string{})
key, ok := t.parseKey(p, ast.NewKey())
if ok && p.Handle(t.startAssignment) {
if value, ok := t.parseValue(p); ok {
err := t.doc.SetKeyValuePair(key, value)
@ -57,7 +58,7 @@ func (t *parser) startKeyValuePair(p *parse.API) {
// is to use bare keys except when absolutely necessary.
// A bare key must be non-empty, but an empty quoted key is allowed (though
// discouraged).
func (t *parser) parseKey(p *parse.API, key []string) ([]string, bool) {
func (t *parser) parseKey(p *parse.API, key ast.Key) (ast.Key, bool) {
var keyPart string
var ok bool
switch {
@ -82,7 +83,7 @@ func (t *parser) parseKey(p *parse.API, key []string) ([]string, bool) {
// This allows for grouping similar properties together.
// Whitespace around dot-separated parts is ignored, however, best
// practice is to not use any extraneous whitespace.
func (t *parser) parseEndOfKeyOrDot(p *parse.API, key []string) ([]string, bool) {
func (t *parser) parseEndOfKeyOrDot(p *parse.API, key ast.Key) (ast.Key, bool) {
if p.Accept(keySeparatorDot) {
return t.parseKey(p, key)
}

View File

@ -58,3 +58,11 @@ func Run(input interface{}) (ast.Table, error) {
err := parse.New(p.startDocument)(input)
return p.doc.Root, err
}
// RunWithoutSanityChecks runs the TOML parser against the provided input data.
// The parsekit sanity checks are disabled during the parse run.
func RunWithoutSanityChecks(input interface{}) (ast.Table, error) {
p := newParser()
err := parse.NewWithoutSanityChecks(p.startDocument)(input)
return p.doc.Root, err
}

View File

@ -14,6 +14,7 @@ import (
"time"
"git.makaay.nl/mauricem/go-toml/parse"
"github.com/pkg/profile"
)
func Test_Invalid(t *testing.T) {
@ -37,6 +38,7 @@ func Test_Invalid(t *testing.T) {
}
func Test_Valid(t *testing.T) {
defer profile.Start().Stop()
for _, suite := range getTestSuites("valid") {
success := 0
fail := 0
@ -45,7 +47,7 @@ func Test_Valid(t *testing.T) {
if err != nil {
panic(fmt.Sprintf("Cannot open toml file for test (%v): %s", testCase, err))
}
tomlTable, err := parse.Run(input)
tomlTable, err := parse.RunWithoutSanityChecks(input)
if err != nil {
t.Errorf("[%s] parse failed unexpectedly: %s", name, err)
fail++
@ -160,13 +162,15 @@ func cmpJsonObjects(key string, expected map[string]interface{}, a interface{})
actual, _ := a.(map[string]interface{})
// Check to make sure both or neither are values.
if isValue(expected) && !isValue(actual) {
eIsVal := isValue(expected)
aIsVal := isValue(actual)
if eIsVal && !aIsVal {
return fmt.Errorf("Key '%s' is supposed to be a value, but the parser reports it as a table.", key)
}
if !isValue(expected) && isValue(actual) {
if !eIsVal && aIsVal {
return fmt.Errorf("Key '%s' is supposed to be a table, but the parser reports it as a value.", key)
}
if isValue(expected) && isValue(actual) {
if eIsVal && aIsVal {
return cmpJsonValues(key, expected, actual)
}