/
testcase.go
65 lines (59 loc) · 1.89 KB
/
testcase.go
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
package core
import (
"errors"
"os"
"path"
"path/filepath"
"strings"
"github.com/spf13/afero"
)
// TestCase represent testcase. A test case is a pair of text file that defines input and expected output of a test case.
// A file named "example.in" and "example.out" in current working directory considered as a test case named "example".
// Name property store test case's name. InputPath contain path to test case's input file. OutputPath contains path
// to test case's expected output file.
type TestCase struct {
Name string
InputPath string
OutputPath string
}
// ErrNoSuchTestCase indicates testcase not found
var ErrNoSuchTestCase = errors.New("No such testcase")
func (cptool *CPTool) getTestCaseByName(testcaseName string) (TestCase, error) {
for _, testCase := range cptool.getAllTestCaseWithPrefix(testcaseName) {
if testCase.Name == testcaseName {
return testCase, nil
}
}
return TestCase{}, ErrNoSuchTestCase
}
func (cptool *CPTool) getAllTestCaseWithPrefix(testcasePrefix string) []TestCase {
testCases := make([]TestCase, 0)
cwd := filepath.Clean(cptool.workingDirectory)
afero.Walk(cptool.fs, cwd, func(testPath string, info os.FileInfo, err error) error {
if info.IsDir() && testPath != cwd {
return filepath.SkipDir
}
relativePath := filepath.Clean(testPath)[len(cwd):]
if len(relativePath) == 0 {
return nil
}
if relativePath[0] == '/' {
relativePath = relativePath[1:]
}
if strings.HasPrefix(relativePath, testcasePrefix) && filepath.Ext(testPath) == ".in" {
testName := relativePath[:len(relativePath)-3]
outputFilePath := path.Join(cptool.workingDirectory, testName+".out")
info, err := cptool.fs.Stat(outputFilePath)
if err != nil || info.IsDir() {
return nil
}
testCases = append(testCases, TestCase{
Name: testName,
InputPath: testPath,
OutputPath: outputFilePath,
})
}
return nil
})
return testCases
}