/
murex.go
167 lines (138 loc) · 4.49 KB
/
murex.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package test
import (
"fmt"
"regexp"
"strings"
"testing"
"github.com/lmorg/murex/config"
"github.com/lmorg/murex/config/defaults"
"github.com/lmorg/murex/lang"
"github.com/lmorg/murex/lang/ref"
"github.com/lmorg/murex/test/count"
)
// MurexTest is a basic framework to test murex code.
// Please note this shouldn't be confused with the murex scripting language's inbuilt testing framework!
type MurexTest struct {
Block string
Stdout string
Stderr string
ExitNum int
}
// RunMurexTests runs through all the test cases for MurexTest where STDOUT/ERR are literal strings
func RunMurexTests(tests []MurexTest, t *testing.T) {
t.Helper()
count.Tests(t, len(tests))
defaults.Config(config.InitConf, false)
lang.InitEnv()
for i := range tests {
hasError := false
fork := lang.ShellProcess.Fork(lang.F_FUNCTION | lang.F_NEW_MODULE | lang.F_NO_STDIN | lang.F_CREATE_STDOUT | lang.F_CREATE_STDERR)
fork.Name.Set("RunMurexTests()")
fork.FileRef = &ref.File{Source: &ref.Source{Module: fmt.Sprintf("murex/%s-%d", t.Name(), i)}}
exitNum, err := fork.Execute([]rune(tests[i].Block))
if err != nil {
t.Errorf("Cannot execute script on test %d", i)
t.Log(" ", err)
continue
}
bErr, err := fork.Stderr.ReadAll()
if err != nil {
t.Errorf("Cannot ReadAll() from Stderr on test %d", i)
t.Log(" ", err)
continue
}
if string(bErr) != tests[i].Stderr {
hasError = true
}
bOut, err := fork.Stdout.ReadAll()
if err != nil {
t.Errorf("Cannot ReadAll() from Stdout on test %d", i)
t.Log(" ", err)
continue
}
if string(bOut) != tests[i].Stdout {
hasError = true
}
if exitNum != tests[i].ExitNum {
hasError = true
}
if hasError {
t.Errorf("Code block doesn't return expected values in test %d", i)
t.Log(" Code block: ", tests[i].Block)
t.Log(" Expected Stdout: ", strings.Replace(tests[i].Stdout, "\n", `\n`, -1))
t.Log(" Actual Stdout: ", strings.Replace(string(bOut), "\n", `\n`, -1))
t.Log(" Exp. out bytes: ", []byte(tests[i].Stdout))
t.Log(" Act. out bytes: ", bOut)
t.Log(" Expected Stderr: ", strings.Replace(tests[i].Stderr, "\n", `\n`, -1))
t.Log(" Actual Stderr: ", strings.Replace(string(bErr), "\n", `\n`, -1))
t.Log(" Exp. err bytes: ", []byte(tests[i].Stderr))
t.Log(" Act. err bytes: ", bErr)
t.Log(" Expected exitnum:", tests[i].ExitNum)
t.Log(" Actual exitnum: ", exitNum)
}
}
}
// RunMurexTestsRx runs through all the test cases for MurexTest where STDOUT/ERR are regexp expressions
func RunMurexTestsRx(tests []MurexTest, t *testing.T) {
t.Helper()
count.Tests(t, len(tests))
defaults.Config(config.InitConf, false)
lang.InitEnv()
for i := range tests {
hasError := false
fork := lang.ShellProcess.Fork(lang.F_FUNCTION | lang.F_NEW_MODULE | lang.F_NO_STDIN | lang.F_CREATE_STDOUT | lang.F_CREATE_STDERR)
fork.Name.Set("RunMurexTestsRx()")
fork.FileRef = &ref.File{Source: &ref.Source{Module: fmt.Sprintf("murex/%s-%d", t.Name(), i)}}
exitNum, err := fork.Execute([]rune(tests[i].Block))
if err != nil {
t.Errorf("Cannot execute script on test %d", i)
t.Log(" ", err)
continue
}
bErr, err := fork.Stderr.ReadAll()
if err != nil {
t.Errorf("Cannot ReadAll() from Stderr on test %d", i)
t.Log(" ", err)
continue
}
rxErr, err := regexp.Compile(tests[i].Stderr)
if err != nil {
t.Errorf("Cannot compile regexp expression for STDERR on test %d", i)
t.Log(" ", err)
continue
}
if !rxErr.MatchString(string(bErr)) {
hasError = true
}
bOut, err := fork.Stdout.ReadAll()
if err != nil {
t.Errorf("Cannot ReadAll() from Stdout on test %d", i)
t.Log(" ", err)
continue
}
rxOut, err := regexp.Compile(tests[i].Stdout)
if err != nil {
t.Errorf("Cannot compile regexp expression for STDOUT on test %d", i)
t.Log(" ", err)
continue
}
if !rxOut.MatchString(string(bOut)) {
hasError = true
}
if exitNum != tests[i].ExitNum {
hasError = true
}
if hasError {
t.Errorf("Code block doesn't return expected values in test %d", i)
t.Log(" Code block: ", tests[i].Block)
t.Log(" Expected out rx: ", tests[i].Stdout)
t.Log(" Actual Stdout: ", strings.Replace(string(bOut), "\n", `\n`, -1))
t.Log(" Act. out bytes: ", bOut)
t.Log(" Expected err rx: ", tests[i].Stderr)
t.Log(" Actual Stderr: ", strings.Replace(string(bErr), "\n", `\n`, -1))
t.Log(" Act. err bytes: ", bErr)
t.Log(" Expected exitnum:", tests[i].ExitNum)
t.Log(" Actual exitnum: ", exitNum)
}
}
}