136 lines
3.3 KiB
Go
136 lines
3.3 KiB
Go
package parse_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"reflect"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.makaay.nl/mauricem/go-toml/parse"
|
|
)
|
|
|
|
func Test_Invalid(t *testing.T) {
|
|
for _, suite := range getTestSuites("invalid") {
|
|
success := 0
|
|
fail := 0
|
|
for name, testCase := range getTestCasesForSuite(suite) {
|
|
reader, err := os.Open(testCase.toml)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Cannot read file (%s): %s", testCase.toml, err))
|
|
}
|
|
if _, err = parse.Run(reader); err == nil {
|
|
t.Errorf("[%s] expected a parse error, but got none", name)
|
|
fail++
|
|
} else {
|
|
success++
|
|
}
|
|
}
|
|
t.Logf("Suite %s: successful = %d, failed = %d", suite.name, success, fail)
|
|
}
|
|
}
|
|
|
|
func Test_Valid(t *testing.T) {
|
|
for _, suite := range getTestSuites("valid") {
|
|
success := 0
|
|
fail := 0
|
|
for name, testCase := range getTestCasesForSuite(suite) {
|
|
input, err := os.Open(testCase.toml)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Cannot open toml file for test (%v): %s", testCase, err))
|
|
}
|
|
tomlTable, err := parse.Run(input)
|
|
if err != nil {
|
|
t.Errorf("[%s] parse failed unexpectedly: %s", name, err)
|
|
fail++
|
|
continue
|
|
}
|
|
jsonfh, err := os.Open(testCase.json)
|
|
if err != nil {
|
|
t.Errorf("[%s] Could not open JSON file (%s): %s", name, testCase.json, err)
|
|
fail++
|
|
continue
|
|
}
|
|
var expected interface{}
|
|
if err := json.NewDecoder(jsonfh).Decode(&expected); err != nil {
|
|
t.Errorf("[%s] Could not read JSON from (%s): %s", name, testCase.json, err)
|
|
fail++
|
|
continue
|
|
}
|
|
var actual interface{}
|
|
if err := json.Unmarshal([]byte(tomlTable.MakeSushi()), &actual); err != nil {
|
|
t.Errorf("[%s] Could not convert parser output to BurntSushi format: %s", name, err)
|
|
fail++
|
|
continue
|
|
}
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
t.Errorf("[%s] Expected result does not match the actual result", name)
|
|
fail++
|
|
continue
|
|
}
|
|
success++
|
|
}
|
|
t.Logf("Suite %s: successful = %d, failed = %d", suite.name, success, fail)
|
|
}
|
|
}
|
|
|
|
type testSuite struct {
|
|
testType string
|
|
name string
|
|
dir string
|
|
}
|
|
|
|
func getTestSuites(testType string) []testSuite {
|
|
dir := getTestfilesDir() + "/" + testType
|
|
entries, err := ioutil.ReadDir(dir)
|
|
if err != nil {
|
|
log.Fatalf("Cannot read directory (%s): %s", dir, err)
|
|
}
|
|
suites := make([]testSuite, len(entries))
|
|
for i, ent := range entries {
|
|
suites[i] = testSuite{testType, ent.Name(), dir + "/" + ent.Name()}
|
|
}
|
|
return suites
|
|
}
|
|
|
|
func getTestfilesDir() string {
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
return strings.Replace(filename, "_test.go", "", 1)
|
|
}
|
|
|
|
type testCase struct {
|
|
toml string
|
|
json string
|
|
}
|
|
|
|
func getTestCasesForSuite(suite testSuite) map[string]*testCase {
|
|
entries, err := ioutil.ReadDir(suite.dir)
|
|
if err != nil {
|
|
log.Fatalf("Cannot read directory (%s): %s", suite.dir, err)
|
|
}
|
|
testCases := make(map[string]*testCase)
|
|
for _, ent := range entries {
|
|
name := ent.Name()
|
|
id := name[0 : len(name)-5]
|
|
key := suite.testType + "/" + suite.name + "/" + id
|
|
c, ok := testCases[key]
|
|
if !ok {
|
|
c = &testCase{}
|
|
testCases[key] = c
|
|
}
|
|
switch {
|
|
case strings.HasSuffix(name, ".toml"):
|
|
c.toml = suite.dir + "/" + name
|
|
case strings.HasSuffix(name, ".json"):
|
|
c.json = suite.dir + "/" + name
|
|
default:
|
|
panic("Invalid test suite file found: " + suite.dir + "/" + name)
|
|
}
|
|
}
|
|
return testCases
|
|
}
|