]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gofmt: make test files self-describing
authorRobert Griesemer <gri@golang.org>
Fri, 22 Aug 2014 00:25:13 +0000 (17:25 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 22 Aug 2014 00:25:13 +0000 (17:25 -0700)
1) Interpret a comment of the form

//gofmt <flags>

in test files to drive the respective
gofmt command. Eliminates the need to
enumerate all test files in the test
harness.

2) Added -update flag to make it easier
to update test cases.

LGTM=josharian
R=golang-codereviews, josharian
CC=golang-codereviews
https://golang.org/cl/130440043

37 files changed:
src/cmd/gofmt/gofmt_test.go
src/cmd/gofmt/testdata/composites.golden
src/cmd/gofmt/testdata/composites.input
src/cmd/gofmt/testdata/crlf.golden
src/cmd/gofmt/testdata/crlf.input
src/cmd/gofmt/testdata/emptydecl.golden
src/cmd/gofmt/testdata/emptydecl.input
src/cmd/gofmt/testdata/ranges.golden
src/cmd/gofmt/testdata/ranges.input
src/cmd/gofmt/testdata/rewrite1.golden
src/cmd/gofmt/testdata/rewrite1.input
src/cmd/gofmt/testdata/rewrite2.golden
src/cmd/gofmt/testdata/rewrite2.input
src/cmd/gofmt/testdata/rewrite3.golden
src/cmd/gofmt/testdata/rewrite3.input
src/cmd/gofmt/testdata/rewrite4.golden
src/cmd/gofmt/testdata/rewrite4.input
src/cmd/gofmt/testdata/rewrite5.golden
src/cmd/gofmt/testdata/rewrite5.input
src/cmd/gofmt/testdata/rewrite6.golden
src/cmd/gofmt/testdata/rewrite6.input
src/cmd/gofmt/testdata/rewrite7.golden
src/cmd/gofmt/testdata/rewrite7.input
src/cmd/gofmt/testdata/rewrite8.golden
src/cmd/gofmt/testdata/rewrite8.input
src/cmd/gofmt/testdata/slices1.golden
src/cmd/gofmt/testdata/slices1.input
src/cmd/gofmt/testdata/slices2.golden
src/cmd/gofmt/testdata/slices2.input
src/cmd/gofmt/testdata/stdin1.golden
src/cmd/gofmt/testdata/stdin1.input
src/cmd/gofmt/testdata/stdin2.golden
src/cmd/gofmt/testdata/stdin2.input
src/cmd/gofmt/testdata/stdin3.golden
src/cmd/gofmt/testdata/stdin3.input
src/cmd/gofmt/testdata/stdin4.golden
src/cmd/gofmt/testdata/stdin4.input

index ca44f3dcf7a6c361c82807c00682ba92df3f4173..d1edb7bcc16c25fba5cdaa68b287838bd451cfac 100644 (file)
@@ -6,18 +6,60 @@ package main
 
 import (
        "bytes"
+       "flag"
        "io/ioutil"
+       "os"
        "path/filepath"
        "strings"
        "testing"
+       "text/scanner"
 )
 
