@@ -7,42 +7,79 @@ import (
7
7
"testing"
8
8
)
9
9
10
- type ValidationFunction = func (output []byte , err error ) (bool , []string )
10
+ type ValidationResult struct {
11
+ Error error
12
+ Output []byte
13
+ Message []string
14
+ Success bool
15
+ }
16
+
17
+ func (v ValidationResult ) Test (t * testing.T ) (output []byte , err error ) {
18
+ t .Log (fmt .Sprintf ("%s\n " , output ))
19
+ if ! v .Success {
20
+ t .Fatal (v .Message )
21
+ }
22
+ return v .Output , v .Error
23
+ }
24
+
25
+ type ValidationFunction = func (output []byte , err error ) ValidationResult
26
+
27
+ func SuccessValidation (output []byte , err error ) ValidationResult {
28
+ return ValidationResult {nil , output , []string {}, true }
29
+ }
11
30
12
- func EmptyValidation (output []byte , err error ) ( bool , [] string ) {
13
- return true , []string {}
31
+ func FailureValidation (output []byte , err error ) ValidationResult {
32
+ return ValidationResult { err , output , []string {fmt . Sprintf ( "%s" , err )}, false }
14
33
}
15
34
16
- func NoErr (output []byte , err error ) (bool , []string ) {
17
- return err == nil , []string {fmt .Sprintf ("%s" , err )}
35
+ func NoErr (output []byte , err error ) ValidationResult {
36
+ if err == nil {
37
+ return SuccessValidation (output , err )
38
+ }
39
+ return FailureValidation (output , err )
18
40
}
19
41
20
- func IsErr (output []byte , err error ) (bool , []string ) {
21
- return err != nil , []string {fmt .Sprintf ("%s" , err )}
42
+ func IsErr (output []byte , err error ) ValidationResult {
43
+ if err != nil {
44
+ return SuccessValidation (output , err )
45
+ }
46
+ return ValidationResult {nil , output , []string {"Expected error to occur" }, false }
22
47
}
23
48
24
- func ValidRegex (pattern string ) func (output []byte , err error ) ( bool , [] string ) {
25
- return func (output []byte , err error ) ( bool , [] string ) {
49
+ func ValidRegex (pattern string ) func (output []byte , err error ) ValidationResult {
50
+ return func (output []byte , err error ) ValidationResult {
26
51
matched , errMatch := regexp .MatchString (pattern , fmt .Sprintf ("%s" , output ))
27
52
if errMatch != nil {
28
- return false , []string {fmt .Sprintf ("Error in regexp %s when trying to match %s" , errMatch , pattern )}
53
+ return ValidationResult {
54
+ Success : false ,
55
+ Message : []string {fmt .Sprintf ("Error in regexp %s when trying to match %s" , errMatch , pattern )},
56
+ Error : err ,
57
+ Output : output ,
58
+ }
59
+
29
60
}
30
61
if ! matched {
31
- return false , []string {fmt .Sprintf ("Expected combined output matching %s" , pattern )}
62
+ return ValidationResult {
63
+ Success : false ,
64
+ Message : []string {fmt .Sprintf ("Expected combined output matching %s" , pattern )},
65
+ Error : nil ,
66
+ Output : output ,
67
+ }
68
+
32
69
}
33
- return true , [] string {}
70
+ return SuccessValidation ( output , err )
34
71
}
35
72
}
36
73
37
74
func All (vs ... ValidationFunction ) ValidationFunction {
38
- return func (output []byte , err error ) ( bool , [] string ) {
75
+ return func (output []byte , err error ) ValidationResult {
39
76
for _ , v := range vs {
40
- r , o := v (output , err )
41
- if ! r {
42
- return r , o
77
+ r := v (output , err )
78
+ if ! r . Success {
79
+ return r
43
80
}
44
81
}
45
- return true , []string {}
82
+ return ValidationResult { nil , output , []string {}, true }
46
83
}
47
84
}
48
85
@@ -60,18 +97,12 @@ func (c CmdDesc) Should(validator ValidationFunction) CmdDesc {
60
97
return CmdDesc {c .executable , c .Arg , All (c .Validator , validator )}
61
98
}
62
99
63
- func (c CmdDesc ) Run (t * testing.T ) ([]byte , error ) {
64
- t .Log (c .Arg )
100
+ func (c CmdDesc ) Run () ValidationResult {
65
101
cmd := exec .Command (c .executable , c .Arg ... )
66
102
output , err := cmd .CombinedOutput ()
67
- t .Log (fmt .Sprintf ("%s\n " , output ))
68
- v , errs := c .Validator (output , err )
69
- if ! v {
70
- t .Fatal (errs )
71
- }
72
- return output , err
103
+ return c .Validator (output , err )
73
104
}
74
105
75
106
func ValidatedCmd (executable string , arg ... string ) CmdDesc {
76
- return CmdDesc {executable , arg , EmptyValidation }
107
+ return CmdDesc {executable , arg , SuccessValidation }
77
108
}
0 commit comments