forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilassert.go
76 lines (70 loc) · 2.05 KB
/
utilassert.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
70
71
72
73
74
75
76
// Package utilassert provides testing assertion generation functions.
package utilassert
import (
"fmt"
"regexp"
"strconv"
"strings"
"testing"
"github.com/aws/aws-sdk-go/internal/model/api"
"github.com/aws/aws-sdk-go/internal/util/utilsort"
)
// findMember searches the shape for the member with the matching key name.
func findMember(shape *api.Shape, key string) string {
for actualKey := range shape.MemberRefs {
if strings.ToLower(key) == strings.ToLower(actualKey) {
return actualKey
}
}
return ""
}
// GenerateAssertions builds assertions for a shape based on its type.
//
// The shape's recursive values also will have assertions generated for them.
func GenerateAssertions(out interface{}, shape *api.Shape, prefix string) string {
switch t := out.(type) {
case map[string]interface{}:
keys := utilsort.SortedKeys(t)
code := ""
if shape.Type == "map" {
for _, k := range keys {
v := t[k]
s := shape.ValueRef.Shape
code += GenerateAssertions(v, s, prefix+"[\""+k+"\"]")
}
} else {
for _, k := range keys {
v := t[k]
m := findMember(shape, k)
s := shape.MemberRefs[m].Shape
code += GenerateAssertions(v, s, prefix+"."+m+"")
}
}
return code
case []interface{}:
code := ""
for i, v := range t {
s := shape.MemberRef.Shape
code += GenerateAssertions(v, s, prefix+"["+strconv.Itoa(i)+"]")
}
return code
default:
switch shape.Type {
case "timestamp":
return fmt.Sprintf("assert.Equal(t, time.Unix(%#v, 0).UTC().String(), %s.String())\n", out, prefix)
case "blob":
return fmt.Sprintf("assert.Equal(t, %#v, string(%s))\n", out, prefix)
case "integer", "long":
return fmt.Sprintf("assert.Equal(t, int64(%#v), *%s)\n", out, prefix)
default:
return fmt.Sprintf("assert.Equal(t, %#v, *%s)\n", out, prefix)
}
}
}
// Match is a testing helper to test for testing error by comparing expected
// with a regular expression.
func Match(t *testing.T, regex, expected string) {
if !regexp.MustCompile(regex).Match([]byte(expected)) {
t.Errorf("%q\n\tdoes not match /%s/", expected, regex)
}
}