github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/kbfsmd/root_metadata_test.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package kbfsmd
     6  
     7  import (
     8  	"reflect"
     9  	"runtime"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  var testMetadataVers = []MetadataVer{
    15  	InitialExtraMetadataVer, SegregatedKeyBundlesVer,
    16  }
    17  
    18  // runTestOverMetadataVers runs the given test function over all
    19  // metadata versions to test. The test is assumed to be parallelizable
    20  // with other instances of itself. Example use:
    21  //
    22  //	func TestFoo(t *testing.T) {
    23  //		runTestOverMetadataVers(t, testFoo)
    24  //	}
    25  //
    26  //	func testFoo(t *testing.T, ver MetadataVer) {
    27  //		...
    28  //		brmd, err := MakeInitialRootMetadata(ver, ...)
    29  //		...
    30  //	}
    31  func runTestOverMetadataVers(
    32  	t *testing.T, f func(t *testing.T, ver MetadataVer)) {
    33  	for _, ver := range testMetadataVers {
    34  		ver := ver // capture range variable.
    35  		t.Run(ver.String(), func(t *testing.T) {
    36  			f(t, ver)
    37  		})
    38  	}
    39  }
    40  
    41  // runTestsOverMetadataVers runs the given list of test functions over
    42  // all metadata versions to test. prefix should be the common prefix
    43  // for all the test function names, and the names of the subtest will
    44  // be taken to be the strings after that prefix. Example use:
    45  //
    46  //	func TestFoo(t *testing.T) {
    47  //		tests := []func(*testing.T, MetadataVer){
    48  //			testFooBar1,
    49  //			testFooBar2,
    50  //			testFooBar3,
    51  //			...
    52  //		}
    53  //		runTestsOverMetadataVers(t, "testFoo", tests)
    54  //	}
    55  func runTestsOverMetadataVers(t *testing.T, prefix string,
    56  	fs []func(t *testing.T, ver MetadataVer)) {
    57  	for _, f := range fs {
    58  		f := f // capture range variable.
    59  		name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
    60  		i := strings.LastIndex(name, prefix)
    61  		if i >= 0 {
    62  			i += len(prefix)
    63  		} else {
    64  			i = 0
    65  		}
    66  		name = name[i:]
    67  		t.Run(name, func(t *testing.T) {
    68  			runTestOverMetadataVers(t, f)
    69  		})
    70  	}
    71  }