let's try file-based tests here too

master
Jordan Orelli 10 years ago
parent 734d72c9a8
commit 45f56933a7

@ -11,7 +11,7 @@ import (
"testing"
)
func runTest(t *testing.T, basepath, inpath, outpath string) {
func runLexTest(t *testing.T, basepath, inpath, outpath string) {
in, err := os.Open(inpath)
if err != nil {
t.Errorf("unable to open input file %s: %s", inpath, err)
@ -54,6 +54,6 @@ func TestLex(t *testing.T) {
}
for _, fname := range files {
runTest(t, "tests/lex/", fname, strings.Replace(fname, "in", "out", -1))
runLexTest(t, "tests/lex/", fname, strings.Replace(fname, "in", "out", -1))
}
}

@ -1,151 +1,65 @@
package main
import (
"reflect"
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
)
var parseTests = []parseTest{
{
source: ``,
root: &rootNode{
children: []node{},
},
},
{
source: `# just a comment`,
root: &rootNode{
children: []node{},
},
},
{
source: `name: "jordan"`,
root: &rootNode{
children: []node{
&assignmentNode{"name", "jordan"},
},
},
},
{
source: `
hostname: "jordanorelli.com"
port: 9000
freq: 1e9
duty: 0.2
neg: -2
neg2: -2.3
imag: 1+2i
`,
root: &rootNode{
children: []node{
&assignmentNode{"hostname", "jordanorelli.com"},
&assignmentNode{"port", 9000},
&assignmentNode{"freq", 1e9},
&assignmentNode{"duty", 0.2},
&assignmentNode{"neg", -2},
&assignmentNode{"neg2", -2.3},
&assignmentNode{"imag", 1 + 2i},
},
},
},
{
source: `
first_name: "jordan" # yep, that's my name
last_name: "orelli" # comments should be able to follow other shit
`,
root: &rootNode{
children: []node{
&assignmentNode{"first_name", "jordan"},
&assignmentNode{"last_name", "orelli"},
},
},
},
{
source: `
heroes: ["lina" "cm"]
`,
root: &rootNode{
children: []node{
&assignmentNode{"heroes", list{"lina", "cm"}},
},
},
},
{
source: `
nested: [["one" "two"] ["three" "four"]]
`,
root: &rootNode{
children: []node{
&assignmentNode{"nested", list{list{"one", "two"}, list{"three", "four"}}},
},
},
},
{
source: `
nested: [
["one" "two"]
["three" "four"]
]
`,
root: &rootNode{
children: []node{
&assignmentNode{"nested", list{list{"one", "two"}, list{"three", "four"}}},
},
},
},
{
source: `
admin: {first_name: "jordan" last_name: "orelli"}
`,
root: &rootNode{
children: []node{
&assignmentNode{"admin", object{
"first_name": "jordan",
"last_name": "orelli",
}},
},
},
},
{
source: `
http: {
port: 9000
routes: "/path/to/some/file"
func runParseTest(t *testing.T, basepath, inpath, outpath string) {
in, err := os.Open(inpath)
if err != nil {
t.Errorf("unable to open input file %s: %s", inpath, err)
return
}
`,
root: &rootNode{
children: []node{
&assignmentNode{"http", object{
"port": 9000,
"routes": "/path/to/some/file",
}},
},
},
},
defer in.Close()
expected, err := ioutil.ReadFile(outpath)
if err != nil {
t.Errorf("unable to read expected output for %s: %s", outpath, err)
return
}
type parseTest struct {
source string
root node
r_inpath := filepath.Base(inpath)
n, err := strconv.ParseInt(strings.TrimSuffix(r_inpath, ".in"), 0, 64)
if err != nil {
t.Errorf("unable to get test number for path %s: %s", inpath, err)
return
}
func (p *parseTest) run(t *testing.T) {
r := strings.NewReader(p.source)
n, err := parse(r)
var buf bytes.Buffer
root, err := parse(in)
if err != nil {
t.Errorf("parse error: %v", err)
t.Logf("test %d: in: %s out: %s", n, inpath, outpath)
t.Errorf("parse error in test %d: %s", n, err)
return
}
if !reflect.DeepEqual(p.root, n) {
t.Errorf("trees are not equal. expected:\n%v\nsaw:\n%v", p.root, n)
} else {
t.Logf("OK trees are equal: %v = %v", p.root, n)
if err := root.pretty(&buf, ""); err != nil {
t.Logf("test %d: in: %s out: %s", n, inpath, outpath)
t.Errorf("output error in test %d: %s", n, err)
return
}
if !bytes.Equal(buf.Bytes(), expected) {
t.Logf("test %d: in: %s out: %s", n, inpath, outpath)
t.Errorf("lex output does not match expected result for test %d", n)
t.Logf("expected output:\n%s", expected)
t.Logf("received output:\n%s", buf.Bytes())
}
}
func TestParse(t *testing.T) {
for _, test := range parseTests {
test.run(t)
files, err := filepath.Glob("tests/parse/*.in")
if err != nil {
t.Errorf("unable to find test files: %s", err)
return
}
for _, fname := range files {
runParseTest(t, "tests/parse/", fname, strings.Replace(fname, "in", "out", -1))
}
}

Loading…
Cancel
Save