This repository has been archived by the owner on Jan 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
string_list_test.go
69 lines (54 loc) · 1.93 KB
/
string_list_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cloudformation
import (
"encoding/json"
. "gopkg.in/check.v1"
)
type StringListTest struct{}
var _ = Suite(&StringListTest{})
func (testSuite *StringListTest) TestStringList(c *C) {
inputBuf := `{"B": ["one"], "C": ["two", {"Ref": "foo"}]}`
v := struct {
A *StringListExpr `json:",omitempty"`
B *StringListExpr `json:",omitempty"`
C *StringListExpr `json:",omitempty"`
}{}
err := json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, IsNil)
c.Assert(v.A, IsNil)
c.Assert(v.B, DeepEquals, StringList(String("one")))
c.Assert(v.C, DeepEquals, StringList(String("two"), Ref("foo")))
// old way still works
c.Assert(v.B, DeepEquals, StringList(*String("one")))
c.Assert(v.C, DeepEquals, StringList(*String("two"), *Ref("foo").String()))
buf, err := json.Marshal(v)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals,
`{"B":["one"],"C":["two",{"Ref":"foo"}]}`)
v.B, v.C = nil, nil
inputBuf = `{"A":{"Fn::GetAZs":""}}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, IsNil)
c.Assert(v.A, DeepEquals, GetAZs(String("")))
c.Assert(v.A, DeepEquals, GetAZs(*String("")).StringList()) // old way still works
buf, err = json.Marshal(v)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals, inputBuf)
inputBuf = `{"A": false}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, ErrorMatches, "json: cannot unmarshal .*")
// A single string where a string list is expected returns
// a string list.
inputBuf = `{"A": "asdf"}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, IsNil)
buf, err = json.Marshal(v)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals, `{"A":["asdf"]}`)
inputBuf = `{"A": [false]}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, ErrorMatches, "json: cannot unmarshal .*")
// Base64 is not available in stringlist context
inputBuf = `{"A": {"Fn::Base64": "hello"}}`
err = json.Unmarshal([]byte(inputBuf), &v)
c.Assert(err, ErrorMatches, ".* is not a StringListFunc")
}