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

     1  package registry_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"math"
     7  	"testing"
     8  
     9  	"github.com/ipld/go-ipld-prime/codec/dagcbor"
    10  	"github.com/ipld/go-ipld-prime/codec/dagjson"
    11  	"github.com/ipld/go-ipld-prime/node/basicnode"
    12  	"github.com/ipld/go-ipld-prime/node/bindnode"
    13  	"github.com/ipld/go-ipld-prime/node/bindnode/registry"
    14  
    15  	qt "github.com/frankban/quicktest"
    16  )
    17  
    18  type HexString string
    19  type Foo struct {
    20  	Int  int
    21  	Bool bool
    22  }
    23  
    24  func TestRegistry(t *testing.T) {
    25  	reg := registry.NewRegistry()
    26  	qt.Assert(t, reg.IsRegistered((*Foo)(nil)), qt.IsFalse)
    27  	qt.Assert(t, reg.IsRegistered((*HexString)(nil)), qt.IsFalse)
    28  
    29  	err := reg.RegisterType((*Foo)(nil),
    30  		`type Foo struct {
    31  			Int Int
    32  			Bool Bool
    33  		}`, "Foo")
    34  	qt.Assert(t, err, qt.IsNil)
    35  
    36  	err = reg.RegisterType((*HexString)(nil), "type HS bytes", "HS", bindnode.TypedBytesConverter(
    37  		(*HexString)(nil),
    38  		func(b []byte) (interface{}, error) {
    39  			return HexString(hex.EncodeToString(b)), nil
    40  		},
    41  		func(i interface{}) ([]byte, error) {
    42  			s, _ := i.(*HexString)
    43  			return hex.DecodeString(string(*s))
    44  		}))
    45  	qt.Assert(t, err, qt.IsNil)
    46  
    47  	qt.Assert(t, reg.IsRegistered((*Foo)(nil)), qt.IsTrue)
    48  	qt.Assert(t, reg.IsRegistered((*HexString)(nil)), qt.IsTrue)
    49  
    50  	hsi, err := reg.TypeFromNode(basicnode.NewBytes([]byte{0, 1, 2, 3, 4}), (*HexString)(nil))
    51  	qt.Assert(t, err, qt.IsNil)
    52  	hs, ok := hsi.(*HexString)
    53  	qt.Assert(t, ok, qt.IsTrue)
    54  	qt.Assert(t, string(*hs), qt.Equals, "0001020304")
    55  
    56  	byts, _ := hex.DecodeString("a263496e74386364426f6f6cf4")
    57  	fooi, err := reg.TypeFromBytes(byts, (*Foo)(nil), dagcbor.Decode)
    58  	qt.Assert(t, err, qt.IsNil)
    59  	foo, ok := fooi.(*Foo)
    60  	qt.Assert(t, ok, qt.IsTrue)
    61  	qt.Assert(t, *foo, qt.Equals, Foo{Int: -100, Bool: false})
    62  
    63  	byts, err = reg.TypeToBytes(&Foo{Int: -100, Bool: false}, dagjson.Encode)
    64  	qt.Assert(t, err, qt.IsNil)
    65  	qt.Assert(t, string(byts), qt.Equals, `{"Bool":false,"Int":-100}`)
    66  
    67  	byts, _ = hex.DecodeString("a263496e741a7fffffff64426f6f6cf5")
    68  	fooi, err = reg.TypeFromReader(bytes.NewReader(byts), (*Foo)(nil), dagcbor.Decode)
    69  	qt.Assert(t, err, qt.IsNil)
    70  	foo, ok = fooi.(*Foo)
    71  	qt.Assert(t, ok, qt.IsTrue)
    72  	qt.Assert(t, *foo, qt.Equals, Foo{Int: math.MaxInt32, Bool: true})
    73  
    74  	w := bytes.Buffer{}
    75  	err = reg.TypeToWriter(&Foo{Int: math.MaxInt32, Bool: true}, &w, dagjson.Encode)
    76  	qt.Assert(t, err, qt.IsNil)
    77  	qt.Assert(t, w.String(), qt.Equals, `{"Bool":true,"Int":2147483647}`)
    78  }
    79  
    80  func TestRegistryErrors(t *testing.T) {
    81  	reg := registry.NewRegistry()
    82  	err := reg.RegisterType((*Foo)(nil), `type Nope nope {}`, "Foo")
    83  	qt.Assert(t, err, qt.ErrorMatches, `.*unknown type keyword: "nope".*`)
    84  
    85  	err = reg.RegisterType((*HexString)(nil), "type HS string", "HS")
    86  	qt.Assert(t, err, qt.IsNil)
    87  
    88  	err = reg.RegisterType((*HexString)(nil), "type HS2 string", "HS2")
    89  	qt.Assert(t, err, qt.ErrorMatches, `.*type already registered: HexString`)
    90  
    91  	err = reg.RegisterType((*Foo)(nil), "type NotFoo string", "Foo")
    92  	qt.Assert(t, err, qt.ErrorMatches, `.*does not contain that named type.*`)
    93  
    94  	err = reg.RegisterType((*Foo)(nil),
    95  		`type Foo struct {
    96  			NotInt String
    97  			NotBool Float
    98  		}`, "Foo")
    99  	qt.Assert(t, err, qt.ErrorMatches, `.*kind mismatch.*`)
   100  }