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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
package godirwalk
import (
"fmt"
"path/filepath"
"sort"
"strings"
"testing"
)
func ensureError(tb testing.TB, err error, contains ...string) {
tb.Helper()
if len(contains) == 0 || (len(contains) == 1 && contains[0] == "") {
if err != nil {
tb.Fatalf("GOT: %v; WANT: %v", err, contains)
}
} else if err == nil {
tb.Errorf("GOT: %v; WANT: %v", err, contains)
} else {
for _, stub := range contains {
if stub != "" && !strings.Contains(err.Error(), stub) {
tb.Errorf("GOT: %v; WANT: %q", err, stub)
}
}
}
}
func ensureStringSlicesMatch(tb testing.TB, actual, expected []string) {
tb.Helper()
results := make(map[string]int)
for _, s := range actual {
results[s] = -1
}
for _, s := range expected {
results[s]++
}
keys := make([]string, 0, len(results))
for k := range results {
keys = append(keys, k)
}
sort.Strings(keys)
for _, s := range keys {
v, ok := results[s]
if !ok {
panic(fmt.Errorf("cannot find key: %s", s)) // panic because this function is broken
}
switch v {
case -1:
tb.Errorf("GOT: %q (extra)", s)
case 0:
// both slices have this key
case 1:
tb.Errorf("WANT: %q (missing)", s)
default:
panic(fmt.Errorf("key has invalid value: %s: %d", s, v)) // panic because this function is broken
}
}
}
func ensureDirentsMatch(tb testing.TB, actual, expected Dirents) {
tb.Helper()
sort.Sort(actual)
sort.Sort(expected)
al := len(actual)
el := len(expected)
var ai, ei int
for ai < al || ei < el {
if ai == al {
tb.Errorf("GOT: %s %s (extra)", expected[ei].Name(), expected[ei].ModeType())
ei++
} else if ei == el {
tb.Errorf("WANT: %s %s (missing)", actual[ai].Name(), actual[ai].ModeType())
ai++
} else {
epn := filepath.Join(expected[ei].path, expected[ei].Name())
apn := filepath.Join(actual[ai].path, actual[ai].Name())
if apn < epn {
tb.Errorf("GOT: %s %s (extra)", apn, actual[ai].ModeType())
ai++
} else if epn < apn {
tb.Errorf("WANT: %s %s (missing)", epn, expected[ei].ModeType())
ei++
} else {
// names match; check mode types
if got, want := actual[ai].ModeType(), expected[ei].ModeType(); got != want {
tb.Errorf("GOT: %v; WANT: %v", actual[ai].ModeType(), expected[ei].ModeType())
}
ai++
ei++
}
}
}
}
|