~sinzui/ubuntu/wily/juju-core/wily-1.24.7

« back to all changes in this revision

Viewing changes to src/github.com/juju/testing/checkers/codec.go

  • Committer: Package Import Robot
  • Author(s): Oleg Strikov
  • Date: 2015-03-26 15:54:39 UTC
  • mfrom: (1.1.32)
  • Revision ID: package-import@ubuntu.com-20150326155439-ot7bwwyoomq13btm
Tags: 1.22.0-0ubuntu1
* New upstream release (LP: #1416051).
* d/patches/fix-detect-new-release.patch: Added upstream patch to redeem
  the ability to handle future Ubuntu releases (LP: #1427879, #1434092).
* d/tests/fake-future.sh: New ability to generate fake /etc/os-release.
* d/copyright: Updated to reflect changes in the codebase.
* d/control:
  - Change build dependency from gccgo to gccgo-go.
  - Use either cloud-image-utils or cloud-utils as dependency for juju-local
    because cloud-image-utils is not available on precise.
  - Compliance to Debian Policy 3.9.6 was declared.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012-2014 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package checkers
 
5
 
 
6
import (
 
7
        "encoding/json"
 
8
        "fmt"
 
9
 
 
10
        gc "gopkg.in/check.v1"
 
11
        "gopkg.in/yaml.v1"
 
12
)
 
13
 
 
14
type codecEqualChecker struct {
 
15
        name      string
 
16
        marshal   func(interface{}) ([]byte, error)
 
17
        unmarshal func([]byte, interface{}) error
 
18
}
 
19
 
 
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{
 
27
        name:      "JSONEquals",
 
28
        marshal:   json.Marshal,
 
29
        unmarshal: json.Unmarshal,
 
30
}
 
31
 
 
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{
 
39
        name:      "YAMLEquals",
 
40
        marshal:   yaml.Marshal,
 
41
        unmarshal: yaml.Unmarshal,
 
42
}
 
43
 
 
44
func (checker *codecEqualChecker) Info() *gc.CheckerInfo {
 
45
        return &gc.CheckerInfo{
 
46
                Name:   checker.name,
 
47
                Params: []string{"obtained", "expected"},
 
48
        }
 
49
}
 
50
 
 
51
func (checker *codecEqualChecker) Check(params []interface{}, names []string) (result bool, error string) {
 
52
        gotContent, ok := params[0].(string)
 
53
        if !ok {
 
54
                return false, fmt.Sprintf("expected string, got %T", params[0])
 
55
        }
 
56
        expectContent := params[1]
 
57
        expectContentBytes, err := checker.marshal(expectContent)
 
58
        if err != nil {
 
59
                return false, fmt.Sprintf("cannot marshal expected contents: %v", err)
 
60
        }
 
61
        var expectContentVal interface{}
 
62
        if err := checker.unmarshal(expectContentBytes, &expectContentVal); err != nil {
 
63
                return false, fmt.Sprintf("cannot unmarshal expected contents: %v", err)
 
64
        }
 
65
 
 
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)
 
69
        }
 
70
 
 
71
        if ok, err := DeepEqual(gotContentVal, expectContentVal); !ok {
 
72
                return false, err.Error()
 
73
        }
 
74
        return true, ""
 
75
}