github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/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 initPrinterMode() 44 initRewrite() 45 46 var buf bytes.Buffer 47 err := processFile(in, nil, &buf, stdin) 48 if err != nil { 49 t.Error(err) 50 return 51 } 52 53 expected, err := ioutil.ReadFile(out) 54 if err != nil { 55 t.Error(err) 56 return 57 } 58 59 if got := buf.Bytes(); !bytes.Equal(got, expected) { 60 t.Errorf("(gofmt %s) != %s (see %s.gofmt)", in, out, in) 61 d, err := diff(expected, got) 62 if err == nil { 63 t.Errorf("%s", d) 64 } 65 if err := ioutil.WriteFile(in+".gofmt", got, 0666); err != nil { 66 t.Error(err) 67 } 68 } 69 } 70 71 var tests = []struct { 72 in, flags string 73 }{ 74 {"gofmt.go", ""}, 75 {"gofmt_test.go", ""}, 76 {"testdata/composites.input", "-s"}, 77 {"testdata/slices1.input", "-s"}, 78 {"testdata/slices2.input", "-s"}, 79 {"testdata/old.input", ""}, 80 {"testdata/rewrite1.input", "-r=Foo->Bar"}, 81 {"testdata/rewrite2.input", "-r=int->bool"}, 82 {"testdata/rewrite3.input", "-r=x->x"}, 83 {"testdata/rewrite4.input", "-r=(x)->x"}, 84 {"testdata/rewrite5.input", "-r=x+x->2*x"}, 85 {"testdata/rewrite6.input", "-r=fun(x)->Fun(x)"}, 86 {"testdata/rewrite7.input", "-r=fun(x...)->Fun(x)"}, 87 {"testdata/rewrite8.input", "-r=interface{}->int"}, 88 {"testdata/stdin*.input", "-stdin"}, 89 {"testdata/comments.input", ""}, 90 {"testdata/import.input", ""}, 91 {"testdata/crlf.input", ""}, // test case for issue 3961; see also TestCRLF 92 {"testdata/typeswitch.input", ""}, // test case for issue 4470 93 } 94 95 func TestRewrite(t *testing.T) { 96 for _, test := range tests { 97 match, err := filepath.Glob(test.in) 98 if err != nil { 99 t.Error(err) 100 continue 101 } 102 for _, in := range match { 103 out := in 104 if strings.HasSuffix(in, ".input") { 105 out = in[:len(in)-len(".input")] + ".golden" 106 } 107 runTest(t, in, out, test.flags) 108 if in != out { 109 // Check idempotence. 110 runTest(t, out, out, test.flags) 111 } 112 } 113 } 114 } 115 116 func TestCRLF(t *testing.T) { 117 const input = "testdata/crlf.input" // must contain CR/LF's 118 const golden = "testdata/crlf.golden" // must not contain any CR's 119 120 data, err := ioutil.ReadFile(input) 121 if err != nil { 122 t.Error(err) 123 } 124 if bytes.Index(data, []byte("\r\n")) < 0 { 125 t.Errorf("%s contains no CR/LF's", input) 126 } 127 128 data, err = ioutil.ReadFile(golden) 129 if err != nil { 130 t.Error(err) 131 } 132 if bytes.Index(data, []byte("\r")) >= 0 { 133 t.Errorf("%s contains CR's", golden) 134 } 135 }