gen_test.go 1.87 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
	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)
29
		tg.EmitNodeMethodIsUndefined(w)
Eric Myhre's avatar
Eric Myhre committed
30 31 32 33 34 35 36 37 38
		tg.EmitNodeMethodIsNull(w)
		tg.EmitNodeMethodAsBool(w)
		tg.EmitNodeMethodAsInt(w)
		tg.EmitNodeMethodAsFloat(w)
		tg.EmitNodeMethodAsString(w)
		tg.EmitNodeMethodAsBytes(w)
		tg.EmitNodeMethodAsLink(w)
		tg.EmitNodeMethodNodeBuilder(w)
	}
39

40 41
	f := openOrPanic("_test/minima.go")
	emitMinima(f)
42

43
	tString := schema.SpawnString("String")
Eric Myhre's avatar
Eric Myhre committed
44 45
	tStract := schema.SpawnStruct("Stract",
		[]schema.StructField{schema.SpawnStructField(
46
			"aField", tString, false, false,
Eric Myhre's avatar
Eric Myhre committed
47 48 49
		)},
		schema.StructRepresentation_Map{},
	)
Eric Myhre's avatar
Eric Myhre committed
50 51
	tStract2 := schema.SpawnStruct("Stract2",
		[]schema.StructField{schema.SpawnStructField(
52
			"nulble", tString, false, true,
Eric Myhre's avatar
Eric Myhre committed
53 54 55 56 57
		)},
		schema.StructRepresentation_Map{},
	)
	tStract3 := schema.SpawnStruct("Stract3",
		[]schema.StructField{schema.SpawnStructField(
58
			"noptble", tString, true, true,
Eric Myhre's avatar
Eric Myhre committed
59 60 61
		)},
		schema.StructRepresentation_Map{},
	)
Eric Myhre's avatar
Eric Myhre committed
62

63
	f = openOrPanic("_test/tString.go")
64
	emitFileHeader(f)
65
	emitType(NewGeneratorForKindString(tString), f)
Eric Myhre's avatar
Eric Myhre committed
66

Eric Myhre's avatar
Eric Myhre committed
67 68 69 70 71 72 73 74 75 76 77
	f = openOrPanic("_test/Stract.go")
	emitFileHeader(f)
	emitType(NewGeneratorForKindStruct(tStract), f)

	f = openOrPanic("_test/Stract2.go")
	emitFileHeader(f)
	emitType(NewGeneratorForKindStruct(tStract2), f)

	f = openOrPanic("_test/Stract3.go")
	emitFileHeader(f)
	emitType(NewGeneratorForKindStruct(tStract3), f)
Eric Myhre's avatar
Eric Myhre committed
78
}