github.com/ipld/go-ipld-prime@v0.21.0/node/tests/schema.go (about)

     1  package tests
     2  
     3  import "testing"
     4  
     5  // use a table instead of a map, to get a consistent order
     6  
     7  var allSchemaTests = []struct {
     8  	name string
     9  	fn   func(*testing.T, Engine)
    10  }{
    11  	{"Links", SchemaTestLinks},
    12  	{"ListsContainingMaybe", SchemaTestListsContainingMaybe},
    13  	{"ListsContainingLists", SchemaTestListsContainingLists},
    14  	{"MapsContainingMaybe", SchemaTestMapsContainingMaybe},
    15  	{"MapsContainingMaps", SchemaTestMapsContainingMaps},
    16  	{"MapsWithComplexKeys", SchemaTestMapsWithComplexKeys},
    17  	{"Scalars", SchemaTestScalars},
    18  	{"RequiredFields", SchemaTestRequiredFields},
    19  	{"StructNesting", SchemaTestStructNesting},
    20  	{"StructReprStringjoin", SchemaTestStructReprStringjoin},
    21  	{"StructReprTuple", SchemaTestStructReprTuple},
    22  	{"StructReprListPairs", SchemaTestStructReprListPairs},
    23  	{"StructsContainingMaybe", SchemaTestStructsContainingMaybe},
    24  	{"UnionKeyed", SchemaTestUnionKeyed},
    25  	{"UnionKeyedComplexChildren", SchemaTestUnionKeyedComplexChildren},
    26  	{"UnionKeyedReset", SchemaTestUnionKeyedReset},
    27  	{"UnionKinded", SchemaTestUnionKinded},
    28  	{"UnionStringprefix", SchemaTestUnionStringprefix},
    29  }
    30  
    31  type EngineSubtest struct {
    32  	Name   string // subtest name
    33  	Engine Engine
    34  }
    35  
    36  func SchemaTestAll(t *testing.T, forTest func(name string) []EngineSubtest) {
    37  	for _, test := range allSchemaTests {
    38  		test := test // do not reuse the range variable
    39  		t.Run(test.name, func(t *testing.T) {
    40  			t.Parallel()
    41  
    42  			subtests := forTest(test.name)
    43  			if len(subtests) == 0 {
    44  				t.Skip("no engine provided to SchemaTestAll")
    45  			}
    46  			if len(subtests) == 1 {
    47  				sub := subtests[0]
    48  				if sub.Name != "" {
    49  					t.Fatal("a single engine shouldn't be named")
    50  				}
    51  				test.fn(t, sub.Engine)
    52  				return
    53  			}
    54  			for _, sub := range subtests {
    55  				if sub.Name == "" {
    56  					t.Fatal("multiple engines should be named")
    57  				}
    58  				t.Run(sub.Name, func(t *testing.T) {
    59  					test.fn(t, sub.Engine)
    60  				})
    61  			}
    62  		})
    63  	}
    64  }