-func runTest(t *testing.T, in, out, flags string) {
+var update = flag.Bool("update", false, "update .golden files")
+
+// gofmtFlags looks for a comment of the form
+//
+//     //gofmt flags
+//
+// within the first maxLines lines of the given file,
+// and returns the flags string, if any. Otherwise it
+// returns the empty string.
+func gofmtFlags(filename string, maxLines int) string {
+       f, err := os.Open(filename)
+       if err != nil {
+               return "" // ignore errors - they will be found later
+       }
+       defer f.Close()
+
+       // initialize scanner
+       var s scanner.Scanner
+       s.Init(f)
+       s.Error = func(*scanner.Scanner, string) {}       // ignore errors
+       s.Mode = scanner.GoTokens &^ scanner.SkipComments // want comments
+
+       // look for //gofmt comment
+       for s.Line <= maxLines {
+               switch s.Scan() {
+               case scanner.Comment:
+                       const prefix = "//gofmt "
+                       if t := s.TokenText(); strings.HasPrefix(t, prefix) {
+                               return strings.TrimSpace(t[len(prefix):])
+                       }
+               case scanner.EOF:
+                       return ""
+               }
+
+       }
+
+       return ""
+}
+
+func runTest(t *testing.T, in, out string) {
        // process flags
        *simplifyAST = false
        *rewriteRule = ""
        stdin := false
-       for _, flag := range strings.Split(flags, " ") {
+       for _, flag := range strings.Split(gofmtFlags(in, 20), " ") {
                elts := strings.SplitN(flag, "=", 2)
                name := elts[0]
                value := ""
@@ -56,6 +98,17 @@ func runTest(t *testing.T, in, out, flags string) {
        }
 
        if got := buf.Bytes(); !bytes.Equal(got, expected) {
+               if *update {
+                       if in != out {
+                               if err := ioutil.WriteFile(out, got, 0666); err != nil {
+                                       t.Error(err)
+                               }
+                               return
+                       }
+                       // in == out: don't accidentally destroy input
+                       t.Errorf("WARNING: -update did not rewrite input file %s", in)
+               }
+
                t.Errorf("(gofmt %s) != %s (see %s.gofmt)", in, out, in)
                d, err := diff(expected, got)
                if err == nil {
@@ -67,53 +120,37 @@ func runTest(t *testing.T, in, out, flags string) {
        }
 }
 
-var tests = []struct {
-       in, flags string
-}{
-       {"gofmt.go", ""},
-       {"gofmt_test.go", ""},
-       {"testdata/composites.input", "-s"},
-       {"testdata/slices1.input", "-s"},
-       {"testdata/slices2.input", "-s"},
-       {"testdata/ranges.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/rewrite6.input", "-r=fun(x)->Fun(x)"},
-       {"testdata/rewrite7.input", "-r=fun(x...)->Fun(x)"},
-       {"testdata/rewrite8.input", "-r=interface{}->int"},
-       {"testdata/stdin*.input", "-stdin"},
-       {"testdata/comments.input", ""},
-       {"testdata/import.input", ""},
-       {"testdata/crlf.input", ""},        // test case for issue 3961; see also TestCRLF
-       {"testdata/typeswitch.input", ""},  // test case for issue 4470
-       {"testdata/emptydecl.input", "-s"}, // test case for issue 7631
-}
-
+// TestRewrite processes testdata/*.input files and compares them to the
+// corresponding testdata/*.golden files. The gofmt flags used to process
+// a file must be provided via a comment of the form
+//
+//     //gofmt flags
+//
+// in the processed file within the first 20 lines, if any.
 func TestRewrite(t *testing.T) {
-       for _, test := range tests {
-               match, err := filepath.Glob(test.in)
-               if err != nil {
-                       t.Error(err)
-                       continue
+       // determine input files
+       match, err := filepath.Glob("testdata/*.input")
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       // add larger examples
+       match = append(match, "gofmt.go", "gofmt_test.go")
+
+       for _, in := range match {
+               out := in // for files where input and output are identical
+               if strings.HasSuffix(in, ".input") {
+                       out = in[:len(in)-len(".input")] + ".golden"
                }
-               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)
-                       }
+               runTest(t, in, out)
+               if in != out {
+                       // Check idempotence.
+                       runTest(t, out, out)
                }
        }
 }
 
+// Test case for issue 3961.
 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
index b2825e732aa516d1612e6d95270206c79c40f8bd..fc9c98e625b4b869250a464fc5fd6b08fc42b613 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -s
+
 package P
 
 type T struct {
index 7210dafc96c1820371efffa9c2e3e5d6e3d6330b..fc7598af99edf1fc729f0e51f459ceda11ca73c0 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -s
+
 package P
 
 type T struct {
index 57679f770fe2b3b2f9415c043efe4c281737c377..193dbacc727dd786f3ac6604c8d1f0f55ab26376 100644 (file)
@@ -2,6 +2,7 @@
        Source containing CR/LF line endings.
        The gofmt'ed output must only have LF
        line endings.
+       Test case for issue 3961.
 */
 package main
 
index 61a1aa0b4ee7e1a0e32dd98cc9893e099e896abe..ae7e14dbf1386c7133cc0863acf425ad85f51f63 100644 (file)
@@ -2,6 +2,7 @@
        Source containing CR/LF line endings.\r
        The gofmt'ed output must only have LF\r
        line endings.\r
+       Test case for issue 3961.\r
 */\r
 package main\r
 \r
index 9fe62c9738a148385d1a81e0b7ada9646c55018a..33d6435e0a99dc63d16abb9cf6edaf9799b2de23 100644 (file)
@@ -1,3 +1,7 @@
+//gofmt -s
+
+// Test case for issue 7631.
+
 package main
 
 // Keep this declaration
index d1cab00ef7e75790f8c5ee657be1f015972d4c85..4948a61f0de2285960ebb54ed0392c474f69d503 100644 (file)
@@ -1,3 +1,7 @@
+//gofmt -s
+
+// Test case for issue 7631.
+
 package main
 
 // Keep this declaration
index 42168526d1df72f40beebd4186596592e90633c5..506b3a035a3684b783b29d5113087f42fbfaf87e 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -s
+
 // Test cases for range simplification.
 package p
 
index 4b02d517520f8c84169475b0a71841fcc0a6e603..df5f8333c21c91af3de98d58fe2aa52582491ce4 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -s
+
 // Test cases for range simplification.
 package p
 
index d9beb370582c9eb7a392447312f5873886183814..3ee5373a79094a6b05328b2114d75946cb4c4575 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=Foo->Bar
+
 // 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.
index bdb894320d37f26417707e82873d94b0b859a34a..a84c8f781659ba5ebd9ae62ac351d680167551cd 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=Foo->Bar
+
 // 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.
index 64c67ffa67b460e05e8ba31014fdf26df79613d9..f980e035309e1a6958b6bb02458ee055dac98431 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=int->bool
+
 // 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.
index 21171447a10fd7ccb4c0710cf789d665de0b33bb..489be4e07dc80facb5a7ee72a6b8f5c7ba2cf18e 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=int->bool
+
 // 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.
index 0d16d16011be2687128966552db1eb1f7446d46b..261a220c65d7f1988a2ae55fa31af59b32bb6dec 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=x->x
+
 // 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.
index 0d16d16011be2687128966552db1eb1f7446d46b..261a220c65d7f1988a2ae55fa31af59b32bb6dec 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=x->x
+
 // 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.
index 8dfc81a0746bdc0aa7a5ddd55cc3207158b92ed5..b05547b4bf08216357c7f7bfc2622916aa976165 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=(x)->x
+
 // Copyright 2012 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.
index 164cc0451f3874563b48adc53f960699d1680468..0817099209c0e87ce5c422bd7750c0d01a38b839 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=(x)->x
+
 // Copyright 2012 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.
index 5a448a63d37e1205b51b4a1a9190cc33a24a440f..9beb34aee76d13da7fafce9f32006f4c16b9092a 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=x+x->2*x
+
 // 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.
index 0d759e69b6db29e8cdf7f395ebe3f6a78bf90fd3..d7a6122d07a7a99c20e3ce7e0f57d27b054d2bcc 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=x+x->2*x
+
 // 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.
index e565dbdd97b57feff54c1b6c067970a6bd4ad46c..48ec9aa0df7780e1dea1567071240fee46d1b2f9 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=fun(x)->Fun(x)
+
 // Copyright 2013 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.
index 8c088b3e87873c6afd613f99d7d4cd4d792cdf95..b085a84fef4a878b41bdd90716dce23acb90ecb9 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=fun(x)->Fun(x)
+
 // Copyright 2013 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.
index 29babad9f94124a6498928cb452affc188de52c0..8386a0b2a3eb791bd1bb5a68d4277ea75b660441 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=fun(x...)->Fun(x)
+
 // Copyright 2013 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.
index 073e2a3e6f8ac57343328e3ac720f07e8d01c96e..c1984708e71b34b96a1e78292ba8cca8ce1280f5 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=fun(x...)->Fun(x)
+
 // Copyright 2013 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.
index cfc452b031007ed2e47f86d459b6a50aa2242a57..62f0419dfb460297d64aef8850d228c507f411f9 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=interface{}->int
+
 // Copyright 2013 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.
index 235efa91cc69a8a920f13b99966fe8dfc0b1f0ba..7964c5c75c78fb0551487b8ae637353081990851 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -r=interface{}->int
+
 // Copyright 2013 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.
index 6633a5e001e1f680c1ec088ea06694acf3a4e846..04bc16f2160b62e330346aa424afc395b4be200a 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -s
+
 // Test cases for slice expression simplification.
 package p
 
index 27e9cb8fef33be37b701bffa4c6471ee7344cec5..1f25c43ccbc825ba1a89cee7e0362c43c4eba257 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -s
+
 // Test cases for slice expression simplification.
 package p
 
index 433788e1ee6911ab72c748d413345e0b2ebef818..ab657004e64accaae1c2b1258e3b795fef78bcf0 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -s
+
 // Test cases for slice expression simplification.
 // Because of a dot import, these slices must remain untouched.
 package p
index 433788e1ee6911ab72c748d413345e0b2ebef818..ab657004e64accaae1c2b1258e3b795fef78bcf0 100644 (file)
@@ -1,3 +1,5 @@
+//gofmt -s
+
 // Test cases for slice expression simplification.
 // Because of a dot import, these slices must remain untouched.
 package p
index ff8b0b7ab484ade2a19216f4bcbd0c2b94d16921..9e4dcd20fe0d563ed51b2bac27567fbc7f5ee63c 100644 (file)
@@ -1,3 +1,5 @@
+       //gofmt -stdin
+
        if x {
                y
        }
index ff8b0b7ab484ade2a19216f4bcbd0c2b94d16921..9e4dcd20fe0d563ed51b2bac27567fbc7f5ee63c 100644 (file)
@@ -1,3 +1,5 @@
+       //gofmt -stdin
+
        if x {
                y
        }
index 7eb1b54fec058ebbc4f248f05a63fddb63112380..57df35540358c1c15f8cb3dfbdc292ae7d59c225 100644 (file)
@@ -1,4 +1,4 @@
-
+//gofmt -stdin
 
 var x int
 
index 99defd2d10c5c3cb53e294fbcf9f955fe2a26f9f..69d6bdd682efe8f90e157ef1f93f549c7cc7b4a4 100644 (file)
@@ -1,4 +1,4 @@
-
+//gofmt -stdin
 
 var x int
 
index 1bf2f5a483f9a0e0c0a10a959045f5a44911547b..d6da0e417a06ce11d31cab2bea97606987ee3288 100644 (file)
@@ -1,3 +1,4 @@
+               //gofmt -stdin
 
                /* note: no newline at end of file */
                for i := 0; i < 10; i++ {
index d963bd0d21bc54f829799e8596c84d7c35c5707a..ab46c1063beba078cd8a18c4fd2beb64186e9d0a 100644 (file)
@@ -1,3 +1,4 @@
+               //gofmt -stdin
 
                /* note: no newline at end of file */
                for i := 0; i < 10; i++ { s += i }
index 5f73435517f4376404e5915844df18113b8fb338..0c7acace5d0fa8c557e988699f92311ad74aa659 100644 (file)
@@ -1,3 +1,5 @@
+       //gofmt -stdin
+
        // comment
 
        i := 0
index f02a54fb1a9dbdf052093eb45172e64c309907e0..1fc73f31e5e68aadf77ed9a334655e98754812bf 100644 (file)
@@ -1,3 +1,5 @@
+       //gofmt -stdin
+
        // comment
        
        i := 0