forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root_metadata_test.go
96 lines (89 loc) · 2.44 KB
/
root_metadata_test.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package kbfsmd
import (
"reflect"
"runtime"
"strings"
"testing"
)
var testMetadataVers = []MetadataVer{
InitialExtraMetadataVer, SegregatedKeyBundlesVer,
}
// runTestOverMetadataVers runs the given test function over all
// metadata versions to test. The test is assumed to be parallelizable
// with other instances of itself. Example use:
//
// func TestFoo(t *testing.T) {
// runTestOverMetadataVers(t, testFoo)
// }
//
// func testFoo(t *testing.T, ver MetadataVer) {
// ...
// brmd, err := MakeInitialRootMetadata(ver, ...)
// ...
// }
func runTestOverMetadataVers(
t *testing.T, f func(t *testing.T, ver MetadataVer)) {
for _, ver := range testMetadataVers {
ver := ver // capture range variable.
t.Run(ver.String(), func(t *testing.T) {
f(t, ver)
})
}
}
// runTestsOverMetadataVers runs the given list of test functions over
// all metadata versions to test. prefix should be the common prefix
// for all the test function names, and the names of the subtest will
// be taken to be the strings after that prefix. Example use:
//
// func TestFoo(t *testing.T) {
// tests := []func(*testing.T, MetadataVer){
// testFooBar1,
// testFooBar2,
// testFooBar3,
// ...
// }
// runTestsOverMetadataVers(t, "testFoo", tests)
// }
func runTestsOverMetadataVers(t *testing.T, prefix string,
fs []func(t *testing.T, ver MetadataVer)) {
for _, f := range fs {
f := f // capture range variable.
name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
i := strings.LastIndex(name, prefix)
if i >= 0 {
i += len(prefix)
} else {
i = 0
}
name = name[i:]
t.Run(name, func(t *testing.T) {
runTestOverMetadataVers(t, f)
})
}
}
// runBenchmarkOverMetadataVers runs the given benchmark function over
// all metadata versions to test. Example use:
//
// func BenchmarkFoo(b *testing.B) {
// runBenchmarkOverMetadataVers(b, testFoo)
// }
//
// func benchmarkFoo(b *testing.B, ver MetadataVer) {
// ...
// brmd, err := MakeInitialRootMetadata(ver, ...)
// ...
// }
func runBenchmarkOverMetadataVers(
b *testing.B, f func(b *testing.B, ver MetadataVer)) {
for _, ver := range testMetadataVers {
ver := ver // capture range variable.
b.Run(ver.String(), func(b *testing.B) {
f(b, ver)
})
}
}
// TODO: Add way to test with all possible (ver, maxVer) combos,
// e.g. for upconversion tests.