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

     1  package tests
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ipld/go-ipld-prime/datamodel"
     7  	"github.com/ipld/go-ipld-prime/schema"
     8  )
     9  
    10  func SchemaTestUnionStringprefix(t *testing.T, engine Engine) {
    11  	ts := schema.TypeSystem{}
    12  	ts.Init()
    13  	ts.Accumulate(schema.SpawnString("String"))
    14  	ts.Accumulate(schema.SpawnStruct("SmolStruct",
    15  		[]schema.StructField{
    16  			schema.SpawnStructField("a", "String", false, false),
    17  			schema.SpawnStructField("b", "String", false, false),
    18  		},
    19  		schema.SpawnStructRepresentationStringjoin(":"),
    20  	))
    21  	ts.Accumulate(schema.SpawnUnion("WheeUnion",
    22  		[]schema.TypeName{
    23  			"String",
    24  			"SmolStruct",
    25  		},
    26  		schema.SpawnUnionRepresentationStringprefix(
    27  			":",
    28  			map[string]schema.TypeName{
    29  				"simple":  "String",
    30  				"complex": "SmolStruct",
    31  			},
    32  		),
    33  	))
    34  	engine.Init(t, ts)
    35  
    36  	// These are the same *type-level* as in TestUnionKeyedComplexChildren,
    37  	//  but (of course) have very different representations.
    38  	specs := []testcase{
    39  		{
    40  			name:     "InhabitantA",
    41  			typeJson: `{"String":"whee"}`,
    42  			reprJson: `"simple:whee"`,
    43  			typePoints: []testcasePoint{
    44  				{"", datamodel.Kind_Map},
    45  				{"String", "whee"},
    46  				//{"SmolStruct", datamodel.ErrNotExists{}}, // TODO: need better error typing from traversal package.
    47  			},
    48  			reprPoints: []testcasePoint{
    49  				{"", datamodel.Kind_String},
    50  				{"", "simple:whee"},
    51  			},
    52  		},
    53  		{
    54  			name:     "InhabitantB",
    55  			typeJson: `{"SmolStruct":{"a":"whee","b":"woo"}}`,
    56  			reprJson: `"complex:whee:woo"`,
    57  			typePoints: []testcasePoint{
    58  				{"", datamodel.Kind_Map},
    59  				//{"String", datamodel.ErrNotExists{}}, // TODO: need better error typing from traversal package.
    60  				{"SmolStruct", datamodel.Kind_Map},
    61  				{"SmolStruct/a", "whee"},
    62  				{"SmolStruct/b", "woo"},
    63  			},
    64  			reprPoints: []testcasePoint{
    65  				{"", datamodel.Kind_String},
    66  				{"", "complex:whee:woo"},
    67  			},
    68  		},
    69  	}
    70  
    71  	np := engine.PrototypeByName("WheeUnion")
    72  	nrp := engine.PrototypeByName("WheeUnion.Repr")
    73  	for _, tcase := range specs {
    74  		tcase.Test(t, np, nrp)
    75  	}
    76  }