summaryrefslogtreecommitdiff
path: root/src/pkg/go/printer/printer_test.go
blob: f9019fdacfc3932f6fdfe6863f8164bda2c9ab92 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package printer

import (
	"bytes";
	"flag";
	"io";
	"go/ast";
	"go/parser";
	"os";
	"path";
	"testing";
)


const (
	dataDir = "testdata";
	tabwidth = 4;
)


var update = flag.Bool("update", false, "update golden files");


func lineString(text []byte, i int) string {
	i0 := i;
	for i < len(text) && text[i] != '\n' {
		i++;
	}
	return string(text[i0 : i]);
}


func check(t *testing.T, source, golden string, exports bool) {
	// parse source
	prog, err := parser.ParseFile(source, nil, parser.ParseComments);
	if err != nil {
		t.Error(err);
		return;
	}

	// filter exports if necessary
	if exports {
		ast.FileExports(prog);  // ignore result
		prog.Comments = nil;  // don't print comments that are not in AST
	}

	// format source
	var buf bytes.Buffer;
	if _, err := Fprint(&buf, prog, 0, tabwidth); err != nil {
		t.Error(err);
	}
	res := buf.Bytes();

	// update golden files if necessary
	if *update {
		if err := io.WriteFile(golden, res, 0644); err != nil {
			t.Error(err);
		}
		return;
	}

	// get golden
	gld, err := io.ReadFile(golden);
	if err != nil {
		t.Error(err);
		return;
	}

	// compare lengths
	if len(res) != len(gld) {
		t.Errorf("len = %d, expected %d (= len(%s))", len(res), len(gld), golden);
	}

	// compare contents
	for i, line, offs := 0, 1, 0; i < len(res) && i < len(gld); i++ {
		ch := res[i];
		if ch != gld[i] {
			t.Errorf("%s:%d:%d: %s", source, line, i-offs+1, lineString(res, offs));
			t.Errorf("%s:%d:%d: %s", golden, line, i-offs+1, lineString(gld, offs));
			t.Error();
			return;
		}
		if ch == '\n' {
			line++;
			offs = i+1;
		}
	}
}


type entry struct {
	source, golden string;
	exports bool;
}

// Use gotest -update to create/update the respective golden files.
var data = []entry{
	entry{ "comments.go", "comments.golden", false },
	entry{ "comments.go", "comments.x", true },
	entry{ "linebreaks.go", "linebreaks.golden", false },
	entry{ "expressions.go", "expressions.golden", false },
	entry{ "declarations.go", "declarations.golden", false },
}


func Test(t *testing.T) {
	for _, e := range data {
		source := path.Join(dataDir, e.source);
		golden := path.Join(dataDir, e.golden);
		check(t, source, golden, e.exports);
		// TODO(gri) check that golden is idempotent
		//check(t, golden, golden, e.exports);
	}
}