github.com/ipld/go-ipld-prime@v0.21.0/adl/rot13adl/rot13node_test.go (about)

     1  package rot13adl
     2  
     3  import (
     4  	"testing"
     5  
     6  	qt "github.com/frankban/quicktest"
     7  
     8  	"github.com/ipld/go-ipld-prime/datamodel"
     9  	"github.com/ipld/go-ipld-prime/node/basicnode"
    10  )
    11  
    12  func TestLogicalNodeRoundtrip(t *testing.T) {
    13  	// Build high level node.
    14  	nb := Prototype.Node.NewBuilder()
    15  	err := nb.AssignString("abcd")
    16  	qt.Assert(t, err, qt.IsNil)
    17  	n := nb.Build()
    18  	// Inspect the high level node.
    19  	s, err := n.AsString()
    20  	qt.Check(t, err, qt.IsNil)
    21  	qt.Check(t, s, qt.Equals, "abcd")
    22  }
    23  
    24  func TestNodeInternal(t *testing.T) {
    25  	// Build high level node.
    26  	nb := Prototype.Node.NewBuilder()
    27  	err := nb.AssignString("abcd")
    28  	qt.Assert(t, err, qt.IsNil)
    29  	n := nb.Build()
    30  	// Poke its insides directly to see that the transformation occured.
    31  	qt.Check(t, n.(*_R13String).synthesized, qt.Equals, "abcd")
    32  	qt.Check(t, n.(*_R13String).raw, qt.Equals, "nopq")
    33  }
    34  
    35  func TestReify(t *testing.T) {
    36  	t.Run("using unspecialized raw node", func(t *testing.T) {
    37  		// Build substrate-shaped data using basicnode.
    38  		sn := basicnode.NewString("nopq")
    39  		// Reify it.
    40  		synth, err := Reify(sn)
    41  		// Inspect the resulting high level node.
    42  		qt.Assert(t, err, qt.IsNil)
    43  		qt.Check(t, synth.Kind(), qt.Equals, datamodel.Kind_String)
    44  		s, err := synth.AsString()
    45  		qt.Check(t, err, qt.IsNil)
    46  		qt.Check(t, s, qt.Equals, "abcd")
    47  	})
    48  	t.Run("using substrate node", func(t *testing.T) {
    49  		// Build substrate-shaped data, in the substrate type right from the start.
    50  		snb := Prototype.SubstrateRoot.NewBuilder()
    51  		snb.AssignString("nopq")
    52  		sn := snb.Build()
    53  		// Reify it.
    54  		synth, err := Reify(sn)
    55  		// Inspect the resulting high level node.
    56  		qt.Assert(t, err, qt.IsNil)
    57  		qt.Check(t, synth.Kind(), qt.Equals, datamodel.Kind_String)
    58  		s, err := synth.AsString()
    59  		qt.Check(t, err, qt.IsNil)
    60  		qt.Check(t, s, qt.Equals, "abcd")
    61  	})
    62  }
    63  
    64  func TestInspectingSubstrate(t *testing.T) {
    65  	// Build high level node.
    66  	nb := Prototype.Node.NewBuilder()
    67  	err := nb.AssignString("abcd")
    68  	qt.Assert(t, err, qt.IsNil)
    69  	n := nb.Build()
    70  	// Ask it about its substrate, and inspect that.
    71  	sn := n.(R13String).Substrate()
    72  	// TODO: It's unfortunate this is only available as a concrete type cast: we should probably make a standard feature detection interface with `Substrate()`.
    73  	//  Is it reasonable to make this part of a standard feature detection pattern,
    74  	//   and make that interface reside in the main IPLD package?  Or in an `adl` package that contains such standard interfaces?
    75  	ss, err := sn.AsString()
    76  	qt.Check(t, err, qt.IsNil)
    77  	qt.Check(t, ss, qt.Equals, "nopq")
    78  }