summaryrefslogtreecommitdiff
path: root/assert.go
blob: 3e70bd80ea87530ee508fb2c223df06062c36794 (plain)
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
package main

import (
	"bufio"
	"errors"
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"strings"
)

func getTestCase(outdir, testCase string) (string, int, error) {
	tetRoot := env("TET_ROOT", "/usr/local/share")
	tetScen := filepath.Join(tetRoot, "xts5", "tet_scen")
	in, err := os.Open(tetScen)
	if err != nil {
		return "", 0, err
	}
	defer in.Close()

	s := bufio.NewScanner(in)
	for s.Scan() {
		line := s.Text()
		if line == testCase {
			num := 0
			for s.Scan() {
				line := s.Text()
				switch {
				case strings.Contains(line, "CASE"):
					f := strings.Fields(line)
					if len(f) < 1 {
						break
					}
					count := f[len(f)-1]
					if !strings.HasSuffix(count, `"`) {
						break
					}
					n, err := strconv.Atoi(count[:len(count)-1])
					if err == nil {
						num = n
					}
				case strings.Contains(line, "/"):
					return strings.TrimSpace(line), num, nil
				}
			}
			break
		}
	}
	return "", 0, s.Err()
}

func runAssert(outdir string) (int, error) {
	testCase := flag.Arg(0)
	var assertions []int
	for _, v := range flag.Args()[1:] {
		a, err := strconv.ParseInt(v, 10, 16)
		if err != nil {
			return 1, err
		}
		assertions = append(assertions, int(a))
	}
	if len(assertions) == 0 {
		return 1, errors.New("no assertion(s) were specified")
	}

	testApp, num, err := getTestCase(outdir, testCase)
	if err != nil {
		return 1, err
	}
	if testApp == "" {
		return 1, errors.New("cannot find test " + testCase)
	}

	aScen := filepath.Join(outdir, "assert_scen")
	out, err := os.Create(aScen)
	if err != nil {
		return 1, err
	}
	defer out.Close()

	fmt.Fprintln(out, testCase)
	fmt.Fprintf(out, "  \"VSW5TESTSUITE CASE %s %d\"\n", testCase, num)
	for _, a := range assertions {
		if a <= 0 || a > num {
			return 1, fmt.Errorf("%s does not contain %d tests", testCase, a)
		}
		fmt.Fprintf(out, "  %s{%d}\n", testApp, a)
	}

	args := []string{"-e", "-i", outdir, "-x", *config, "-s", aScen, "xts5", testCase}
	return tcc(args)
}