bounce.go 2.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
package tests

import (
	"testing"

	"github.com/ipld/go-ipld-prime"
	"github.com/ipld/go-ipld-prime/fluent"

	. "github.com/warpfork/go-wish"
)

type MutableNodeFactory func() ipld.MutableNode

Eric Myhre's avatar
Eric Myhre committed
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
func TestScalars(t *testing.T, newNode MutableNodeFactory) {
	t.Run("null node bounce", func(t *testing.T) {
		n0 := newNode()
		n0.SetNull()
		Wish(t, n0.Kind(), ShouldEqual, ipld.ReprKind_Null)
		Wish(t, n0.IsNull(), ShouldEqual, true)
	})
	t.Run("bool node bounce", func(t *testing.T) {
		n0 := newNode()
		n0.SetBool(true)
		Wish(t, n0.Kind(), ShouldEqual, ipld.ReprKind_Bool)
		Wish(t, n0.IsNull(), ShouldEqual, false)
		Wish(t, fluent.WrapNode(n0).AsBool(), ShouldEqual, true)
	})
	t.Run("int node bounce", func(t *testing.T) {
		n0 := newNode()
		n0.SetInt(17)
		Wish(t, n0.Kind(), ShouldEqual, ipld.ReprKind_Int)
		Wish(t, n0.IsNull(), ShouldEqual, false)
		Wish(t, fluent.WrapNode(n0).AsInt(), ShouldEqual, 17)
	})
	t.Run("float node bounce", func(t *testing.T) {
		n0 := newNode()
		n0.SetFloat(0.122)
		Wish(t, n0.Kind(), ShouldEqual, ipld.ReprKind_Float)
		Wish(t, n0.IsNull(), ShouldEqual, false)
		Wish(t, fluent.WrapNode(n0).AsFloat(), ShouldEqual, 0.122)
	})
42
	t.Run("string node bounce", func(t *testing.T) {
Eric Myhre's avatar
Eric Myhre committed
43 44
		n0 := newNode()
		n0.SetString("asdf")
Eric Myhre's avatar
Eric Myhre committed
45 46
		Wish(t, n0.Kind(), ShouldEqual, ipld.ReprKind_String)
		Wish(t, n0.IsNull(), ShouldEqual, false)
Eric Myhre's avatar
Eric Myhre committed
47
		Wish(t, fluent.WrapNode(n0).AsString(), ShouldEqual, "asdf")
48
	})
Eric Myhre's avatar
Eric Myhre committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	t.Run("bytes node bounce", func(t *testing.T) {
		n0 := newNode()
		n0.SetBytes([]byte{65, 66})
		Wish(t, n0.Kind(), ShouldEqual, ipld.ReprKind_Bytes)
		Wish(t, n0.IsNull(), ShouldEqual, false)
		Wish(t, fluent.WrapNode(n0).AsBytes(), ShouldEqual, []byte{65, 66})
	})
	t.Run("link node bounce", func(t *testing.T) {
		n0 := newNode()
		n0.SetBool(true)
		Wish(t, n0.Kind(), ShouldEqual, ipld.ReprKind_Bool)
		Wish(t, n0.IsNull(), ShouldEqual, false)
		Wish(t, fluent.WrapNode(n0).AsBool(), ShouldEqual, true)
	})
}

func TestRecursives(t *testing.T, newNode MutableNodeFactory) {
Eric Myhre's avatar
Eric Myhre committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	t.Run("short list node bounce", func(t *testing.T) {
		n0 := newNode()
		n00 := newNode()
		n00.SetString("asdf")
		n0.SetIndex(0, n00)
		Wish(t, fluent.WrapNode(n0).TraverseIndex(0).AsString(), ShouldEqual, "asdf")
	})
	t.Run("nested list node bounce", func(t *testing.T) {
		n0 := newNode()
		n00 := newNode()
		n0.SetIndex(0, n00)
		n000 := newNode()
		n000.SetString("asdf")
		n00.SetIndex(0, n000)
		n01 := newNode()
		n01.SetString("quux")
		n0.SetIndex(1, n01)
		nf := fluent.WrapNode(n0)
		Wish(t, nf.Kind(), ShouldEqual, ipld.ReprKind_List)
		Wish(t, nf.TraverseIndex(0).TraverseIndex(0).AsString(), ShouldEqual, "asdf")
		Wish(t, nf.TraverseIndex(1).AsString(), ShouldEqual, "quux")
87
	})
88
}