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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package checkers_test
import (
gc "gopkg.in/check.v1"
jc "github.com/juju/testing/checkers"
)
type Inner struct {
First string
Second int `json:",omitempty" yaml:",omitempty"`
Third map[string]bool `json:",omitempty" yaml:",omitempty"`
}
type Outer struct {
First float64
Second []*Inner `json:"Last,omitempty" yaml:"last,omitempty"`
}
func (s *CheckerSuite) TestJSONEquals(c *gc.C) {
tests := []struct {
descr string
obtained string
expected *Outer
result bool
msg string
}{
{
descr: "very simple",
obtained: `{"First": 47.11}`,
expected: &Outer{
First: 47.11,
},
result: true,
}, {
descr: "nested",
obtained: `{"First": 47.11, "Last": [{"First": "Hello", "Second": 42}]}`,
expected: &Outer{
First: 47.11,
Second: []*Inner{
{First: "Hello", Second: 42},
},
},
result: true,
}, {
descr: "nested with newline",
obtained: `{"First": 47.11, "Last": [{"First": "Hello", "Second": 42},
{"First": "World", "Third": {"T": true, "F": false}}]}`,
expected: &Outer{
First: 47.11,
Second: []*Inner{
{First: "Hello", Second: 42},
{First: "World", Third: map[string]bool{
"F": false,
"T": true,
}},
},
},
result: true,
}, {
descr: "illegal field",
obtained: `{"NotThere": 47.11}`,
expected: &Outer{
First: 47.11,
},
result: false,
msg: `mismatch at .*: validity mismatch; .*`,
}, {
descr: "illegal optained content",
obtained: `{"NotThere": `,
result: false,
msg: `cannot unmarshal obtained contents: unexpected end of JSON input; .*`,
},
}
for i, test := range tests {
c.Logf("test #%d) %s", i, test.descr)
result, msg := jc.JSONEquals.Check([]interface{}{test.obtained, test.expected}, nil)
c.Check(result, gc.Equals, test.result)
c.Check(msg, gc.Matches, test.msg)
}
// Test non-string input.
result, msg := jc.JSONEquals.Check([]interface{}{true, true}, nil)
c.Check(result, gc.Equals, false)
c.Check(msg, gc.Matches, "expected string, got bool")
}
func (s *CheckerSuite) TestYAMLEquals(c *gc.C) {
tests := []struct {
descr string
obtained string
expected *Outer
result bool
msg string
}{
{
descr: "very simple",
obtained: `first: 47.11`,
expected: &Outer{
First: 47.11,
},
result: true,
}, {
descr: "nested",
obtained: `{first: 47.11, last: [{first: 'Hello', second: 42}]}`,
expected: &Outer{
First: 47.11,
Second: []*Inner{
{First: "Hello", Second: 42},
},
},
result: true,
}, {
descr: "nested with newline",
obtained: `{first: 47.11, last: [{first: 'Hello', second: 42},
{first: 'World', third: {t: true, f: false}}]}`,
expected: &Outer{
First: 47.11,
Second: []*Inner{
{First: "Hello", Second: 42},
{First: "World", Third: map[string]bool{
"f": false,
"t": true,
}},
},
},
result: true,
}, {
descr: "illegal field",
obtained: `{"NotThere": 47.11}`,
expected: &Outer{
First: 47.11,
},
result: false,
msg: `mismatch at .*: validity mismatch; .*`,
}, {
descr: "illegal obtained content",
obtained: `{"NotThere": `,
result: false,
msg: `cannot unmarshal obtained contents: yaml: line 1: .*`,
},
}
for i, test := range tests {
c.Logf("test #%d) %s", i, test.descr)
result, msg := jc.YAMLEquals.Check([]interface{}{test.obtained, test.expected}, nil)
c.Check(result, gc.Equals, test.result)
c.Check(msg, gc.Matches, test.msg)
}
// Test non-string input.
result, msg := jc.YAMLEquals.Check([]interface{}{true, true}, nil)
c.Check(result, gc.Equals, false)
c.Check(msg, gc.Matches, "expected string, got bool")
}
|