]> Cypherpunks repositories - gostls13.git/commitdiff
gofmt: add test framework in Go
authorRobert Griesemer <gri@golang.org>
Wed, 13 Apr 2011 20:59:59 +0000 (13:59 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 13 Apr 2011 20:59:59 +0000 (13:59 -0700)
- replaced existing testdata/test.sh with new gofmt_test
- added initial test case for rewrite tests

TODO: Need to add more tests.

R=rsc
CC=golang-dev
https://golang.org/cl/4368063

src/cmd/gofmt/gofmt_test.go [new file with mode: 0644]
src/cmd/gofmt/testdata/rewrite1.golden [new file with mode: 0644]
src/cmd/gofmt/testdata/rewrite1.input [new file with mode: 0644]
src/cmd/gofmt/testdata/test.sh [deleted file]

diff --git a/src/cmd/gofmt/gofmt_test.go b/src/cmd/gofmt/gofmt_test.go
new file mode 100644 (file)
index 0000000..4ec94e2
--- /dev/null
@@ -0,0 +1,81 @@
+// 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, dirname, in, out, flags string) {
+       in = filepath.Join(dirname, in)
+       out = filepath.Join(dirname, out)
+
+       // process flags
+       *simplifyAST = false
+       *rewriteRule = ""
+       for _, flag := range strings.Split(flags, " ", -1) {
+               elts := strings.Split(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
+               default:
+                       t.Errorf("unrecognized flag name: %s", name)
+               }
+       }
+
+       initParserMode()
+       initPrinterMode()
+       initRewrite()
+
+       var buf bytes.Buffer
+       err := processFile(in, nil, &buf)
+       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)
+               ioutil.WriteFile(in+".gofmt", got, 0666)
+       }
+}
+
+
+// TODO(gri) Add more test cases!
+var tests = []struct {
+       dirname, in, out, flags string
+}{
+       {".", "gofmt.go", "gofmt.go", ""},
+       {".", "gofmt_test.go", "gofmt_test.go", ""},
+       {"testdata", "composites.input", "composites.golden", "-s"},
+       {"testdata", "rewrite1.input", "rewrite1.golden", "-r=Foo->Bar"},
+}
+
+
+func TestRewrite(t *testing.T) {
+       for _, test := range tests {
+               runTest(t, test.dirname, test.in, test.out, test.flags)
+       }
+}
diff --git a/src/cmd/gofmt/testdata/rewrite1.golden b/src/cmd/gofmt/testdata/rewrite1.golden
new file mode 100644 (file)
index 0000000..3f909ff
--- /dev/null
@@ -0,0 +1,8 @@
+package main
+
+type Bar int
+
+func main() {
+       var a Bar
+       println(a)
+}
diff --git a/src/cmd/gofmt/testdata/rewrite1.input b/src/cmd/gofmt/testdata/rewrite1.input
new file mode 100644 (file)
index 0000000..1f10e36
--- /dev/null
@@ -0,0 +1,8 @@
+package main
+
+type Foo int
+
+func main() {
+       var a Foo
+       println(a)
+}
diff --git a/src/cmd/gofmt/testdata/test.sh b/src/cmd/gofmt/testdata/test.sh
deleted file mode 100755 (executable)
index a1d5d82..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-#!/usr/bin/env bash
-# Copyright 2010 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.
-
-CMD="../gofmt"
-TMP=test_tmp.go
-COUNT=0
-
-
-cleanup() {
-       rm -f $TMP
-}
-
-
-error() {
-       echo $1
-       exit 1
-}
-
-
-count() {
-       #echo $1
-       let COUNT=$COUNT+1
-       let M=$COUNT%10
-       if [ $M == 0 ]; then
-               echo -n "."
-       fi
-}
-
-
-test() {
-       count $1
-
-       # compare against .golden file
-       cleanup
-       $CMD -s $1 > $TMP
-       cmp -s $TMP $2
-       if [ $? != 0 ]; then
-               diff $TMP $2
-               error "Error: simplified $1 does not match $2"
-       fi
-
-       # make sure .golden is idempotent
-       cleanup
-       $CMD -s $2 > $TMP
-       cmp -s $TMP $2
-       if [ $? != 0 ]; then
-               diff $TMP $2
-               error "Error: $2 is not idempotent"
-       fi
-}
-
-
-runtests() {
-       smoketest=../../../pkg/go/parser/parser.go
-       test $smoketest $smoketest
-       test composites.input composites.golden
-       # add more test cases here
-}
-
-
-runtests
-cleanup
-echo "PASSED ($COUNT tests)"