github.com/ipld/go-ipld-prime@v0.21.0/node/bindnode/schema_test.go (about)

     1  package bindnode_test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/ipld/go-ipld-prime/datamodel"
     8  	"github.com/ipld/go-ipld-prime/node/bindnode"
     9  	"github.com/ipld/go-ipld-prime/node/tests"
    10  	"github.com/ipld/go-ipld-prime/schema"
    11  )
    12  
    13  // For now, we simply run all schema tests with Prototype.
    14  // In the future, forSchemaTest might return multiple engines.
    15  
    16  func forSchemaTest(name string) []tests.EngineSubtest {
    17  	if name == "Links" {
    18  		// TODO(mvdan): support typed links; see https://github.com/ipld/go-ipld-prime/issues/272
    19  		return nil
    20  	}
    21  	if name == "UnionKeyedComplexChildren" {
    22  		return nil // Specifically, 'InhabitantB/repr-create_with_AK+AV' borks, because it needs representation-level AssignNode to support more.
    23  	}
    24  	return []tests.EngineSubtest{{
    25  		Engine: &bindEngine{},
    26  	}}
    27  }
    28  
    29  func TestSchema(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	tests.SchemaTestAll(t, forSchemaTest)
    33  }
    34  
    35  var _ tests.Engine = (*bindEngine)(nil)
    36  
    37  type bindEngine struct {
    38  	ts schema.TypeSystem
    39  }
    40  
    41  func (e *bindEngine) Init(t *testing.T, ts schema.TypeSystem) {
    42  	e.ts = ts
    43  }
    44  
    45  func (e *bindEngine) PrototypeByName(name string) datamodel.NodePrototype {
    46  	wantRepr := strings.HasSuffix(name, ".Repr")
    47  	if wantRepr {
    48  		name = strings.TrimSuffix(name, ".Repr")
    49  	}
    50  	schemaType := e.ts.TypeByName(name)
    51  	if wantRepr {
    52  		return bindnode.Prototype(nil, schemaType).Representation()
    53  	}
    54  	return bindnode.Prototype(nil, schemaType)
    55  }