diff --git a/lex_test.go b/lex_test.go index 0790e7b..3314a04 100644 --- a/lex_test.go +++ b/lex_test.go @@ -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)) } } diff --git a/parse_test.go b/parse_test.go index 3729d45..c755566 100644 --- a/parse_test.go +++ b/parse_test.go @@ -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" - } - `, - root: &rootNode{ - children: []node{ - &assignmentNode{"http", object{ - "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 + } + defer in.Close() -type parseTest struct { - source string - root node -} + expected, err := ioutil.ReadFile(outpath) + if err != nil { + t.Errorf("unable to read expected output for %s: %s", outpath, err) + return + } -func (p *parseTest) run(t *testing.T) { - r := strings.NewReader(p.source) - n, err := parse(r) + r_inpath := filepath.Base(inpath) + n, err := strconv.ParseInt(strings.TrimSuffix(r_inpath, ".in"), 0, 64) if err != nil { - t.Errorf("parse error: %v", err) + t.Errorf("unable to get test number for path %s: %s", inpath, 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) + + var buf bytes.Buffer + root, err := parse(in) + if err != nil { + t.Logf("test %d: in: %s out: %s", n, inpath, outpath) + t.Errorf("parse error in test %d: %s", n, err) + return + } + 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)) } }