testStructsContainingMaybe_test.go 14.2 KB
Newer Older
1
package gengo
2 3 4 5 6 7

import (
	"testing"

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

8
	"github.com/ipld/go-ipld-prime"
9 10 11
	"github.com/ipld/go-ipld-prime/schema"
)

12 13 14 15
// TestStructsContainingMaybe checks all the variations of "nullable" and "optional" on struct fields.
// It does this twice: once for the child maybes being implemented with pointers,
// and once with maybes implemented as embeds.
// The child values are scalars.
16
//
17 18 19
// Both type-level generic build and access as well as representation build and access are exercised;
// the representation used is map (the native representation for structs).
func TestStructsContainingMaybe(t *testing.T) {
20 21 22
	// There's a lot of cases to cover so a shorthand labels helper funcs:
	//  - 'v' -- value in that entry
	//  - 'z' -- null in that entry
23
	//  - 'u' -- absent entry
24 25
	build_vvvvv := func(t *testing.T, np ipld.NodePrototype) schema.TypedNode {
		nb := np.NewBuilder()
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
		ma, err := nb.BeginMap(5)
		Require(t, err, ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f1"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("a"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f2"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("b"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f3"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("c"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f4"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("d"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f5"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("e"), ShouldEqual, nil)
		Wish(t, ma.Finish(), ShouldEqual, nil)
		return nb.Build().(schema.TypedNode)
	}
41 42
	build_vvvvv_repr := func(t *testing.T, np ipld.NodePrototype) schema.TypedNode {
		nb := np.NewBuilder()
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
		ma, err := nb.BeginMap(5)
		Require(t, err, ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("r1"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("a"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("r2"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("b"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("r3"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("c"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("r4"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("d"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f5"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("e"), ShouldEqual, nil)
		Wish(t, ma.Finish(), ShouldEqual, nil)
		return nb.Build().(schema.TypedNode)
	}
58 59
	testLookups_vvvvv := func(t *testing.T, n ipld.Node) {
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
60
		Wish(t, n.Length(), ShouldEqual, 5)
61 62 63 64 65
		Wish(t, plzStr(n.LookupByString("f1")), ShouldEqual, "a")
		Wish(t, plzStr(n.LookupByString("f2")), ShouldEqual, "b")
		Wish(t, plzStr(n.LookupByString("f3")), ShouldEqual, "c")
		Wish(t, plzStr(n.LookupByString("f4")), ShouldEqual, "d")
		Wish(t, plzStr(n.LookupByString("f5")), ShouldEqual, "e")
66
	}
67 68 69
	testLookups_vvvvv_repr := func(t *testing.T, n ipld.Node) {
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
		Wish(t, n.Length(), ShouldEqual, 5)
70 71 72 73 74
		Wish(t, plzStr(n.LookupByString("r1")), ShouldEqual, "a")
		Wish(t, plzStr(n.LookupByString("r2")), ShouldEqual, "b")
		Wish(t, plzStr(n.LookupByString("r3")), ShouldEqual, "c")
		Wish(t, plzStr(n.LookupByString("r4")), ShouldEqual, "d")
		Wish(t, plzStr(n.LookupByString("f5")), ShouldEqual, "e")
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
	testIteration_vvvvv := func(t *testing.T, n ipld.Node) {
		itr := n.MapIterator()
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ := itr.Next()
		Wish(t, str(k), ShouldEqual, "f1")
		Wish(t, str(v), ShouldEqual, "a")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
		Wish(t, str(k), ShouldEqual, "f2")
		Wish(t, str(v), ShouldEqual, "b")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
		Wish(t, str(k), ShouldEqual, "f3")
		Wish(t, str(v), ShouldEqual, "c")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
		Wish(t, str(k), ShouldEqual, "f4")
		Wish(t, str(v), ShouldEqual, "d")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
		Wish(t, str(k), ShouldEqual, "f5")
		Wish(t, str(v), ShouldEqual, "e")
		Wish(t, itr.Done(), ShouldEqual, true)
		k, v, err := itr.Next()
		Wish(t, k, ShouldEqual, nil)
		Wish(t, v, ShouldEqual, nil)
		Wish(t, err, ShouldEqual, ipld.ErrIteratorOverread{})
	}
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
	testIteration_vvvvv_repr := func(t *testing.T, n ipld.Node) {
		itr := n.MapIterator()
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ := itr.Next()
		Wish(t, str(k), ShouldEqual, "r1")
		Wish(t, str(v), ShouldEqual, "a")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
		Wish(t, str(k), ShouldEqual, "r2")
		Wish(t, str(v), ShouldEqual, "b")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
		Wish(t, str(k), ShouldEqual, "r3")
		Wish(t, str(v), ShouldEqual, "c")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
		Wish(t, str(k), ShouldEqual, "r4")
		Wish(t, str(v), ShouldEqual, "d")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
		Wish(t, str(k), ShouldEqual, "f5")
		Wish(t, str(v), ShouldEqual, "e")
		Wish(t, itr.Done(), ShouldEqual, true)
		k, v, err := itr.Next()
		Wish(t, k, ShouldEqual, nil)
		Wish(t, v, ShouldEqual, nil)
		Wish(t, err, ShouldEqual, ipld.ErrIteratorOverread{})
	}
132 133
	build_vvzzv := func(t *testing.T, np ipld.NodePrototype) schema.TypedNode {
		nb := np.NewBuilder()
Eric Myhre's avatar
Eric Myhre committed
134 135 136 137 138 139 140 141 142
		ma, err := nb.BeginMap(5)
		Require(t, err, ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f1"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("a"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f2"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("b"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f3"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignNull(), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f4"), ShouldEqual, nil)
143
		Wish(t, ma.AssembleValue().AssignNull(), ShouldEqual, nil)
Eric Myhre's avatar
Eric Myhre committed
144 145 146 147 148
		Wish(t, ma.AssembleKey().AssignString("f5"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("e"), ShouldEqual, nil)
		Wish(t, ma.Finish(), ShouldEqual, nil)
		return nb.Build().(schema.TypedNode)
	}
149 150
	build_vvzzv_repr := func(t *testing.T, np ipld.NodePrototype) schema.TypedNode {
		nb := np.NewBuilder()
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
		ma, err := nb.BeginMap(5)
		Require(t, err, ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("r1"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("a"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("r2"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("b"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("r3"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignNull(), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("r4"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignNull(), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f5"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("e"), ShouldEqual, nil)
		Wish(t, ma.Finish(), ShouldEqual, nil)
		return nb.Build().(schema.TypedNode)
	}
166
	testLookups_vvzzv := func(t *testing.T, n ipld.Node) {
Eric Myhre's avatar
Eric Myhre committed
167
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
168
		Wish(t, n.Length(), ShouldEqual, 5)
169 170 171 172 173
		Wish(t, plzStr(n.LookupByString("f1")), ShouldEqual, "a")
		Wish(t, plzStr(n.LookupByString("f2")), ShouldEqual, "b")
		Wish(t, erp(n.LookupByString("f3")), ShouldEqual, ipld.Null)
		Wish(t, erp(n.LookupByString("f4")), ShouldEqual, ipld.Null)
		Wish(t, plzStr(n.LookupByString("f5")), ShouldEqual, "e")
174
	}
175 176
	build_vuvuv := func(t *testing.T, np ipld.NodePrototype) schema.TypedNode {
		nb := np.NewBuilder()
177 178 179 180 181 182 183 184 185 186 187 188 189 190
		ma, err := nb.BeginMap(3)
		Require(t, err, ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f1"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("a"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f3"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("c"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f5"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("e"), ShouldEqual, nil)
		Wish(t, ma.Finish(), ShouldEqual, nil)
		return nb.Build().(schema.TypedNode)
	}
	testLookups_vuvuv := func(t *testing.T, n ipld.Node) {
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
		Wish(t, n.Length(), ShouldEqual, 5)
191
		Wish(t, plzStr(n.LookupByString("f1")), ShouldEqual, "a")
192
		Wish(t, erp(n.LookupByString("f2")), ShouldEqual, ipld.Absent)
193
		Wish(t, plzStr(n.LookupByString("f3")), ShouldEqual, "c")
194
		Wish(t, erp(n.LookupByString("f4")), ShouldEqual, ipld.Absent)
195
		Wish(t, plzStr(n.LookupByString("f5")), ShouldEqual, "e")
Eric Myhre's avatar
Eric Myhre committed
196
	}
197
	testIteration_vuvuv_repr := func(t *testing.T, n ipld.Node) {
198 199 200
		itr := n.MapIterator()
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ := itr.Next()
201
		Wish(t, str(k), ShouldEqual, "r1")
202 203 204
		Wish(t, str(v), ShouldEqual, "a")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
205
		Wish(t, str(k), ShouldEqual, "r3")
206 207 208 209 210 211 212 213 214 215 216
		Wish(t, str(v), ShouldEqual, "c")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
		Wish(t, str(k), ShouldEqual, "f5")
		Wish(t, str(v), ShouldEqual, "e")
		Wish(t, itr.Done(), ShouldEqual, true)
		k, v, err := itr.Next()
		Wish(t, k, ShouldEqual, nil)
		Wish(t, v, ShouldEqual, nil)
		Wish(t, err, ShouldEqual, ipld.ErrIteratorOverread{})
	}
217 218
	build_vvzuu := func(t *testing.T, np ipld.NodePrototype) schema.TypedNode {
		nb := np.NewBuilder()
219 220 221 222 223 224 225 226 227 228 229
		ma, err := nb.BeginMap(3)
		Require(t, err, ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f1"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("a"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f2"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignString("b"), ShouldEqual, nil)
		Wish(t, ma.AssembleKey().AssignString("f3"), ShouldEqual, nil)
		Wish(t, ma.AssembleValue().AssignNull(), ShouldEqual, nil)
		Wish(t, ma.Finish(), ShouldEqual, nil)
		return nb.Build().(schema.TypedNode)
	}
230
	testIteration_vvzuu_repr := func(t *testing.T, n ipld.Node) {
231 232 233
		itr := n.MapIterator()
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ := itr.Next()
234
		Wish(t, str(k), ShouldEqual, "r1")
235 236 237
		Wish(t, str(v), ShouldEqual, "a")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
238
		Wish(t, str(k), ShouldEqual, "r2")
239 240 241
		Wish(t, str(v), ShouldEqual, "b")
		Wish(t, itr.Done(), ShouldEqual, false)
		k, v, _ = itr.Next()
242
		Wish(t, str(k), ShouldEqual, "r3")
243 244 245 246 247 248 249
		Wish(t, v, ShouldEqual, ipld.Null)
		Wish(t, itr.Done(), ShouldEqual, true)
		k, v, err := itr.Next()
		Wish(t, k, ShouldEqual, nil)
		Wish(t, v, ShouldEqual, nil)
		Wish(t, err, ShouldEqual, ipld.ErrIteratorOverread{})
	}
250

251
	// Okay, now the test actions:
252
	test := func(t *testing.T, np ipld.NodePrototype, nrp ipld.NodePrototype) {
253 254
		t.Run("all fields set", func(t *testing.T) {
			t.Run("typed-create", func(t *testing.T) {
255
				n := build_vvvvv(t, np)
256 257 258
				t.Run("typed-read", func(t *testing.T) {
					testLookups_vvvvv(t, n)
					testIteration_vvvvv(t, n)
259
				})
260 261 262
				t.Run("repr-read", func(t *testing.T) {
					testLookups_vvvvv_repr(t, n.Representation())
					testIteration_vvvvv_repr(t, n.Representation())
Eric Myhre's avatar
Eric Myhre committed
263
				})
264
			})
265
			t.Run("repr-create", func(t *testing.T) {
266
				Wish(t, build_vvvvv_repr(t, nrp), ShouldEqual, build_vvvvv(t, np))
Eric Myhre's avatar
Eric Myhre committed
267
			})
268
		})
269 270
		t.Run("setting nulls", func(t *testing.T) {
			t.Run("typed-create", func(t *testing.T) {
271
				n := build_vvzzv(t, np)
272 273
				t.Run("typed-read", func(t *testing.T) {
					testLookups_vvzzv(t, n)
274
				})
275 276
				t.Run("repr-read", func(t *testing.T) {
					// nyi
277
				})
278 279
			})
			t.Run("repr-create", func(t *testing.T) {
280
				Wish(t, build_vvzzv_repr(t, nrp), ShouldEqual, build_vvzzv(t, np))
281 282
			})
		})
283 284
		t.Run("absent optionals", func(t *testing.T) {
			t.Run("typed-create", func(t *testing.T) {
285
				n := build_vuvuv(t, np)
286 287
				t.Run("typed-read", func(t *testing.T) {
					testLookups_vuvuv(t, n)
288
				})
289 290
				t.Run("repr-read", func(t *testing.T) {
					testIteration_vuvuv_repr(t, n.Representation())
291 292
				})
			})
293 294 295 296 297 298 299
			t.Run("repr-create", func(t *testing.T) {
				// nyi
			})
		})
		t.Run("absent trailing optionals", func(t *testing.T) {
			// Trailing optionals are especially touchy in a few details of iterators, so this gets an extra focused test.
			t.Run("typed-create", func(t *testing.T) {
300
				n := build_vvzuu(t, np)
301 302 303 304 305
				t.Run("typed-read", func(t *testing.T) {
					// Not very interesting; still returns absent explicitly, same as 'vuvuv' scenario.
				})
				t.Run("repr-read", func(t *testing.T) {
					testIteration_vvzuu_repr(t, n.Representation())
Eric Myhre's avatar
Eric Myhre committed
306
				})
307
			})
308 309
			t.Run("repr-create", func(t *testing.T) {
				// nyi
310 311
			})
		})
312
	}
313

314 315 316 317 318 319 320 321 322 323
	// Do most of the type declarations.
	ts := schema.TypeSystem{}
	ts.Init()
	adjCfg := &AdjunctCfg{
		maybeUsesPtr: map[schema.TypeName]bool{},
	}
	ts.Accumulate(schema.SpawnString("String"))
	ts.Accumulate(schema.SpawnStruct("Stroct",
		[]schema.StructField{
			// Every field in this struct (including their order) is exercising an interesting case...
324 325 326 327 328
			schema.SpawnStructField("f1", "String", false, false), // plain field.
			schema.SpawnStructField("f2", "String", true, false),  // optional; later we have more than one optional field, nonsequentially.
			schema.SpawnStructField("f3", "String", false, true),  // nullable; but required.
			schema.SpawnStructField("f4", "String", true, true),   // optional and nullable; trailing optional.
			schema.SpawnStructField("f5", "String", true, false),  // optional; and the second one in a row, trailing.
329 330 331 332 333 334 335 336
		},
		schema.SpawnStructRepresentationMap(map[string]string{
			"f1": "r1",
			"f2": "r2",
			"f3": "r3",
			"f4": "r4",
		}),
	))
337

338 339 340
	// And finally, launch tests! ...while specializing the adjunct config a bit.
	t.Run("maybe-using-embed", func(t *testing.T) {
		adjCfg.maybeUsesPtr["String"] = false
341

342 343
		prefix := "stroct"
		pkgName := "main"
344 345
		genAndCompileAndTest(t, prefix, pkgName, ts, adjCfg, func(t *testing.T, getPrototypeByName func(string) ipld.NodePrototype) {
			test(t, getPrototypeByName("Stroct"), getPrototypeByName("Stroct.Repr"))
346 347 348
		})
	})
	t.Run("maybe-using-ptr", func(t *testing.T) {
349
		adjCfg.maybeUsesPtr["String"] = true
350 351 352

		prefix := "stroct2"
		pkgName := "main"
353 354
		genAndCompileAndTest(t, prefix, pkgName, ts, adjCfg, func(t *testing.T, getPrototypeByName func(string) ipld.NodePrototype) {
			test(t, getPrototypeByName("Stroct"), getPrototypeByName("Stroct.Repr"))
355 356 357
		})
	})
}