diff options
| author | Rob Pike <r@golang.org> | 2009-06-09 09:53:44 -0700 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2009-06-09 09:53:44 -0700 |
| commit | 7249ea4df2b4f12a4e7ed446f270cea87e4ffd34 (patch) | |
| tree | 7032a11d0cac2ae4d3e90f7a189b575b5a50f848 /src/pkg/testing | |
| parent | acf6ef7a82b3fe61516a1bac4563706552bdf078 (diff) | |
| download | golang-7249ea4df2b4f12a4e7ed446f270cea87e4ffd34.tar.gz | |
mv src/lib to src/pkg
tests: all.bash passes, gobuild still works, godoc still works.
R=rsc
OCL=30096
CL=30102
Diffstat (limited to 'src/pkg/testing')
| -rw-r--r-- | src/pkg/testing/Makefile | 60 | ||||
| -rw-r--r-- | src/pkg/testing/iotest/Makefile | 61 | ||||
| -rw-r--r-- | src/pkg/testing/iotest/logger.go | 55 | ||||
| -rw-r--r-- | src/pkg/testing/iotest/reader.go | 44 | ||||
| -rw-r--r-- | src/pkg/testing/testing.go | 155 |
5 files changed, 375 insertions, 0 deletions
diff --git a/src/pkg/testing/Makefile b/src/pkg/testing/Makefile new file mode 100644 index 000000000..493eba6f2 --- /dev/null +++ b/src/pkg/testing/Makefile @@ -0,0 +1,60 @@ +# Copyright 2009 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. + +# DO NOT EDIT. Automatically generated by gobuild. +# gobuild -m >Makefile + +D= + +include $(GOROOT)/src/Make.$(GOARCH) +AR=gopack + +default: packages + +clean: + rm -rf *.[$(OS)] *.a [$(OS)].out _obj + +test: packages + gotest + +coverage: packages + gotest + 6cov -g `pwd` | grep -v '_test\.go:' + +%.$O: %.go + $(GC) -I_obj $*.go + +%.$O: %.c + $(CC) $*.c + +%.$O: %.s + $(AS) $*.s + +O1=\ + testing.$O\ + + +phases: a1 +_obj$D/testing.a: phases + +a1: $(O1) + $(AR) grc _obj$D/testing.a testing.$O + rm -f $(O1) + + +newpkg: clean + mkdir -p _obj$D + $(AR) grc _obj$D/testing.a + +$(O1): newpkg +$(O2): a1 + +nuke: clean + rm -f $(GOROOT)/pkg/$(GOOS)_$(GOARCH)$D/testing.a + +packages: _obj$D/testing.a + +install: packages + test -d $(GOROOT)/pkg && mkdir -p $(GOROOT)/pkg/$(GOOS)_$(GOARCH)$D + cp _obj$D/testing.a $(GOROOT)/pkg/$(GOOS)_$(GOARCH)$D/testing.a diff --git a/src/pkg/testing/iotest/Makefile b/src/pkg/testing/iotest/Makefile new file mode 100644 index 000000000..1d01041f5 --- /dev/null +++ b/src/pkg/testing/iotest/Makefile @@ -0,0 +1,61 @@ +# Copyright 2009 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. + +# DO NOT EDIT. Automatically generated by gobuild. +# gobuild -m >Makefile + +D=/testing/ + +include $(GOROOT)/src/Make.$(GOARCH) +AR=gopack + +default: packages + +clean: + rm -rf *.[$(OS)] *.a [$(OS)].out _obj + +test: packages + gotest + +coverage: packages + gotest + 6cov -g `pwd` | grep -v '_test\.go:' + +%.$O: %.go + $(GC) -I_obj $*.go + +%.$O: %.c + $(CC) $*.c + +%.$O: %.s + $(AS) $*.s + +O1=\ + logger.$O\ + reader.$O\ + + +phases: a1 +_obj$D/iotest.a: phases + +a1: $(O1) + $(AR) grc _obj$D/iotest.a logger.$O reader.$O + rm -f $(O1) + + +newpkg: clean + mkdir -p _obj$D + $(AR) grc _obj$D/iotest.a + +$(O1): newpkg +$(O2): a1 + +nuke: clean + rm -f $(GOROOT)/pkg/$(GOOS)_$(GOARCH)$D/iotest.a + +packages: _obj$D/iotest.a + +install: packages + test -d $(GOROOT)/pkg && mkdir -p $(GOROOT)/pkg/$(GOOS)_$(GOARCH)$D + cp _obj$D/iotest.a $(GOROOT)/pkg/$(GOOS)_$(GOARCH)$D/iotest.a diff --git a/src/pkg/testing/iotest/logger.go b/src/pkg/testing/iotest/logger.go new file mode 100644 index 000000000..8ee574080 --- /dev/null +++ b/src/pkg/testing/iotest/logger.go @@ -0,0 +1,55 @@ +// Copyright 2009 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 iotest + +import ( + "io"; + "log"; + "os"; +) + +type writeLogger struct { + prefix string; + w io.Writer; +} + +func (l *writeLogger) Write(p []byte) (n int, err os.Error) { + n, err = l.w.Write(p); + if err != nil { + log.Stdoutf("%s %x: %v", l.prefix, p[0:n], err); + } else { + log.Stdoutf("%s %x", l.prefix, p[0:n]); + } + return; +} + +// NewWriteLogger returns a writer that behaves like w except +// that it logs (using log.Stdout) each write to standard output, +// printing the prefix and the hexadecimal data written. +func NewWriteLogger(prefix string, w io.Writer) io.Writer { + return &writeLogger{prefix, w} +} + +type readLogger struct { + prefix string; + r io.Reader; +} + +func (l *readLogger) Read(p []byte) (n int, err os.Error) { + n, err = l.r.Read(p); + if err != nil { + log.Stdoutf("%s %x: %v", l.prefix, p[0:n], err); + } else { + log.Stdoutf("%s %x", l.prefix, p[0:n]); + } + return; +} + +// NewReadLogger returns a writer that behaves like w except +// that it logs (using log.Stdout) each write to standard output, +// printing the prefix and the hexadecimal data written. +func NewReadLogger(prefix string, r io.Reader) io.Reader { + return &readLogger{prefix, r} +} diff --git a/src/pkg/testing/iotest/reader.go b/src/pkg/testing/iotest/reader.go new file mode 100644 index 000000000..0bb863338 --- /dev/null +++ b/src/pkg/testing/iotest/reader.go @@ -0,0 +1,44 @@ +// Copyright 2009 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. + +// The iotest package implements Readers and Writers +// useful only for testing. +package iotest + +import ( + "io"; + "os"; +) + +type oneByteReader struct { + r io.Reader; +} + +func (r *oneByteReader) Read(p []byte) (int, os.Error) { + if len(p) == 0 { + return 0, nil; + } + return r.r.Read(p[0:1]); +} + +// OneByteReader returns a Reader that implements +// each non-empty Read by reading one byte from r. +func OneByteReader(r io.Reader) io.Reader { + return &oneByteReader{r}; +} + +type halfReader struct { + r io.Reader; +} + +func (r *halfReader) Read(p []byte) (int, os.Error) { + return r.r.Read(p[0:(len(p)+1)/2]); +} + +// HalfReader returns a Reader that implements Read +// by reading half as many requested bytes from r. +func HalfReader(r io.Reader) io.Reader { + return &halfReader{r}; +} + diff --git a/src/pkg/testing/testing.go b/src/pkg/testing/testing.go new file mode 100644 index 000000000..330fadd3a --- /dev/null +++ b/src/pkg/testing/testing.go @@ -0,0 +1,155 @@ +// Copyright 2009 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. + +// The testing package provides support for automated testing of Go packages. +// It is intended to be used in concert with the ``gotest'' utility, which automates +// execution of any function of the form +// func TestXxx(*testing.T) +// where Xxx can by any alphanumeric string (but the first letter must not be in +// [a-z]) and serves to identify the test routine. +// These TestXxx routines should be declared within the package they are testing. +package testing + +import ( + "flag"; + "fmt"; + "os"; + "regexp"; + "runtime"; +) + +// Report as tests are run; default is silent for success. +var chatty = flag.Bool("v", false, "verbose: print additional output") +var match = flag.String("match", "", "regular expression to select tests to run") + + +// Insert final newline if needed and tabs after internal newlines. +func tabify(s string) string { + n := len(s); + if n > 0 && s[n-1] != '\n' { + s += "\n"; + n++; + } + for i := 0; i < n - 1; i++ { // -1 to avoid final newline + if s[i] == '\n' { + return s[0:i+1] + "\t" + tabify(s[i+1:n]); + } + } + return s +} + +// T is a type passed to Test functions to manage test state and support formatted test logs. +// Logs are accumulated during execution and dumped to standard error when done. +type T struct { + errors string; + failed bool; + ch chan *T; +} + +// Fail marks the Test function as having failed but continues execution. +func (t *T) Fail() { + t.failed = true +} + +// Failed returns whether the Test function has failed. +func (t *T) Failed() bool { + return t.failed +} + +// FailNow marks the Test function as having failed and stops its execution. +// Execution will continue at the next Test. +func (t *T) FailNow() { + t.Fail(); + t.ch <- t; + runtime.Goexit(); +} + +// Log formats its arguments using default formatting, analogous to Print(), +// and records the text in the error log. +func (t *T) Log(args ...) { + t.errors += "\t" + tabify(fmt.Sprintln(args)); +} + +// Log formats its arguments according to the format, analogous to Printf(), +// and records the text in the error log. +func (t *T) Logf(format string, args ...) { + t.errors += "\t" + tabify(fmt.Sprintf(format, args)); +} + +// Error is equivalent to Log() followed by Fail(). +func (t *T) Error(args ...) { + t.Log(args); + t.Fail(); +} + +// Errorf is equivalent to Logf() followed by Fail(). +func (t *T) Errorf(format string, args ...) { + t.Logf(format, args); + t.Fail(); +} + +// Fatal is equivalent to Log() followed by FailNow(). +func (t *T) Fatal(args ...) { + t.Log(args); + t.FailNow(); +} + +// Fatalf is equivalent to Logf() followed by FailNow(). +func (t *T) Fatalf(format string, args ...) { + t.Logf(format, args); + t.FailNow(); +} + +// An internal type but exported because it is cross-package; part of the implementation +// of gotest. +type Test struct { + Name string; + F func(*T); +} + +func tRunner(t *T, test *Test) { + test.F(t); + t.ch <- t; +} + +// An internal function but exported because it is cross-package; part of the implementation +// of gotest. +func Main(tests []Test) { + flag.Parse(); + args := flag.Args(); + ok := true; + if len(tests) == 0 { + println("testing: warning: no tests to run"); + } + re, err := regexp.Compile(*match); + if err != nil { + println("invalid regexp for -match:", err.String()); + os.Exit(1); + } + for i := 0; i < len(tests); i++ { + if !re.Match(tests[i].Name) { + continue; + } + if *chatty { + println("=== RUN ", tests[i].Name); + } + t := new(T); + t.ch = make(chan *T); + go tRunner(t, &tests[i]); + <-t.ch; + if t.failed { + println("--- FAIL:", tests[i].Name); + print(t.errors); + ok = false; + } else if *chatty { + println("--- PASS:", tests[i].Name); + print(t.errors); + } + } + if !ok { + println("FAIL"); + os.Exit(1); + } + println("PASS"); +} |
