aboutsummaryrefslogtreecommitdiff
path: root/src/urubu/tester.go
blob: cae52b2328b2ddf2de48887c91884a8a72b527ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package tester

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"runtime/debug"
	"strings"

	driver "urubu/driver/parser"
	gspec  "urubu/spec/grammar"
	tspec  "urubu/spec/test"
)

type TestResult struct {
	TestCasePath string
	Error        error
	Diffs        []*tspec.TreeDiff
}

func (r *TestResult) String() string {
	if r.Error != nil {
		const indent1 = "    "
		const indent2 = indent1 + indent1

		msgLines := strings.Split(r.Error.Error(), "\n")
		msg := fmt.Sprintf("Failed %v:\n%v%v", r.TestCasePath, indent1, strings.Join(msgLines, "\n"+indent1))
		if len(r.Diffs) == 0 {
			return msg
		}
		var diffLines []string
		for _, diff := range r.Diffs {
			diffLines = append(diffLines, diff.Message)
			diffLines = append(diffLines, fmt.Sprintf("%vexpected path: %v", indent1, diff.ExpectedPath))
			diffLines = append(diffLines, fmt.Sprintf("%vactual path:   %v", indent1, diff.ActualPath))
		}
		return fmt.Sprintf("%v\n%v%v", msg, indent2, strings.Join(diffLines, "\n"+indent2))
	}
	return fmt.Sprintf("Passed %v", r.TestCasePath)
}

type TestCaseWithMetadata struct {
	TestCase *tspec.TestCase
	FilePath string
	Error    error
}

func ListTestCases(testPath string) []*TestCaseWithMetadata {
	fi, err := os.Stat(testPath)
	if err != nil {
		return []*TestCaseWithMetadata{
			{
				FilePath: testPath,
				Error:    err,
			},
		}
	}
	if !fi.IsDir() {
		c, err := parseTestCase(testPath)
		return []*TestCaseWithMetadata{
			{
				TestCase: c,
				FilePath: testPath,
				Error:    err,
			},
		}
	}

	es, err := os.ReadDir(testPath)
	if err != nil {
		return []*TestCaseWithMetadata{
			{
				FilePath: testPath,
				Error:    err,
			},
		}
	}
	var cases []*TestCaseWithMetadata
	for _, e := range es {
		cs := ListTestCases(filepath.Join(testPath, e.Name()))
		cases = append(cases, cs...)
	}
	return cases
}

func parseTestCase(testCasePath string) (*tspec.TestCase, error) {
	f, err := os.Open(testCasePath)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	return tspec.ParseTestCase(f)
}

type Tester struct {
	Grammar *gspec.CompiledGrammar
	Cases   []*TestCaseWithMetadata
}

func (t *Tester) Run() []*TestResult {
	var rs []*TestResult
	for _, c := range t.Cases {
		rs = append(rs, runTest(t.Grammar, c))
	}
	return rs
}

func runTest(g *gspec.CompiledGrammar, c *TestCaseWithMetadata) *TestResult {
	var p *driver.Parser
	var tb *driver.DefaultSyntaxTreeBuilder
	{
		gram := driver.NewGrammar(g)
		toks, err := driver.NewTokenStream(g, bytes.NewReader(c.TestCase.Source))
		if err != nil {
			return &TestResult{
				TestCasePath: c.FilePath,
				Error:        err,
			}
		}
		tb = driver.NewDefaultSyntaxTreeBuilder()
		p, err = driver.NewParser(toks, gram, driver.SemanticAction(driver.NewASTActionSet(gram, tb)))
		if err != nil {
			return &TestResult{
				TestCasePath: c.FilePath,
				Error:        err,
			}
		}
	}

	err := p.Parse()
	if err != nil {
		return &TestResult{
			TestCasePath: c.FilePath,
			Error:        err,
		}
	}

	if tb.Tree() == nil {
		var err error
		if len(p.SyntaxErrors()) > 0 {
			err = fmt.Errorf("parse tree was not generated: syntax error occurred")
		} else {
			// The parser should always generate a parse tree in the vartan-test command, so if there is no parse
			// tree, it is a bug. We also include a stack trace in the error message to be sure.
			err = fmt.Errorf("parse tree was not generated: no syntax error:\n%v", string(debug.Stack()))
		}
		return &TestResult{
			TestCasePath: c.FilePath,
			Error:        err,
		}
	}

	// When a parse tree exists, the test continues regardless of whether or not syntax errors occurred.
	diffs := tspec.DiffTree(c.TestCase.Output, ConvertSyntaxTreeToTestableTree(tb.Tree()).Fill())
	if len(diffs) > 0 {
		return &TestResult{
			TestCasePath: c.FilePath,
			Error:        fmt.Errorf("output mismatch"),
			Diffs:        diffs,
		}
	}
	return &TestResult{
		TestCasePath: c.FilePath,
	}
}

func ConvertSyntaxTreeToTestableTree(dTree *driver.Node) *tspec.Tree {
	if dTree.Text != "" {
		return tspec.NewTerminalNode(dTree.KindName, dTree.Text)
	}

	var children []*tspec.Tree
	if len(dTree.Children) > 0 {
		children = make([]*tspec.Tree, len(dTree.Children))
		for i, c := range dTree.Children {
			children[i] = ConvertSyntaxTreeToTestableTree(c)
		}
	}
	return tspec.NewNonTerminalTree(dTree.KindName, children...)
}