Repository navigation
Expand file tree
/
Copy pathparser_source_test.go
More file actions
98 lines (79 loc) 路 2.01 KB
/
Copy pathparser_source_test.go
File metadata and controls
98 lines (79 loc) 路 2.01 KB
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
package main
import (
"os"
"testing"
)
type parseSourceTest struct {
tape string
srcTape string
errors []string
writeFile bool
}
func (st *parseSourceTest) run(t *testing.T) {
if st.writeFile {
err := os.WriteFile("source.tape", []byte(st.srcTape), os.ModePerm)
if err != nil {
t.Fatalf(err.Error())
}
}
l := NewLexer(st.tape)
p := NewParser(l)
_ = p.Parse()
if len(p.errors) != len(st.errors) {
t.Fatalf("Expected errors: %d, errors: %d", len(st.errors), len(p.errors))
}
for i := range st.errors {
err := p.errors[i].Msg
expected := st.errors[i]
if err != expected {
t.Errorf("Expected error: %s, actual error %s", expected, err)
}
}
os.Remove("source.tape")
}
func TestParseSource(t *testing.T) {
t.Run("should not return errors when tape exist and is NOT empty", func(t *testing.T) {
test := &parseSourceTest{
tape: "Source source.tape",
srcTape: `Type "echo 'Welcome to VHS!'"`,
writeFile: true,
}
test.run(t)
})
t.Run("should return errors when tape NOT found", func(t *testing.T) {
test := &parseSourceTest{
tape: "Source source.tape",
errors: []string{"File source.tape not found"},
writeFile: false,
}
test.run(t)
})
t.Run("should return error when tape extension is NOT (.tape)", func(t *testing.T) {
test := &parseSourceTest{
tape: "Source source.vhs",
errors: []string{"Expected file with .tape extension"},
writeFile: true,
}
test.run(t)
})
t.Run("should return error when Source command does NOT have tape path", func(t *testing.T) {
test := &parseSourceTest{
tape: "Source",
errors: []string{"Expected path after Source"},
writeFile: true,
}
test.run(t)
})
t.Run("should return error when find nested Source commands", func(t *testing.T) {
test := &parseSourceTest{
tape: "Source source.tape",
srcTape: `Type "echo 'Welcome to VHS!'"
Source magic.tape
Type "goodbye"
`,
errors: []string{"Nested Source detected"},
writeFile: true,
}
test.run(t)
})
}