Ai
1 Star 0 Fork 0

ks3sdk/aws-sdk-go
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
utilassert.go 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
lijunwei123 提交于 2015-06-16 13:16 +08:00 . change package path
// Package utilassert provides testing assertion generation functions.
package utilassert
import (
"fmt"
"regexp"
"strconv"
"strings"
"testing"
"github.com/ks3sdklib/aws-sdk-go/internal/model/api"
"github.com/ks3sdklib/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)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ks3sdk/aws-sdk-go.git
git@gitee.com:ks3sdk/aws-sdk-go.git
ks3sdk
aws-sdk-go
aws-sdk-go
v1.0.7

搜索帮助