aboutsummaryrefslogtreecommitdiff
path: root/cmd/vartan/test.go
blob: 73949e01c0403a17814df9bd773fe369166615eb (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
package main

import (
	"errors"
	"fmt"
	"os"

	"tester"
)

/*
func init() {
	cmd := &cobra.Command{
		Use:     "test <grammar file path> <test file path>|<test directory path>",
		Short:   "Test a grammar",
		Example: `  vartan test grammar.vartan test`,
		Args:    cobra.ExactArgs(2),
		RunE:    runTest,
	}
	rootCmd.AddCommand(cmd)
}
*/

func runTest(args []string) error {
	gram, _, err := readGrammar(args[0])
	if err != nil {
		return fmt.Errorf("Cannot read a grammar: %w", err)
	}

	var cs []*tester.TestCaseWithMetadata
	{
		cs = tester.ListTestCases(args[1])
		errOccurred := false
		for _, c := range cs {
			if c.Error != nil {
				fmt.Fprintf(os.Stderr, "Failed to read a test case or a directory: %v\n%v\n", c.FilePath, c.Error)
				errOccurred = true
			}
		}
		if errOccurred {
			return errors.New("Cannot run test")
		}
	}

	t := &tester.Tester{
		Grammar: gram,
		Cases:   cs,
	}
	rs := t.Run()
	testFailed := false
	for _, r := range rs {
		fmt.Fprintln(os.Stdout, r)
		if r.Error != nil {
			testFailed = true
		}
	}
	if testFailed {
		return errors.New("Test failed")
	}
	return nil
}