本文整理汇总了Golang中github.com/davecgh/go-spew/spew.Sprintf函数的典型用法代码示例。如果您正苦于以下问题:Golang Sprintf函数的具体用法?Golang Sprintf怎么用?Golang Sprintf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Sprintf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestHashJoinNilKeyFunc
func TestHashJoinNilKeyFunc(t *testing.T) {
keyFunc := func(val interface{}) interface{} {
return val
}
pairs, left, right := HashJoin(JoinList{10, 11, 12},
JoinList{10, 11, 12}, nil, keyFunc)
if len(left) > 0 {
t.Errorf("Unexpected lefts: %s", left)
}
if len(right) > 0 {
t.Errorf("Unexpected rights: %s", right)
}
if !eq(pairs, []Pair{{10, 10}, {11, 11}, {12, 12}}) {
t.Error(spew.Sprintf("Unexpected pairs: %s", pairs))
}
pairs, left, right = HashJoin(JoinList{10, 11, 12},
JoinList{13, 11, 2}, keyFunc, nil)
if len(left) != 2 {
t.Error(spew.Sprintf("Unexpected left: %s", left))
}
if len(right) != 2 {
t.Error(spew.Sprintf("Unexpected right %s", right))
}
if !eq(pairs, []Pair{{11, 11}}) {
t.Error(spew.Sprintf("Unexpected pairs: %s", pairs))
}
}
示例2: TestJoin
func TestJoin(t *testing.T) {
score := func(left, right interface{}) int {
return left.(int) - right.(int)
}
pairs, left, right := Join([]int{10, 11, 12}, []int{10, 11, 12}, score)
if len(left) > 0 {
t.Errorf("Unexpected lefts: %s", left)
}
if len(right) > 0 {
t.Errorf("Unexpected rights: %s", right)
}
if !eq(pairs, []Pair{{10, 10}, {11, 11}, {12, 12}}) {
t.Error(spew.Sprintf("Unexpected pairs: %s", pairs))
}
pairs, left, right = Join([]int{10, 11, 12}, []int{13, 1, 2}, score)
if !eq(left, []interface{}{12}) {
t.Error(spew.Sprintf("Unexpected left: %s", left))
}
if !eq(right, []interface{}{13}) {
t.Error(spew.Sprintf("Unexpected right %s", right))
}
if !eq(pairs, []Pair{{10, 2}, {11, 1}}) {
t.Error(spew.Sprintf("Unexpected pairs: %s", pairs))
}
}
示例3: valueTest
func valueTest(actual, expected interface{}) {
if expected == nil {
Convey(`... should create no value.`, func() {
So(actual, ShouldBeNil)
})
} else {
Convey(`... should create the right value.`, func() {
So(fmt.Sprintf("%T", actual), ShouldEqual, fmt.Sprintf("%T", expected))
So(spew.Sprintf("%v", actual), ShouldEqual, spew.Sprintf("%v", expected))
})
}
}
示例4: TestGetDirectory
func TestGetDirectory(t *testing.T) {
store := NewMock()
paths := []string{
"/a/b",
"/a/c/d/e",
}
for _, p := range paths {
if err := store.Set(p, p); err != nil {
t.Fatal(err)
}
}
dir, err := getDirectory(store, "/")
if err != nil {
t.Fatal(err)
}
exp := make(directory)
exp["a"] = map[string]string{"b": "/a/b", "c": ""}
if !eq(dir, exp) {
t.Error(spew.Sprintf("\nGet Directory:\n%s\n\nExpected:\n%s\n", dir, exp))
}
dir, err = getDirectory(store, "/a")
if err != nil {
t.Fatal(err)
}
exp = make(directory)
exp["b"] = map[string]string{}
exp["c"] = map[string]string{"d": ""}
if !eq(dir, exp) {
t.Error(spew.Sprintf("\nGet Directory:\n%s\n\nExpected:\n%s\n", dir, exp))
}
dir, err = getDirectory(store, "/a/c")
if err != nil {
t.Fatal(err)
}
exp = make(directory)
exp["d"] = map[string]string{"e": "/a/c/d/e"}
if !eq(dir, exp) {
t.Error(spew.Sprintf("\nGetDirectory:\n%s\n\nExpected:\n%s\n", dir, exp))
}
if val, err := getDirectory(store, "/junk"); err == nil {
t.Error(spew.Sprintf("Expected error, got %s", val))
}
}
示例5: Error
func (v *ValidationError) Error() string {
var s string
switch v.Type {
case ValidationErrorTypeRequired:
s = spew.Sprintf("%s: %s", v.Field, v.Type)
default:
s = spew.Sprintf("%s: %s '%+v'", v.Field, v.Type, v.BadValue)
}
if len(v.Detail) != 0 {
s += fmt.Sprintf(": %s", v.Detail)
}
return s
}
示例6: ErrorBody
// ErrorBody returns the error message without the field name. This is useful
// for building nice-looking higher-level error reporting.
func (v *Error) ErrorBody() string {
var s string
switch v.Type {
case ErrorTypeRequired, ErrorTypeTooLong, ErrorTypeInternal:
s = spew.Sprintf("%s", v.Type)
default:
s = spew.Sprintf("%s '%+v'", v.Type, v.BadValue)
}
if len(v.Detail) != 0 {
s += fmt.Sprintf(", Details: %s", v.Detail)
}
return s
}
示例7: TestSyncDB
func TestSyncDB(t *testing.T) {
spew := spew.NewDefaultConfig()
spew.MaxDepth = 2
checkSyncDB := func(cloudMachines []provider.Machine,
databaseMachines []db.Machine, expectedBoot,
expectedStop []provider.Machine) {
_, bootResult, stopResult := syncDB(cloudMachines, databaseMachines)
if !emptySlices(bootResult, expectedBoot) &&
!reflect.DeepEqual(bootResult, expectedBoot) {
t.Error(spew.Sprintf(
"booted wrong machines. Expected %v, got %v.",
expectedBoot, bootResult))
}
if !emptySlices(stopResult, expectedStop) && !reflect.DeepEqual(
stopResult, expectedStop) {
t.Error(spew.Sprintf(
"stopped wrong machines. Expected %v, got %v.",
expectedStop, stopResult))
}
}
var noMachines []provider.Machine
dbNoSize := db.Machine{Provider: FakeAmazon}
cmNoSize := provider.Machine{Provider: FakeAmazon}
dbLarge := db.Machine{Provider: FakeAmazon, Size: "m4.large"}
cmLarge := provider.Machine{Provider: FakeAmazon, Size: "m4.large"}
// Test boot with no size
checkSyncDB(noMachines, []db.Machine{dbNoSize}, []provider.Machine{cmNoSize},
noMachines)
// Test boot with size
checkSyncDB(noMachines, []db.Machine{dbLarge}, []provider.Machine{cmLarge},
noMachines)
// Test mixed boot
checkSyncDB(noMachines, []db.Machine{dbNoSize, dbLarge}, []provider.Machine{
cmNoSize, cmLarge}, noMachines)
// Test partial boot
checkSyncDB([]provider.Machine{cmNoSize}, []db.Machine{dbNoSize, dbLarge},
[]provider.Machine{cmLarge}, noMachines)
// Test stop
checkSyncDB([]provider.Machine{cmNoSize}, []db.Machine{}, noMachines,
[]provider.Machine{cmNoSize})
// Test partial stop
checkSyncDB([]provider.Machine{cmNoSize, cmLarge}, []db.Machine{}, noMachines,
[]provider.Machine{cmNoSize, cmLarge})
}
示例8: TestInto
func TestInto(t *testing.T) {
cache, err := cachei.Open("redis", cachei.DataSource{})
if err != nil {
t.Fatal(err)
}
for i, tt := range cacheTests {
cacheMiss := false
missFunc := func() (interface{}, error) {
cacheMiss = true
return tt.value, nil
}
verr, cerr := cache.OutSetFn(fmt.Sprintf("__kdarcacheinto_redis_test_%d", i), 1, tt.into, missFunc)
if verr != nil {
t.Fatal(verr)
}
if cerr != nil {
t.Fatal(cerr)
}
if !cacheMiss {
t.Fatalf("%d-1. Expected a cache miss, but instead found cache", i)
}
if !reflect.DeepEqual(tt.value, reflect.ValueOf(tt.into).Elem().Interface()) {
t.Fatalf("%d-1. Expected:\n%s,\ngot:\n%s", i, spew.Sprintf("%#v", &tt.value), spew.Sprintf("%#v", tt.into))
}
cacheMiss = false
verr, cerr = cache.OutSetFn(fmt.Sprintf("__kdarcacheinto_redis_test_%d", i), 1, tt.into, missFunc)
if verr != nil {
t.Fatal(verr)
}
if cerr != nil {
t.Fatal(cerr)
}
if cacheMiss {
t.Fatalf("%d-2. Expected cache, but instead got a cache miss.", i)
}
if !reflect.DeepEqual(tt.value, reflect.ValueOf(tt.into).Elem().Interface()) {
t.Fatalf("%d-2. Expected: %s, got: %s", i, spew.Sprintf("%#v", tt.value), spew.Sprintf("%#v", tt.into))
}
}
}
示例9: testReadContainerTransact
func testReadContainerTransact(t *testing.T, view db.Database) {
minion := view.InsertMinion()
minion.Role = db.Worker
minion.Self = true
view.Commit(minion)
for _, id := range []string{"a", "b"} {
container := view.InsertContainer()
container.DockerID = id
view.Commit(container)
}
container := view.InsertContainer()
container.DockerID = "c"
container.IP = "junk"
view.Commit(container)
dir := directory(map[string]map[string]string{
"a": {"IP": "1.0.0.0", "Labels": `["e"]`},
"b": {"IP": "2.0.0.0", "Labels": `["e", "f"]`},
})
readContainerTransact(view, dir)
ipMap := map[string]string{}
labelMap := map[string][]string{}
for _, c := range view.SelectFromContainer(nil) {
ipMap[c.DockerID] = c.IP
labelMap[c.DockerID] = c.Labels
}
expIPMap := map[string]string{
"a": "1.0.0.0",
"b": "2.0.0.0",
"c": "",
}
if !eq(ipMap, expIPMap) {
t.Error(spew.Sprintf("Found %s, Expected: %s", ipMap, expIPMap))
}
expLabelMap := map[string][]string{
"a": {"e"},
"b": {"e", "f"},
"c": nil,
}
if !eq(labelMap, expLabelMap) {
t.Error(spew.Sprintf("Found %s, Expected: %s", ipMap, expIPMap))
}
}
示例10: TestGet
func TestGet(t *testing.T) {
cache, err := cachei.Open("redis", cachei.DataSource{})
if err != nil {
t.Fatal(err)
}
for i, tt := range cacheTests {
cacheMiss := false
missFunc := func() (interface{}, error) {
cacheMiss = true
return tt.value, nil
}
v1, verr, cerr := cache.GetSetFn(fmt.Sprintf("__kdarcacheget_redis_test_%d", i), 1, missFunc)
if verr != nil {
t.Fatal(verr)
}
if cerr != nil {
t.Fatal(cerr)
}
if !cacheMiss {
t.Fatalf("%d-1. Expected a cache miss, but instead found cache", i)
}
if !reflect.DeepEqual(tt.value, v1) {
t.Fatalf("%d-1. Expected: %s, got: %s", i, spew.Sprintf("%#v", tt.value), spew.Sprintf("%#v", v1))
}
cacheMiss = false
v2, verr, cerr := cache.GetSetFn(fmt.Sprintf("__kdarcacheget_redis_test_%d", i), 1, missFunc)
if verr != nil {
t.Fatal(verr)
}
if cerr != nil {
t.Fatal(cerr)
}
if cacheMiss {
t.Fatalf("%d-2. Expected cache, but instead got a cache miss.", i)
}
if !reflect.DeepEqual(v1, v2) {
t.Fatalf("%d-2. Expected: %s, got: %s", i, spew.Sprintf("%#v", v1), spew.Sprintf("%#v", v2))
}
}
}
示例11: TestSyncLabels
func TestSyncLabels(t *testing.T) {
store := NewMock()
store.Mkdir("/test/a")
store.Mkdir("/test/b")
store.Mkdir("/test/c")
dir, _ := getDirectory(store, "/test")
containers := []db.Container{
{DockerID: "a", Labels: []string{"d", "c"}},
{DockerID: "b", Labels: []string{}},
{DockerID: "c", Labels: nil},
}
syncLabels(store, dir, "/test", containers)
newDir, _ := getDirectory(store, "/test")
if !eq(dir, newDir) {
t.Error(spew.Sprintf("syncLabels did not update dir.\n"+
"Found %s\nExpected %s", dir, newDir))
}
expDir := directory(map[string]map[string]string{
"a": {"Labels": `["c","d"]`},
"b": {"Labels": "[]"},
"c": {"Labels": "[]"},
})
if !eq(dir, expDir) {
t.Error(spew.Sprintf("syncLabels Found %s\nExpected %s", dir, expDir))
}
containers = []db.Container{
{DockerID: "a", Labels: []string{"d", "c"}},
}
syncLabels(store, dir, "/test", containers)
newDir, _ = getDirectory(store, "/test")
if !eq(dir, newDir) {
t.Error(spew.Sprintf("syncLabels did not update dir.\n"+
"Found %s\nExpected %s", dir, newDir))
}
expDir = directory(map[string]map[string]string{
"a": {"Labels": `["c","d"]`},
"b": {"Labels": `[]`},
"c": {"Labels": `[]`},
})
if !eq(dir, expDir) {
t.Error(spew.Sprintf("syncLabels Found %s\nExpected %s", dir, expDir))
}
}
示例12: newVolumeBuilderFromPlugins
func (kl *Kubelet) newVolumeBuilderFromPlugins(spec *api.Volume, podRef *api.ObjectReference) (volume.Builder, error) {
plugin, err := kl.volumePluginMgr.FindPluginBySpec(spec)
if err != nil {
return nil, fmt.Errorf("can't use volume plugins for %s: %v", spew.Sprintf("%#v", *spec), err)
}
if plugin == nil {
// Not found but not an error
return nil, nil
}
builder, err := plugin.NewBuilder(spec, podRef)
if err != nil {
return nil, fmt.Errorf("failed to instantiate volume plugin for %s: %v", spew.Sprintf("%#v", *spec), err)
}
glog.V(3).Infof("Used volume plugin %q for %s", plugin.Name(), spew.Sprintf("%#v", *spec))
return builder, nil
}
示例13: TestKeys
func TestKeys(t *testing.T) {
getGithubKeys = func(username string) ([]string, error) {
return []string{username}, nil
}
checkKeys := func(code, expectedCode string, expected ...string) {
ctx := parseTest(t, code, expectedCode)
machineResult := Stitch{"", ctx}.QueryMachines()
if len(machineResult) == 0 {
t.Error("no machine found")
return
}
if !reflect.DeepEqual(machineResult[0].SSHKeys, expected) {
t.Error(spew.Sprintf("test: %s, result: %s, expected: %s",
code, machineResult[0].SSHKeys, expected))
}
}
code := `(machine (sshkey "key"))`
checkKeys(code, code, "key")
code = `(machine (githubKey "user"))`
checkKeys(code, code, "user")
code = `(machine (githubKey "user") (sshkey "key"))`
checkKeys(code, code, "user", "key")
}
示例14: newVolumeBuilderFromPlugins
func (kl *Qinglet) newVolumeBuilderFromPlugins(spec *volume.Spec, pod *api.Pod, opts volume.VolumeOptions, mounter mount.Interface) (volume.Builder, error) {
plugin, err := kl.volumePluginMgr.FindPluginBySpec(spec)
if err != nil {
return nil, fmt.Errorf("can't use volume plugins for %s: %v", spew.Sprintf("%#v", *spec), err)
}
if plugin == nil {
// Not found but not an error
return nil, nil
}
builder, err := plugin.NewBuilder(spec, pod, opts, mounter)
if err != nil {
return nil, fmt.Errorf("failed to instantiate volume plugin for %s: %v", spew.Sprintf("%#v", *spec), err)
}
glog.V(3).Infof("Used volume plugin %q for %s", plugin.Name(), spew.Sprintf("%#v", *spec))
return builder, nil
}
示例15: dump
// ES: Yes, it isn't great. Whatever. Go fix something else.
func dump(obj interface{}) string {
x, err := json.Marshal(obj)
if err != nil {
return spew.Sprintf("%+v", obj)
}
return string(x)
}