-
Notifications
You must be signed in to change notification settings - Fork 26
/
match_toml.go
63 lines (51 loc) · 1.41 KB
/
match_toml.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
package matchers
import (
"fmt"
"reflect"
"github.com/BurntSushi/toml"
"github.com/onsi/gomega/types"
)
func MatchTOML(expected interface{}) types.GomegaMatcher {
return &matchTOML{
expected: expected,
}
}
type matchTOML struct {
expected interface{}
}
func (matcher *matchTOML) Match(actual interface{}) (success bool, err error) {
var e, a string
switch eType := matcher.expected.(type) {
case string:
e = eType
case []byte:
e = string(eType)
default:
return false, fmt.Errorf("expected value must be []byte or string, received %T", matcher.expected)
}
switch aType := actual.(type) {
case string:
a = aType
case []byte:
a = string(aType)
default:
return false, fmt.Errorf("actual value must be []byte or string, received %T", matcher.expected)
}
var eValue map[string]interface{}
_, err = toml.Decode(e, &eValue)
if err != nil {
return false, err
}
var aValue map[string]interface{}
_, err = toml.Decode(a, &aValue)
if err != nil {
return false, err
}
return reflect.DeepEqual(eValue, aValue), nil
}
func (matcher *matchTOML) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected\n%s\nto match the TOML representation of\n%s", actual, matcher.expected)
}
func (matcher *matchTOML) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected\n%s\nnot to match the TOML representation of\n%s", actual, matcher.expected)
}