gen_test.go 1.46 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1
package gengo
Eric Myhre's avatar
Eric Myhre committed
2 3 4 5 6 7

import (
	"io"
	"os"
	"testing"

8
	"github.com/ipld/go-ipld-prime/schema"
Eric Myhre's avatar
Eric Myhre committed
9 10 11
)

func TestNuevo(t *testing.T) {
12
	os.Mkdir("_test", 0755)
Eric Myhre's avatar
Eric Myhre committed
13 14 15 16 17 18 19
	openOrPanic := func(filename string) *os.File {
		y, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
		if err != nil {
			panic(err)
		}
		return y
	}
20

Eric Myhre's avatar
Eric Myhre committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	emitType := func(tg typeGenerator, w io.Writer) {
		tg.EmitNodeType(w)
		tg.EmitNodeMethodReprKind(w)
		tg.EmitNodeMethodTraverseField(w)
		tg.EmitNodeMethodTraverseIndex(w)
		tg.EmitNodeMethodMapIterator(w)
		tg.EmitNodeMethodListIterator(w)
		tg.EmitNodeMethodLength(w)
		tg.EmitNodeMethodIsNull(w)
		tg.EmitNodeMethodAsBool(w)
		tg.EmitNodeMethodAsInt(w)
		tg.EmitNodeMethodAsFloat(w)
		tg.EmitNodeMethodAsString(w)
		tg.EmitNodeMethodAsBytes(w)
		tg.EmitNodeMethodAsLink(w)
		tg.EmitNodeMethodNodeBuilder(w)
	}
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

	f := openOrPanic("_test/thunks.go")
	emitFileHeader(f)
	doTemplate(`
		type mapIteratorReject struct{ err error }
		type listIteratorReject struct{ err error }

		func (itr mapIteratorReject) Next() (ipld.Node, ipld.Node, error) { return nil, nil, itr.err }
		func (itr mapIteratorReject) Done() bool                          { return false }

		func (itr listIteratorReject) Next() (int, ipld.Node, error) { return -1, nil, itr.err }
		func (itr listIteratorReject) Done() bool                    { return false }
	`, f, nil)

	f = openOrPanic("_test/tStrang.go")
	emitFileHeader(f)
54
	emitType(generateKindString{"Strang", schema.TypeString{}}, f)
Eric Myhre's avatar
Eric Myhre committed
55
}