Commit 99f069a9 authored by Robert Griesemer's avatar Robert Griesemer

gofmt: add test framework in Go

- 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
parent 3d4b55ad
// 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)
}
}
package main
type Bar int
func main() {
var a Bar
println(a)
}
package main
type Foo int
func main() {
var a Foo
println(a)
}
#!/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)"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment