aboutsummaryrefslogtreecommitdiffhomepage
path: root/cgo/cgo_test.go
blob: fad71cf4d233bf2b21a642a252f6a8ec619d21cf (plain)
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
package cgo

import (
	"bytes"
	"go/ast"
	"go/format"
	"go/parser"
	"go/token"
	"io/ioutil"
	"path/filepath"
	"strings"
	"testing"
)

func TestCGo(t *testing.T) {
	var cflags = []string{"--target=armv6m-none-eabi"}

	for _, name := range []string{"basic", "types"} {
		name := name // avoid a race condition
		t.Run(name, func(t *testing.T) {
			t.Parallel()

			// Read the AST in memory.
			path := filepath.Join("testdata", name+".go")
			fset := token.NewFileSet()
			f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
			if err != nil {
				t.Fatal("could not parse Go source file:", err)
			}

			// Process the AST with CGo.
			cgoAST, errs := Process([]*ast.File{f}, "testdata", fset, cflags)
			for _, err := range errs {
				t.Errorf("error during CGo processing: %v", err)
			}

			// Store the (formatted) output in a buffer. Format it, so it
			// becomes easier to read (and will hopefully change less with CGo
			// changes).
			buf := &bytes.Buffer{}
			err = format.Node(buf, fset, cgoAST)
			if err != nil {
				t.Errorf("could not write out CGo AST: %v", err)
			}
			actual := strings.Replace(string(buf.Bytes()), "\r\n", "\n", -1)

			// Read the file with the expected output, to compare against.
			outfile := filepath.Join("testdata", name+".out.go")
			expectedBytes, err := ioutil.ReadFile(outfile)
			if err != nil {
				t.Fatalf("could not read expected output: %v", err)
			}
			expected := strings.Replace(string(expectedBytes), "\r\n", "\n", -1)

			// Check whether the output is as expected.
			if expected != actual {
				// It is not. Test failed.
				t.Errorf("output did not match:\n%s", string(actual))
			}
		})
	}
}