printer: constraints formatting (#209)

Uses `buildtag.Format` to format constraints in the assembly and stub file
printers. This will ensure `// + build` and `//go:build` syntax are used
consistent with the current Go version.

Updates #183
This commit is contained in:
Michael McLoughlin
2021-10-29 01:08:02 -07:00
committed by GitHub
parent 55d98ccf77
commit 79bee1a316
4 changed files with 59 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ import (
"testing"
"github.com/mmcloughlin/avo/build"
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/printer"
)
@@ -26,3 +27,32 @@ func TestStubsPragmas(t *testing.T) {
"",
})
}
func TestStubsConstraints(t *testing.T) {
ctx := build.NewContext()
ctx.ConstraintExpr("linux darwin")
ctx.ConstraintExpr("amd64 arm64 mips64x ppc64x")
expect := []string{
"// Code generated by avo. DO NOT EDIT.",
"",
}
if buildtags.GoBuildSyntaxSupported() {
expect = append(expect,
"//go:build (linux || darwin) && (amd64 || arm64 || mips64x || ppc64x)",
)
}
if buildtags.PlusBuildSyntaxSupported() {
expect = append(expect,
"// +build linux darwin",
"// +build amd64 arm64 mips64x ppc64x",
)
}
expect = append(expect,
"",
"package printer",
"",
)
AssertPrintsLines(t, ctx, printer.NewStubs, expect)
}