From 70d9fc2a699624f8fc87f1c5837241c536087373 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Wed, 7 Feb 2018 20:45:42 +0100 Subject: increase test coverage --- shared_test.go | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) (limited to 'shared_test.go') diff --git a/shared_test.go b/shared_test.go index bc67320..04481d4 100644 --- a/shared_test.go +++ b/shared_test.go @@ -19,6 +19,9 @@ package schwift import ( + "crypto/rand" + "encoding/hex" + "math" "os" "testing" @@ -72,3 +75,108 @@ func testWithAccount(t *testing.T, testCode func(a *Account)) { } testCode(account) } + +func testWithContainer(t *testing.T, testCode func(c *Container)) { + testWithAccount(t, func(a *Account) { + containerName := getRandomName() + container, err := a.Container(containerName).EnsureExists() + expectError(t, err, "") + + testCode(container) + + //cleanup + exists, err := container.Exists() + expectError(t, err, "") + if exists { + err = container.Delete(nil, nil) + expectError(t, err, "") + } + }) +} + +//////////////////////////////////////////////////////////////////////////////// + +func getRandomName() string { + var buf [16]byte + _, err := rand.Read(buf[:]) + if err != nil { + panic(err.Error()) + } + return hex.EncodeToString(buf[:]) +} + +//////////////////////////////////////////////////////////////////////////////// + +func expectBool(t *testing.T, actual bool, expected bool) { + t.Helper() + if actual != expected { + t.Errorf("expected value %#v, got %#v instead\n", expected, actual) + } +} + +func expectFloat64(t *testing.T, actual float64, expected float64) { + t.Helper() + if math.Abs((actual-expected)/expected) > 1e-8 { + t.Errorf("expected value %g, got %g instead\n", expected, actual) + } +} + +func expectUint64(t *testing.T, actual uint64, expected uint64) { + t.Helper() + if actual != expected { + t.Errorf("expected value %d, got %d instead\n", expected, actual) + } +} + +func expectString(t *testing.T, actual string, expected string) { + t.Helper() + if actual != expected { + t.Errorf("expected value %q, got %q instead\n", expected, actual) + } +} + +func expectError(t *testing.T, actual error, expected string) (ok bool) { + t.Helper() + if actual == nil { + if expected != "" { + t.Errorf("expected error %q, got no error\n", expected) + return false + } + } else { + if expected == "" { + t.Errorf("expected no error, got %q\n", actual.Error()) + return false + } else if expected != actual.Error() { + t.Errorf("expected error %q, got %q instead\n", expected, actual.Error()) + return false + } + } + + return true +} + +func expectHeaders(t *testing.T, actual map[string]string, expected map[string]string) { + t.Helper() + reported := make(map[string]bool) + + for k, av := range actual { + ev, exists := expected[k] + if !exists { + ev = "" + } + if av != ev { + t.Errorf(`expected "%s: %s", got "%s: %s" instead`, k, ev, k, av) + reported[k] = true + } + } + + for k, ev := range expected { + av, exists := actual[k] + if !exists { + av = "" + } + if av != ev && !reported[k] { + t.Errorf(`expected "%s: %s", got "%s: %s" instead`, k, ev, k, av) + } + } +} -- cgit v1.2.3