| 1 | package buildtags |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestFormat(t *testing.T) { |
| 10 | cases := []struct { |
| 11 | PlusBuild []string |
| 12 | GoBuild string |
| 13 | }{ |
| 14 | { |
| 15 | PlusBuild: []string{"amd64"}, |
| 16 | GoBuild: "amd64", |
| 17 | }, |
| 18 | { |
| 19 | PlusBuild: []string{ |
| 20 | "linux darwin", |
| 21 | "amd64 arm64 mips64x ppc64x", |
| 22 | }, |
| 23 | GoBuild: "(linux || darwin) && (amd64 || arm64 || mips64x || ppc64x)", |
| 24 | }, |
| 25 | { |
| 26 | PlusBuild: []string{"!linux,!darwin !amd64,!arm64,!mips64x,!ppc64x"}, |
| 27 | GoBuild: "(!linux && !darwin) || (!amd64 && !arm64 && !mips64x && !ppc64x)", |
| 28 | }, |
| 29 | { |
| 30 | PlusBuild: []string{ |
| 31 | "linux,386 darwin,!cgo", |
| 32 | "!noasm", |
| 33 | }, |
| 34 | GoBuild: "((linux && 386) || (darwin && !cgo)) && !noasm", |
| 35 | }, |
| 36 | } |
| 37 | |
| 38 | for _, c := range cases { |
| 39 | // Parse constraints. |
| 40 | var cs Constraints |
| 41 | for _, expr := range c.PlusBuild { |
| 42 | constraint, err := ParseConstraint(expr) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | cs = append(cs, constraint) |
| 47 | } |
| 48 | |
| 49 | // Build expected output. |
| 50 | var buf bytes.Buffer |
| 51 | if GoBuildSyntaxSupported() { |
| 52 | fmt.Fprintf(&buf, "//go:build %s\n", c.GoBuild) |
| 53 | } |
| 54 | if PlusBuildSyntaxSupported() { |
| 55 | for _, expr := range c.PlusBuild { |
| 56 | fmt.Fprintf(&buf, "// +build %s\n", expr) |
| 57 | } |
| 58 | } |
| 59 | expect := buf.String() |
| 60 | |
| 61 | // Format and check. |
| 62 | got, err := Format(cs) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | |
| 67 | if got != expect { |
| 68 | t.Errorf("got=\n%s\nexpect=\n%s\n", got, expect) |
| 69 | } |
| 70 | } |
| 71 | } |