1
// Copyright 2012-2014 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
10
gc "gopkg.in/check.v1"
14
type codecEqualChecker struct {
16
marshal func(interface{}) ([]byte, error)
17
unmarshal func([]byte, interface{}) error
20
// JSONEquals defines a checker that checks whether a byte slice, when
21
// unmarshaled as JSON, is equal to the given value.
22
// Rather than unmarshaling into something of the expected
23
// body type, we reform the expected body in JSON and
24
// back to interface{}, so we can check the whole content.
25
// Otherwise we lose information when unmarshaling.
26
var JSONEquals = &codecEqualChecker{
28
marshal: json.Marshal,
29
unmarshal: json.Unmarshal,
32
// YAMLEquals defines a checker that checks whether a byte slice, when
33
// unmarshaled as YAML, is equal to the given value.
34
// Rather than unmarshaling into something of the expected
35
// body type, we reform the expected body in YAML and
36
// back to interface{}, so we can check the whole content.
37
// Otherwise we lose information when unmarshaling.
38
var YAMLEquals = &codecEqualChecker{
40
marshal: yaml.Marshal,
41
unmarshal: yaml.Unmarshal,
44
func (checker *codecEqualChecker) Info() *gc.CheckerInfo {
45
return &gc.CheckerInfo{
47
Params: []string{"obtained", "expected"},
51
func (checker *codecEqualChecker) Check(params []interface{}, names []string) (result bool, error string) {
52
gotContent, ok := params[0].(string)
54
return false, fmt.Sprintf("expected string, got %T", params[0])
56
expectContent := params[1]
57
expectContentBytes, err := checker.marshal(expectContent)
59
return false, fmt.Sprintf("cannot marshal expected contents: %v", err)
61
var expectContentVal interface{}
62
if err := checker.unmarshal(expectContentBytes, &expectContentVal); err != nil {
63
return false, fmt.Sprintf("cannot unmarshal expected contents: %v", err)
66
var gotContentVal interface{}
67
if err := checker.unmarshal([]byte(gotContent), &gotContentVal); err != nil {
68
return false, fmt.Sprintf("cannot unmarshal obtained contents: %v; %q", err, gotContent)
71
if ok, err := DeepEqual(gotContentVal, expectContentVal); !ok {
72
return false, err.Error()