| 1 | package gen |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mmcloughlin/avo/internal/api" |
| 5 | "github.com/mmcloughlin/avo/internal/inst" |
| 6 | "github.com/mmcloughlin/avo/internal/prnt" |
| 7 | "github.com/mmcloughlin/avo/printer" |
| 8 | ) |
| 9 | |
| 10 | type buildtest struct { |
| 11 | cfg printer.Config |
| 12 | prnt.Generator |
| 13 | } |
| 14 | |
| 15 | // NewBuildTest autogenerates tests for instruction methods on the build |
| 16 | // context. |
| 17 | func NewBuildTest(cfg printer.Config) Interface { |
| 18 | return GoFmt(&buildtest{cfg: cfg}) |
| 19 | } |
| 20 | |
| 21 | func (b *buildtest) Generate(is []inst.Instruction) ([]byte, error) { |
| 22 | b.Printf("// %s\n\n", b.cfg.GeneratedWarning()) |
| 23 | b.BuildConstraint("!integration") |
| 24 | b.NL() |
| 25 | b.Printf("package build\n\n") |
| 26 | b.Printf("import (\n") |
| 27 | b.Printf("\t\"math\"\n") |
| 28 | b.Printf("\t\"testing\"\n") |
| 29 | b.NL() |
| 30 | b.Printf("\t%q\n", api.ImportPath(api.OperandPackage)) |
| 31 | b.Printf("\t%q\n", api.ImportPath(api.RegisterPackage)) |
| 32 | b.Printf(")\n\n") |
| 33 | |
| 34 | DeclareTestArguments(&b.Generator) |
| 35 | |
| 36 | b.Printf("func TestContextInstructions(t *testing.T) {") |
| 37 | b.Printf("ctx := NewContext()\n") |
| 38 | b.Printf("ctx.Function(\"Instructions\")\n") |
| 39 | |
| 40 | fns := api.InstructionsFunctions(is) |
| 41 | for _, fn := range fns { |
| 42 | f := fn.Forms[0] |
| 43 | s := TestSignature(f) |
| 44 | b.Printf("ctx.%s(%s)\n", fn.Name(), s.Arguments()) |
| 45 | } |
| 46 | |
| 47 | b.Printf("if _, err := ctx.Result(); err != nil { t.Fatal(err) }\n") |
| 48 | b.Printf("}\n\n") |
| 49 | |
| 50 | return b.Result() |
| 51 | } |