-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoutput_parser_test.go
59 lines (50 loc) · 1.39 KB
/
output_parser_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
package vagrant
import (
"reflect"
"strings"
"testing"
)
type MockOutputParser struct {
OutputParser
}
type ExpectedOutput struct {
Target string
Key string
Message []string
Seen bool
}
type TestOutputHandler struct {
ExpectedLines []*ExpectedOutput
}
func (parser MockOutputParser) Run(output string, handler outputHandler) {
for _, line := range strings.Split(output, "\n") {
parser.parseLine(line, handler)
}
}
func (expected *ExpectedOutput) Matches(target, key string, message []string) bool {
return expected.Target == target &&
expected.Key == key &&
reflect.DeepEqual(expected.Message, message)
}
func (handler *TestOutputHandler) handleOutput(target, key string, message []string) {
for _, expected := range handler.ExpectedLines {
if expected.Matches(target, key, message) {
expected.Seen = true
}
}
}
func (handler TestOutputHandler) AssertAllSeen(t *testing.T) {
for _, expected := range handler.ExpectedLines {
if !expected.Seen {
t.Errorf("Line not seen: %v", expected)
}
}
}
func TestVagrantOutputParser_parseLine(t *testing.T) {
parser := OutputParser{}
handler := TestOutputHandler{[]*ExpectedOutput{
{Target: "default", Key: "ui", Message: []string{"info", "This is a test, with commas.\nAnd newlines."}},
}}
parser.parseLine("123,default,ui,info,This is a test%!(VAGRANT_COMMA) with commas.\\nAnd newlines.", &handler)
handler.AssertAllSeen(t)
}