github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/cmd/gofmt/gofmt_test.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"io/ioutil"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func runTest(t *testing.T, in, out, flags string) {
    16  	// process flags
    17  	*simplifyAST = false
    18  	*rewriteRule = ""
    19  	stdin := false
    20  	for _, flag := range strings.Split(flags, " ") {
    21  		elts := strings.SplitN(flag, "=", 2)
    22  		name := elts[0]
    23  		value := ""
    24  		if len(elts) == 2 {
    25  			value = elts[1]
    26  		}
    27  		switch name {
    28  		case "":
    29  			// no flags
    30  		case "-r":
    31  			*rewriteRule = value
    32  		case "-s":
    33  			*simplifyAST = true
    34  		case "-stdin":
    35  			// fake flag - pretend input is from stdin
    36  			stdin = true
    37  		default:
    38  			t.Errorf("unrecognized flag name: %s", name)
    39  		}
    40  	}
    41  
    42  	initParserMode()
    43  	initRewrite()
    44  
    45  	var buf bytes.Buffer
    46  	err := processFile(in, nil, &buf, stdin)
    47  	if err != nil {
    48  		t.Error(err)
    49  		return
    50  	}
    51  
    52  	expected, err := ioutil.ReadFile(out)
    53  	if err != nil {
    54  		t.Error(err)
    55  		return
    56  	}
    57  
    58  	if got := buf.Bytes(); !bytes.Equal(got, expected) {
    59  		t.Errorf("(gofmt %s) != %s (see %s.gofmt)", in, out, in)
    60  		d, err := diff(expected, got)
    61  		if err == nil {
    62  			t.Errorf("%s", d)
    63  		}
    64  		if err := ioutil.WriteFile(in+".gofmt", got, 0666); err != nil {
    65  			t.Error(err)
    66  		}
    67  	}
    68  }
    69  
    70  var tests = []struct {
    71  	in, flags string
    72  }{
    73  	{"gofmt.go", ""},
    74  	{"gofmt_test.go", ""},
    75  	{"testdata/composites.input", "-s"},
    76  	{"testdata/slices1.input", "-s"},
    77  	{"testdata/slices2.input", "-s"},
    78  	{"testdata/old.input", ""},
    79  	{"testdata/rewrite1.input", "-r=Foo->Bar"},
    80  	{"testdata/rewrite2.input", "-r=int->bool"},
    81  	{"testdata/rewrite3.input", "-r=x->x"},
    82  	{"testdata/rewrite4.input", "-r=(x)->x"},
    83  	{"testdata/rewrite5.input", "-r=x+x->2*x"},
    84  	{"testdata/rewrite6.input", "-r=fun(x)->Fun(x)"},
    85  	{"testdata/rewrite7.input", "-r=fun(x...)->Fun(x)"},
    86  	{"testdata/rewrite8.input", "-r=interface{}->int"},
    87  	{"testdata/stdin*.input", "-stdin"},
    88  	{"testdata/comments.input", ""},
    89  	{"testdata/import.input", ""},
    90  	{"testdata/crlf.input", ""},       // test case for issue 3961; see also TestCRLF
    91  	{"testdata/typeswitch.input", ""}, // test case for issue 4470
    92  }
    93  
    94  func TestRewrite(t *testing.T) {
    95  	for _, test := range tests {
    96  		match, err := filepath.Glob(test.in)
    97  		if err != nil {
    98  			t.Error(err)
    99  			continue
   100  		}
   101  		for _, in := range match {
   102  			out := in
   103  			if strings.HasSuffix(in, ".input") {
   104  				out = in[:len(in)-len(".input")] + ".golden"
   105  			}
   106  			runTest(t, in, out, test.flags)
   107  			if in != out {
   108  				// Check idempotence.
   109  				runTest(t, out, out, test.flags)
   110  			}
   111  		}
   112  	}
   113  }
   114  
   115  func TestCRLF(t *testing.T) {
   116  	const input = "testdata/crlf.input"   // must contain CR/LF's
   117  	const golden = "testdata/crlf.golden" // must not contain any CR's
   118  
   119  	data, err := ioutil.ReadFile(input)
   120  	if err != nil {
   121  		t.Error(err)
   122  	}
   123  	if bytes.Index(data, []byte("\r\n")) < 0 {
   124  		t.Errorf("%s contains no CR/LF's", input)
   125  	}
   126  
   127  	data, err = ioutil.ReadFile(golden)
   128  	if err != nil {
   129  		t.Error(err)
   130  	}
   131  	if bytes.Index(data, []byte("\r")) >= 0 {
   132  		t.Errorf("%s contains CR's", golden)
   133  	}
   134  }