forked from crewjam/go-cloudformation
/
string_test.go
51 lines (38 loc) · 1.32 KB
/
string_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
46
47
48
49
50
51
package cloudformation
import (
"encoding/json"
"testing"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type StringTest struct{}
var _ = Suite(&StringTest{})
func (testSuite *StringTest) TestString(c *C) {
inputBuf := `{"B": "one", "C": "two", "D": {"Ref": "foo"}}`
v := struct {
A *StringExpr `json:",omitempty"`
B *StringExpr `json:",omitempty"`
C *StringExpr `json:",omitempty"`
D *StringExpr `json:",omitempty"`
}{}
err := json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, IsNil)
c.Assert(v.A, IsNil)
c.Assert(v.B, DeepEquals, String("one"))
c.Assert(v.C, DeepEquals, String("two"))
c.Assert(v.D, DeepEquals, Ref("foo").String())
buf, err := json.Marshal(v)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals,
`{"B":"one","C":"two","D":{"Ref":"foo"}}`)
inputBuf = `{"A": false}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, ErrorMatches, "json: cannot unmarshal bool into Go struct field .A of type string")
inputBuf = `{"A": 1}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, ErrorMatches, "json: cannot unmarshal number into Go struct field .A of type string")
inputBuf = `{"A": {"Fn::Missing": "hello"}}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, ErrorMatches, "unknown function Fn::Missing")
}