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
/
func_findinmap_test.go
55 lines (43 loc) · 1.73 KB
/
func_findinmap_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
package cloudformation
import (
"encoding/json"
. "gopkg.in/check.v1"
)
type FindInMapFuncTest struct{}
var _ = Suite(&FindInMapFuncTest{})
func (testSuite *FindInMapFuncTest) TestBasics(c *C) {
inputBuf := `{ "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },
{ "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] }`
f, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, IsNil)
c.Assert(f.(StringFunc).String(), DeepEquals, FindInMap(
"AWSRegionArch2AMI", Ref("AWS::Region"),
FindInMap("AWSInstanceType2Arch", Ref("InstanceType"),
String("Arch"))))
// old way
c.Assert(f.(StringFunc).String(), DeepEquals, FindInMap(
"AWSRegionArch2AMI", *Ref("AWS::Region").String(),
*FindInMap("AWSInstanceType2Arch", *Ref("InstanceType").String(),
*String("Arch")).String()).String())
// tidy the JSON input
inputStruct := map[string]interface{}{}
_ = json.Unmarshal([]byte(inputBuf), &inputStruct)
expectedBuf, _ := json.Marshal(inputStruct)
buf, err := json.Marshal(f)
c.Assert(err, IsNil)
c.Assert(string(buf), Equals, string(expectedBuf))
}
func (testSuite *FindInMapFuncTest) TestFailures(c *C) {
inputBuf := `{"Fn::FindInMap": 1}`
_, err := unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")
inputBuf = `{"Fn::FindInMap": [1, "2", "3"]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")
inputBuf = `{"Fn::FindInMap": ["1", 2, "3"]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")
inputBuf = `{"Fn::FindInMap": ["1", "2", 3]}`
_, err = unmarshalFunc([]byte(inputBuf))
c.Assert(err, ErrorMatches, "cannot decode function")
}