map_test.go 4.12 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1 2 3 4 5 6 7 8 9 10 11 12 13 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
package impls

import (
	"testing"

	ipld "github.com/ipld/go-ipld-prime/_rsrch/nodesolution"

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

func TestGennedMapIntValues(t *testing.T) {
	CheckMaps(t, Type__Map_K_T{})
}
func TestGenericMapIntValues(t *testing.T) {
	CheckMaps(t, Style__Map{})
}

func CheckMaps(t *testing.T, ns ipld.NodeStyle) {
	t.Run("map node, str:int, 3 entries", func(t *testing.T) {
		nb := ns.NewBuilder()
		ma, err := nb.BeginMap(3)
		if err != nil {
			panic(err)
		}
		if err := ma.AssembleKey().AssignString("whee"); err != nil {
			panic(err)
		}
		if err := ma.AssembleValue().AssignInt(1); err != nil {
			panic(err)
		}
		if err := ma.AssembleKey().AssignString("woot"); err != nil {
			panic(err)
		}
		if err := ma.AssembleValue().AssignInt(2); err != nil {
			panic(err)
		}
		if err := ma.AssembleKey().AssignString("waga"); err != nil {
			panic(err)
		}
		if err := ma.AssembleValue().AssignInt(3); err != nil {
			panic(err)
		}
		if err := ma.Done(); err != nil {
			panic(err)
		}
		n, err := nb.Build()
		if err != nil {
			wish.Require(t, err, wish.ShouldEqual, nil)
		}
		t.Run("reads back out", func(t *testing.T) {
			wish.Wish(t, n.Length(), wish.ShouldEqual, 3)

			v, err := n.LookupString("whee")
			wish.Wish(t, err, wish.ShouldEqual, nil)
			v2, err := v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 1)

			v, err = n.LookupString("waga")
			wish.Wish(t, err, wish.ShouldEqual, nil)
			v2, err = v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 3)

			v, err = n.LookupString("woot")
			wish.Wish(t, err, wish.ShouldEqual, nil)
			v2, err = v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 2)
		})
		t.Run("reads via iteration", func(t *testing.T) {
			itr := n.MapIterator()

			wish.Wish(t, itr.Done(), wish.ShouldEqual, false)
			k, v, err := itr.Next()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			k2, err := k.AsString()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, k2, wish.ShouldEqual, "whee")
			v2, err := v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 1)

			wish.Wish(t, itr.Done(), wish.ShouldEqual, false)
			k, v, err = itr.Next()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			k2, err = k.AsString()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, k2, wish.ShouldEqual, "woot")
			v2, err = v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 2)

			wish.Wish(t, itr.Done(), wish.ShouldEqual, false)
			k, v, err = itr.Next()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			k2, err = k.AsString()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, k2, wish.ShouldEqual, "waga")
			v2, err = v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 3)

			wish.Wish(t, itr.Done(), wish.ShouldEqual, true)
			k, v, err = itr.Next()
			wish.Wish(t, err, wish.ShouldEqual, ipld.ErrIteratorOverread{})
			wish.Wish(t, k, wish.ShouldEqual, nil)
			wish.Wish(t, v, wish.ShouldEqual, nil)
		})
		t.Run("reads for absent keys error sensibly", func(t *testing.T) {
			v, err := n.LookupString("nope")
			// wish.Wish(t, err, wish.ShouldBeSameTypeAs, ipld.ErrNotExists{}) // TODO depends on a bump of go-wish
			wish.Wish(t, err.Error(), wish.ShouldEqual, `key not found: "nope"`)
			wish.Wish(t, v, wish.ShouldEqual, nil)
		})
	})
	t.Run("repeated key should error", func(t *testing.T) {
		nb := ns.NewBuilder()
		ma, err := nb.BeginMap(3)
		if err != nil {
			panic(err)
		}
		if err := ma.AssembleKey().AssignString("whee"); err != nil {
			panic(err)
		}
		if err := ma.AssembleValue().AssignInt(1); err != nil {
			panic(err)
		}
		if err := ma.AssembleKey().AssignString("whee"); err != nil {
			// wish.Wish(t, err, wish.ShouldBeSameTypeAs, ipld.ErrRepeatedMapKey{}) // TODO depends on a bump of go-wish

			// No string assertion at present -- how that should be presented for typed stuff is unsettled
			//  (and if it's clever, it'll differ from untyped, which will mean no assertion possible!).
		}
	})
	t.Run("builder reset works", func(t *testing.T) {
		// TODO
	})
}