summaryrefslogtreecommitdiff
path: root/src/cmd/gofmt/gofmt_test.go
blob: e4d5796f3c8f459ba7ac0594248498783f588895 (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
119
120
121
122
123
124
125
126
127
// Copyright 2011 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 main

import (
	"bytes"
	"io/ioutil"
	"path/filepath"
	"strings"
	"testing"
)

func runTest(t *testing.T, in, out, flags string) {
	// process flags
	*simplifyAST = false
	*rewriteRule = ""
	stdin := false
	for _, flag := range strings.Split(flags, " ") {
		elts := strings.SplitN(flag, "=", 2)
		name := elts[0]
		value := ""
		if len(elts) == 2 {
			value = elts[1]
		}
		switch name {
		case "":
			// no flags
		case "-r":
			*rewriteRule = value
		case "-s":
			*simplifyAST = true
		case "-stdin":
			// fake flag - pretend input is from stdin
			stdin = true
		default:
			t.Errorf("unrecognized flag name: %s", name)
		}
	}

	initParserMode()
	initPrinterMode()
	initRewrite()

	var buf bytes.Buffer
	err := processFile(in, nil, &buf, stdin)
	if err != nil {
		t.Error(err)
		return
	}

	expected, err := ioutil.ReadFile(out)
	if err != nil {
		t.Error(err)
		return
	}

	if got := buf.Bytes(); bytes.Compare(got, expected) != 0 {
		t.Errorf("(gofmt %s) != %s (see %s.gofmt)", in, out, in)
		d, err := diff(expected, got)
		if err == nil {
			t.Errorf("%s", d)
		}
		ioutil.WriteFile(in+".gofmt", got, 0666)
	}
}

var tests = []struct {
	in, flags string
}{
	{"gofmt.go", ""},
	{"gofmt_test.go", ""},
	{"testdata/composites.input", "-s"},
	{"testdata/old.input", ""},
	{"testdata/rewrite1.input", "-r=Foo->Bar"},
	{"testdata/rewrite2.input", "-r=int->bool"},
	{"testdata/rewrite3.input", "-r=x->x"},
	{"testdata/rewrite4.input", "-r=(x)->x"},
	{"testdata/rewrite5.input", "-r=x+x->2*x"},
	{"testdata/stdin*.input", "-stdin"},
	{"testdata/comments.input", ""},
	{"testdata/import.input", ""},
	{"testdata/crlf.input", ""}, // test case for issue 3961; see also TestCRLF
}

func TestRewrite(t *testing.T) {
	for _, test := range tests {
		match, err := filepath.Glob(test.in)
		if err != nil {
			t.Error(err)
			continue
		}
		for _, in := range match {
			out := in
			if strings.HasSuffix(in, ".input") {
				out = in[:len(in)-len(".input")] + ".golden"
			}
			runTest(t, in, out, test.flags)
			if in != out {
				// Check idempotence.
				runTest(t, out, out, test.flags)
			}
		}
	}
}

func TestCRLF(t *testing.T) {
	const input = "testdata/crlf.input"   // must contain CR/LF's
	const golden = "testdata/crlf.golden" // must not contain any CR's

	data, err := ioutil.ReadFile(input)
	if err != nil {
		t.Error(err)
	}
	if bytes.Index(data, []byte("\r\n")) < 0 {
		t.Errorf("%s contains no CR/LF's", input)
	}

	data, err = ioutil.ReadFile(golden)
	if err != nil {
		t.Error(err)
	}
	if bytes.Index(data, []byte("\r")) >= 0 {
		t.Errorf("%s contains CR's", golden)
	}
}