-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_plan_test.go
81 lines (73 loc) · 1.89 KB
/
test_plan_test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package ysgo_test
import (
"fmt"
"os"
"strconv"
"strings"
)
const (
stepTypeLine = iota
stepTypeOption
stepTypeSelect
stepTypeCommand
stepTypeStop
stepTypeSet
)
type TestPlanStep struct {
stepType int
stringValue string
intValue int
expectOptionEnabled bool
}
func newTestPlanStep(line string) (*TestPlanStep, error) {
step := &TestPlanStep{}
line = strings.TrimSpace(line)
if len(line) == 0 || strings.HasPrefix(line, "#") {
return nil, nil
}
switch {
case strings.HasPrefix(line, "line:"):
step.stepType = stepTypeLine
step.stringValue = line[6:]
case strings.HasPrefix(line, "option:"):
step.stepType = stepTypeOption
step.stringValue = line[8:]
step.expectOptionEnabled = true
if strings.HasSuffix(step.stringValue, " [disabled]") {
step.expectOptionEnabled = false
step.stringValue = strings.TrimSuffix(step.stringValue, " [disabled]")
}
case strings.HasPrefix(line, "command:"):
step.stepType = stepTypeCommand
step.stringValue = line[9:]
case strings.HasPrefix(line, "select:"):
step.stepType = stepTypeSelect
i, err := strconv.Atoi(line[8:])
if err != nil {
return nil, fmt.Errorf("failed to parse select index: %w", err)
}
step.intValue = i
case strings.HasPrefix(line, "stop:"):
step.stepType = stepTypeStop
case line == "stop":
return nil, nil
}
return step, nil
}
type TestPlan []*TestPlanStep
func parseTestPlan(path string) (TestPlan, error) {
fileContent, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read test plan file: %w", err)
}
lines := strings.Split(string(fileContent), "\n")
plan := make(TestPlan, 0, len(lines))
for i, line := range lines {
if step, err := newTestPlanStep(line); err != nil {
return nil, fmt.Errorf("failed to parse line [%v] of test plan file: %w", i, err)
} else if step != nil {
plan = append(plan, step)
}
}
return plan, nil
}