schema.go 1.76 KB
Newer Older
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
package tests

import "testing"

// use a table instead of a map, to get a consistent order

var allSchemaTests = []struct {
	name string
	fn   func(*testing.T, Engine)
}{
	{"ListsContainingMaybe", SchemaTestListsContainingMaybe},
	{"ListsContainingLists", SchemaTestListsContainingLists},
	{"MapsContainingMaybe", SchemaTestMapsContainingMaybe},
	{"MapsContainingMaps", SchemaTestMapsContainingMaps},
	{"MapsWithComplexKeys", SchemaTestMapsWithComplexKeys},
	{"String", SchemaTestString},
	{"RequiredFields", SchemaTestRequiredFields},
	{"StructNesting", SchemaTestStructNesting},
	{"StructReprStringjoin", SchemaTestStructReprStringjoin},
	{"StructReprTuple", SchemaTestStructReprTuple},
	{"StructsContainingMaybe", SchemaTestStructsContainingMaybe},
	{"UnionKeyed", SchemaTestUnionKeyed},
	{"UnionKeyedComplexChildren", SchemaTestUnionKeyedComplexChildren},
	{"UnionKeyedReset", SchemaTestUnionKeyedReset},
	{"UnionKinded", SchemaTestUnionKinded},
	{"UnionStringprefix", SchemaTestUnionStringprefix},
}

type EngineSubtest struct {
	Name   string // subtest name
	Engine Engine
}

func SchemaTestAll(t *testing.T, forTest func(name string) []EngineSubtest) {
	for _, test := range allSchemaTests {
		test := test // do not reuse the range variable
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()

			subtests := forTest(test.name)
			if len(subtests) == 0 {
				t.Skip("no engine provided to SchemaTestAll")
			}
			if len(subtests) == 1 {
				sub := subtests[0]
				if sub.Name != "" {
					t.Fatal("a single engine shouldn't be named")
				}
				test.fn(t, sub.Engine)
				return
			}
			for _, sub := range subtests {
				if sub.Name == "" {
					t.Fatal("multiple engines should be named")
				}
				t.Run(sub.Name, func(t *testing.T) {
					test.fn(t, sub.Engine)
				})
			}
		})
	}
}