forked from crewjam/go-cloudformation
/
bool_test.go
45 lines (34 loc) · 1.12 KB
/
bool_test.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
package cloudformation
import (
"encoding/json"
. "gopkg.in/check.v1"
)
type BoolTest struct{}
var _ = Suite(&BoolTest{})
func (testSuite *BoolTest) TestBool(c *C) {
inputBuf := `{"B": true, "C": false, "D": {"Ref": "foo"}, "E": "true"}`
v := struct {
A *BoolExpr `json:",omitempty"`
B *BoolExpr `json:",omitempty"`
C *BoolExpr `json:",omitempty"`
D *BoolExpr `json:",omitempty"`
E *BoolExpr `json:",omitempty"`
}{}
err := json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, IsNil)
c.Assert(v.A, IsNil)
c.Assert(v.B, DeepEquals, Bool(true))
c.Assert(v.C, DeepEquals, Bool(false))
c.Assert(v.D, DeepEquals, Ref("foo").Bool())
c.Assert(v.E, DeepEquals, Bool(true))
buf, err := json.Marshal(v)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals,
`{"B":true,"C":false,"D":{"Ref":"foo"},"E":true}`)
inputBuf = `{"A": "invalid"}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, ErrorMatches, "json: cannot unmarshal string into Go value of type bool")
inputBuf = `{"A": {"Fn::Missing": "invalid"}}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, ErrorMatches, "unknown function Fn::Missing")
}