File: syntax_test.go

package info (click to toggle)
golang-github-mmcloughlin-avo 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,024 kB
  • sloc: xml: 71,029; asm: 14,862; sh: 194; makefile: 21; ansic: 11
file content (71 lines) | stat: -rw-r--r-- 1,389 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package buildtags

import (
	"bytes"
	"fmt"
	"testing"
)

func TestFormat(t *testing.T) {
	cases := []struct {
		PlusBuild []string
		GoBuild   string
	}{
		{
			PlusBuild: []string{"amd64"},
			GoBuild:   "amd64",
		},
		{
			PlusBuild: []string{
				"linux darwin",
				"amd64 arm64 mips64x ppc64x",
			},
			GoBuild: "(linux || darwin) && (amd64 || arm64 || mips64x || ppc64x)",
		},
		{
			PlusBuild: []string{"!linux,!darwin !amd64,!arm64,!mips64x,!ppc64x"},
			GoBuild:   "(!linux && !darwin) || (!amd64 && !arm64 && !mips64x && !ppc64x)",
		},
		{
			PlusBuild: []string{
				"linux,386 darwin,!cgo",
				"!noasm",
			},
			GoBuild: "((linux && 386) || (darwin && !cgo)) && !noasm",
		},
	}

	for _, c := range cases {
		// Parse constraints.
		var cs Constraints
		for _, expr := range c.PlusBuild {
			constraint, err := ParseConstraint(expr)
			if err != nil {
				t.Fatal(err)
			}
			cs = append(cs, constraint)
		}

		// Build expected output.
		var buf bytes.Buffer
		if GoBuildSyntaxSupported() {
			fmt.Fprintf(&buf, "//go:build %s\n", c.GoBuild)
		}
		if PlusBuildSyntaxSupported() {
			for _, expr := range c.PlusBuild {
				fmt.Fprintf(&buf, "// +build %s\n", expr)
			}
		}
		expect := buf.String()

		// Format and check.
		got, err := Format(cs)
		if err != nil {
			t.Fatal(err)
		}

		if got != expect {
			t.Errorf("got=\n%s\nexpect=\n%s\n", got, expect)
		}
	}
}