schema_test.go 1.11 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
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"
)

// For now, we simply run all schema tests with PrototypeOnlySchema.
// 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 schemaType == nil {
		return nil
	}
	proto := bindnode.PrototypeOnlySchema(schemaType)
	if wantRepr {
		proto = proto.(bindnode.TypedPrototype).Representation()
	}
	return proto
}