github.com/aws-cloudformation/cloudformation-cli-go-plugin@v1.2.0/cfn/encoding/stringify_test.go (about) 1 package encoding_test 2 3 import ( 4 "testing" 5 6 "github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/encoding" 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/google/go-cmp/cmp" 9 ) 10 11 func TestStringifyTypes(t *testing.T) { 12 type Struct struct { 13 S string 14 } 15 16 s := "foo" 17 b := true 18 i := 42 19 f := 3.14 20 l := []interface{}{s, b, i, f} 21 m := map[string]interface{}{ 22 "l": l, 23 } 24 o := Struct{S: s} 25 var nilPointer *Struct 26 27 for _, testCase := range []struct { 28 data interface{} 29 expected interface{} 30 }{ 31 // Basic types 32 {s, "foo"}, 33 {b, "true"}, 34 {i, "42"}, 35 {f, "3.14"}, 36 {l, []interface{}{"foo", "true", "42", "3.14"}}, 37 {m, map[string]interface{}{"l": []interface{}{"foo", "true", "42", "3.14"}}}, 38 {o, struct{ S string }{S: "foo"}}, 39 40 // Pointers 41 {&s, "foo"}, 42 {&b, "true"}, 43 {&i, "42"}, 44 {&f, "3.14"}, 45 {&l, []interface{}{"foo", "true", "42", "3.14"}}, 46 {&m, map[string]interface{}{"l": []interface{}{"foo", "true", "42", "3.14"}}}, 47 {&o, struct{ S string }{S: "foo"}}, 48 49 // Nils are stripped 50 {map[string]interface{}{"foo": nil}, map[string]interface{}{}}, 51 52 // Nil pointers are nil 53 {nilPointer, nil}, 54 55 // Nils are nil 56 {nil, nil}, 57 } { 58 actual, err := encoding.Stringify(testCase.data) 59 if err != nil { 60 t.Fatal(err) 61 } 62 63 if d := cmp.Diff(actual, testCase.expected); d != "" { 64 t.Errorf(d) 65 } 66 } 67 } 68 69 func TestStringifyModel(t *testing.T) { 70 type Model struct { 71 BucketName *string 72 Key *string 73 Body *string 74 IsBase64Encoded *bool 75 ContentType *string 76 ContentLength *int 77 ACL *string 78 Grants map[string][]string 79 } 80 81 m := Model{ 82 BucketName: aws.String("foo"), 83 Key: aws.String("bar"), 84 Body: aws.String("baz"), 85 ContentType: aws.String("quux"), 86 ACL: aws.String("mooz"), 87 } 88 89 expected := struct { 90 BucketName string 91 Key string 92 Body string 93 IsBase64Encoded string 94 ContentType string 95 ContentLength string 96 ACL string 97 Grants map[string]interface{} 98 }{ 99 BucketName: "foo", 100 Key: "bar", 101 Body: "baz", 102 ContentType: "quux", 103 ACL: "mooz", 104 Grants: map[string]interface{}{}, 105 } 106 107 actual, err := encoding.Stringify(m) 108 if err != nil { 109 t.Fatal(err) 110 } 111 112 if d := cmp.Diff(actual, expected); d != "" { 113 t.Errorf(d) 114 } 115 }