schema_test.go 1.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package bindnode_test

import (
	"strings"
	"testing"

	"github.com/ipld/go-ipld-prime"
	"github.com/ipld/go-ipld-prime/node/bindnode"
	"github.com/ipld/go-ipld-prime/node/tests"
	"github.com/ipld/go-ipld-prime/schema"
)

13
// For now, we simply run all schema tests with Prototype.
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
// In the future, forSchemaTest might return multiple engines.

func forSchemaTest(name string) []tests.EngineSubtest {
	return []tests.EngineSubtest{{
		Engine: &bindEngine{},
	}}
}

func TestSchema(t *testing.T) {
	t.Parallel()

	tests.SchemaTestAll(t, forSchemaTest)
}

var _ tests.Engine = (*bindEngine)(nil)

type bindEngine struct {
	ts schema.TypeSystem
}

func (e *bindEngine) Init(t *testing.T, ts schema.TypeSystem) {
	e.ts = ts
}

func (e *bindEngine) PrototypeByName(name string) ipld.NodePrototype {
	wantRepr := strings.HasSuffix(name, ".Repr")
	if wantRepr {
		name = strings.TrimSuffix(name, ".Repr")
	}
	schemaType := e.ts.TypeByName(name)
	if wantRepr {
45
		return bindnode.Prototype(nil, schemaType).Representation()
46
	}
47
	return bindnode.Prototype(nil, schemaType)
48
}