Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an interface for the reporter #76

Open
wants to merge 7 commits into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: go
sudo: false
go:
- tip
before_install:
- go get github.com/mattn/goveralls
script:
- ./gen-coverage.sh coverage.out
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Instructions

Install the package with:

go get gopkg.in/check.v1
go get github.com/elopio/check

Import it with:

import "gopkg.in/check.v1"
import "github.com/elopio/check"

and use _check_ as the package name inside the code.

Expand Down
3 changes: 2 additions & 1 deletion benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ package check_test

import (
"time"
. "gopkg.in/check.v1"

. "github.com/elopio/check"
)

var benchmarkS = Suite(&BenchmarkS{})
Expand Down
3 changes: 2 additions & 1 deletion bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ package check_test

import (
"fmt"
"gopkg.in/check.v1"
"strings"

"github.com/elopio/check"
)

type BootstrapS struct{}
Expand Down
60 changes: 35 additions & 25 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ type C struct {
testName string
_status funcStatus
logb *logger
logw io.Writer
done chan *C
reason string
mustFail bool
Expand All @@ -109,25 +108,30 @@ func (c *C) stopNow() {
// logger is a concurrency safe byte.Buffer
type logger struct {
sync.Mutex
writer bytes.Buffer
buffer bytes.Buffer
output io.Writer
verbosity uint8
}

func (l *logger) Write(buf []byte) (int, error) {
l.Lock()
defer l.Unlock()
return l.writer.Write(buf)
if l.verbosity > 1 {
l.output.Write(buf)
}
return l.buffer.Write(buf)
}

func (l *logger) WriteTo(w io.Writer) (int64, error) {
l.Lock()
defer l.Unlock()
return l.writer.WriteTo(w)
return l.buffer.WriteTo(w)
}

func (l *logger) String() string {
l.Lock()
defer l.Unlock()
return l.writer.String()
return l.buffer.String()
}

// -----------------------------------------------------------------------
Expand Down Expand Up @@ -198,9 +202,6 @@ func (c *C) logNewLine() {

func (c *C) writeLog(buf []byte) {
c.logb.Write(buf)
if c.logw != nil {
c.logw.Write(buf)
}
}

func hasStringOrError(x interface{}) (ok bool) {
Expand Down Expand Up @@ -518,10 +519,12 @@ type suiteRunner struct {
tracker *resultTracker
tempDir *tempDir
keepDir bool
output *outputWriter
logOutput io.Writer
reporter testReporter
reportedProblemLast bool
benchTime time.Duration
benchMem bool
verbosity uint8
}

type RunConf struct {
Expand Down Expand Up @@ -551,16 +554,25 @@ func newSuiteRunner(suite interface{}, runConf *RunConf) *suiteRunner {
suiteType := reflect.TypeOf(suite)
suiteNumMethods := suiteType.NumMethod()
suiteValue := reflect.ValueOf(suite)

var verbosity uint8
if conf.Verbose {
verbosity = 1
}
if conf.Stream {
verbosity = 2
}

runner := &suiteRunner{
suite: suite,
output: newOutputWriter(conf.Output, conf.Stream, conf.Verbose),
logOutput: conf.Output,
reporter: newOutputWriter(conf.Output, verbosity),
tracker: newResultTracker(),
benchTime: conf.BenchmarkTime,
benchMem: conf.BenchmarkMem,
tempDir: &tempDir{},
keepDir: conf.KeepWorkDir,
tests: make([]*methodType, 0, suiteNumMethods),
verbosity: verbosity,
}
if runner.benchTime == 0 {
runner.benchTime = 1 * time.Second
Expand Down Expand Up @@ -640,19 +652,17 @@ func (runner *suiteRunner) run() *Result {
// Create a call object with the given suite method, and fork a
// goroutine with the provided dispatcher for running it.
func (runner *suiteRunner) forkCall(method *methodType, kind funcKind, testName string, logb *logger, dispatcher func(c *C)) *C {
var logw io.Writer
if runner.output.Stream {
logw = runner.output
}
if logb == nil {
logb = new(logger)
logb = &logger{
output: runner.logOutput,
verbosity: runner.verbosity,
}
}
c := &C{
method: method,
kind: kind,
testName: testName,
logb: logb,
logw: logw,
tempDir: runner.tempDir,
done: make(chan *C, 1),
timer: timer{benchTime: runner.benchTime},
Expand Down Expand Up @@ -843,31 +853,31 @@ func (runner *suiteRunner) checkFixtureArgs() bool {
}

func (runner *suiteRunner) reportCallStarted(c *C) {
runner.output.WriteCallStarted("START", c)
runner.reporter.StartTest(c)
}

func (runner *suiteRunner) reportCallDone(c *C) {
runner.tracker.callDone(c)
switch c.status() {
case succeededSt:
if c.mustFail {
runner.output.WriteCallSuccess("FAIL EXPECTED", c)
runner.reporter.AddExpectedFailure(c)
} else {
runner.output.WriteCallSuccess("PASS", c)
runner.reporter.AddSuccess(c)
}
case skippedSt:
runner.output.WriteCallSuccess("SKIP", c)
runner.reporter.AddSkip(c)
case failedSt:
runner.output.WriteCallProblem("FAIL", c)
runner.reporter.AddFailure(c)
case panickedSt:
runner.output.WriteCallProblem("PANIC", c)
runner.reporter.AddError(c)
case fixturePanickedSt:
// That's a testKd call reporting that its fixture
// has panicked. The fixture call which caused the
// panic itself was tracked above. We'll report to
// aid debugging.
runner.output.WriteCallProblem("PANIC", c)
runner.reporter.AddError(c)
case missedSt:
runner.output.WriteCallSuccess("MISS", c)
runner.reporter.AddMissed(c)
}
}
34 changes: 17 additions & 17 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
"time"

"gopkg.in/check.v1"
. "github.com/elopio/check"
)

// We count the number of suites run at least to get a vague hint that the
Expand All @@ -23,7 +23,7 @@ const suitesRunExpected = 8
var suitesRun int = 0

func Test(t *testing.T) {
check.TestingT(t)
TestingT(t)
if suitesRun != suitesRunExpected && flag.Lookup("check.f").Value.String() == "" {
critical(fmt.Sprintf("Expected %d suites to run rather than %d",
suitesRunExpected, suitesRun))
Expand Down Expand Up @@ -65,8 +65,8 @@ func (s *String) Write(p []byte) (n int, err error) {

// Trivial wrapper to test errors happening on a different file
// than the test itself.
func checkEqualWrapper(c *check.C, obtained, expected interface{}) (result bool, line int) {
return c.Check(obtained, check.Equals, expected), getMyLine()
func checkEqualWrapper(c *C, obtained, expected interface{}) (result bool, line int) {
return c.Check(obtained, Equals, expected), getMyLine()
}

// -----------------------------------------------------------------------
Expand All @@ -76,7 +76,7 @@ type FailHelper struct {
testLine int
}

func (s *FailHelper) TestLogAndFail(c *check.C) {
func (s *FailHelper) TestLogAndFail(c *C) {
s.testLine = getMyLine() - 1
c.Log("Expected failure!")
c.Fail()
Expand All @@ -87,7 +87,7 @@ func (s *FailHelper) TestLogAndFail(c *check.C) {

type SuccessHelper struct{}

func (s *SuccessHelper) TestLogAndSucceed(c *check.C) {
func (s *SuccessHelper) TestLogAndSucceed(c *C) {
c.Log("Expected success!")
}

Expand All @@ -104,7 +104,7 @@ type FixtureHelper struct {
bytes int64
}

func (s *FixtureHelper) trace(name string, c *check.C) {
func (s *FixtureHelper) trace(name string, c *C) {
s.calls = append(s.calls, name)
if name == s.panicOn {
panic(name)
Expand All @@ -117,46 +117,46 @@ func (s *FixtureHelper) trace(name string, c *check.C) {
}
}

func (s *FixtureHelper) SetUpSuite(c *check.C) {
func (s *FixtureHelper) SetUpSuite(c *C) {
s.trace("SetUpSuite", c)
}

func (s *FixtureHelper) TearDownSuite(c *check.C) {
func (s *FixtureHelper) TearDownSuite(c *C) {
s.trace("TearDownSuite", c)
}

func (s *FixtureHelper) SetUpTest(c *check.C) {
func (s *FixtureHelper) SetUpTest(c *C) {
s.trace("SetUpTest", c)
}

func (s *FixtureHelper) TearDownTest(c *check.C) {
func (s *FixtureHelper) TearDownTest(c *C) {
s.trace("TearDownTest", c)
}

func (s *FixtureHelper) Test1(c *check.C) {
func (s *FixtureHelper) Test1(c *C) {
s.trace("Test1", c)
}

func (s *FixtureHelper) Test2(c *check.C) {
func (s *FixtureHelper) Test2(c *C) {
s.trace("Test2", c)
}

func (s *FixtureHelper) Benchmark1(c *check.C) {
func (s *FixtureHelper) Benchmark1(c *C) {
s.trace("Benchmark1", c)
for i := 0; i < c.N; i++ {
time.Sleep(s.sleep)
}
}

func (s *FixtureHelper) Benchmark2(c *check.C) {
func (s *FixtureHelper) Benchmark2(c *C) {
s.trace("Benchmark2", c)
c.SetBytes(1024)
for i := 0; i < c.N; i++ {
time.Sleep(s.sleep)
}
}

func (s *FixtureHelper) Benchmark3(c *check.C) {
func (s *FixtureHelper) Benchmark3(c *C) {
var x []int64
s.trace("Benchmark3", c)
for i := 0; i < c.N; i++ {
Expand All @@ -181,7 +181,7 @@ type expectedState struct {
// Verify the state of the test. Note that since this also verifies if
// the test is supposed to be in a failed state, no other checks should
// be done in addition to what is being tested.
func checkState(c *check.C, result interface{}, expected *expectedState) {
func checkState(c *C, result interface{}, expected *expectedState) {
failed := c.Failed()
c.Succeed()
log := c.GetTestLog()
Expand Down
3 changes: 2 additions & 1 deletion checkers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package check_test

import (
"errors"
"gopkg.in/check.v1"
"reflect"
"runtime"

"github.com/elopio/check"
)

type CheckersS struct{}
Expand Down
8 changes: 6 additions & 2 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package check

import "io"

type TestReporter interface {
testReporter
}

func PrintLine(filename string, line int) (string, error) {
return printLine(filename, line)
}
Expand All @@ -10,8 +14,8 @@ func Indent(s, with string) string {
return indent(s, with)
}

func NewOutputWriter(writer io.Writer, stream, verbose bool) *outputWriter {
return newOutputWriter(writer, stream, verbose)
func NewOutputWriter(writer io.Writer, verbosity uint8) *outputWriter {
return newOutputWriter(writer, verbosity)
}

func (c *C) FakeSkip(reason string) {
Expand Down
6 changes: 2 additions & 4 deletions fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

package check_test

import (
. "gopkg.in/check.v1"
)
import . "github.com/elopio/check"

// -----------------------------------------------------------------------
// Fixture test suite.
Expand All @@ -14,7 +12,7 @@ type FixtureS struct{}
var fixtureS = Suite(&FixtureS{})

func (s *FixtureS) TestCountSuite(c *C) {
suitesRun += 1
suitesRun++
}

// -----------------------------------------------------------------------
Expand Down
Loading