Skip to content

Commit e900ae0

Browse files
committed
Inlined a subset of gunit functionality, thus removing that dependency.
1 parent eb5b599 commit e900ae0

File tree

5 files changed

+137
-12
lines changed

5 files changed

+137
-12
lines changed

assert/assert_failed_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package assert
33
import (
44
"testing"
55

6+
"github.com/smartystreets/assertions/internal/unit"
67
"github.com/smartystreets/assertions/should"
7-
"github.com/smartystreets/gunit"
88
"github.com/smartystreets/logging"
99
)
1010

1111
func TestFailedResultFixture(t *testing.T) {
12-
gunit.Run(new(FailedResultFixture), t)
12+
unit.Run(new(FailedResultFixture), t)
1313
}
1414

1515
type FailedResultFixture struct {
16-
*gunit.Fixture
16+
*unit.Fixture
1717

1818
result *Result
1919
}

assert/assert_passed_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package assert
33
import (
44
"testing"
55

6+
"github.com/smartystreets/assertions/internal/unit"
67
"github.com/smartystreets/assertions/should"
7-
"github.com/smartystreets/gunit"
88
"github.com/smartystreets/logging"
99
)
1010

1111
func TestPassedResultFixture(t *testing.T) {
12-
gunit.Run(new(PassedResultFixture), t)
12+
unit.Run(new(PassedResultFixture), t)
1313
}
1414

1515
type PassedResultFixture struct {
16-
*gunit.Fixture
16+
*unit.Fixture
1717

1818
result *Result
1919
}

equal_method_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package assertions
33
import (
44
"testing"
55

6-
"github.com/smartystreets/gunit"
6+
"github.com/smartystreets/assertions/internal/unit"
77
)
88

99
func TestEqualityFixture(t *testing.T) {
10-
gunit.Run(new(EqualityFixture), t)
10+
unit.Run(new(EqualityFixture), t)
1111
}
1212

1313
type EqualityFixture struct {
14-
*gunit.Fixture
14+
*unit.Fixture
1515
}
1616

1717
func (this *EqualityFixture) TestNilNil() {

internal/unit/fixture.go

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// package unit implements a light-weight x-Unit style testing framework.
2+
// It is basically a scaled-down version of github.com/smartystreets/gunit.
3+
// See https://smartystreets.com/blog/2018/07/lets-build-xunit-in-go for
4+
// an explanation if the basic moving parts.
5+
package unit
6+
7+
import (
8+
"bytes"
9+
"fmt"
10+
"reflect"
11+
"runtime"
12+
"strings"
13+
"testing"
14+
)
15+
16+
func Run(fixture interface{}, t *testing.T) {
17+
fixtureType := reflect.TypeOf(fixture)
18+
19+
for x := 0; x < fixtureType.NumMethod(); x++ {
20+
testMethodName := fixtureType.Method(x).Name
21+
if strings.HasPrefix(testMethodName, "Test") {
22+
t.Run(testMethodName, func(t *testing.T) {
23+
instance := reflect.New(fixtureType.Elem())
24+
25+
innerFixture := newFixture(t, testing.Verbose())
26+
field := instance.Elem().FieldByName("Fixture")
27+
field.Set(reflect.ValueOf(innerFixture))
28+
29+
defer innerFixture.Finalize()
30+
31+
if setup := instance.MethodByName("Setup"); setup.IsValid() {
32+
setup.Call(nil)
33+
}
34+
35+
instance.MethodByName(testMethodName).Call(nil)
36+
37+
if teardown := instance.MethodByName("Teardown"); teardown.IsValid() {
38+
teardown.Call(nil)
39+
}
40+
})
41+
}
42+
}
43+
}
44+
45+
type Fixture struct {
46+
t *testing.T
47+
log *bytes.Buffer
48+
verbose bool
49+
}
50+
51+
func newFixture(t *testing.T, verbose bool) *Fixture {
52+
return &Fixture{t: t, verbose: verbose, log: &bytes.Buffer{}}
53+
}
54+
55+
func (this *Fixture) So(actual interface{}, assert assertion, expected ...interface{}) bool {
56+
failure := assert(actual, expected...)
57+
failed := len(failure) > 0
58+
if failed {
59+
this.fail(failure)
60+
}
61+
return !failed
62+
}
63+
64+
func (this *Fixture) fail(failure string) {
65+
this.t.Fail()
66+
this.Print(failure)
67+
}
68+
69+
// Assert tests a boolean which, if not true, marks the current test case as failed and
70+
// prints the provided message.
71+
func (this *Fixture) Assert(condition bool, messages ...string) bool {
72+
if !condition {
73+
if len(messages) == 0 {
74+
messages = append(messages, "Expected condition to be true, was false instead.")
75+
}
76+
this.fail(strings.Join(messages, ", "))
77+
}
78+
return condition
79+
}
80+
func (this *Fixture) AssertEqual(expected, actual interface{}) bool {
81+
return this.Assert(expected == actual, fmt.Sprintf(comparisonFormat, fmt.Sprint(expected), fmt.Sprint(actual)))
82+
}
83+
func (this *Fixture) AssertSprintEqual(expected, actual interface{}) bool {
84+
return this.AssertEqual(fmt.Sprint(expected), fmt.Sprint(actual))
85+
}
86+
func (this *Fixture) AssertSprintfEqual(expected, actual interface{}, format string) bool {
87+
return this.AssertEqual(fmt.Sprintf(format, expected), fmt.Sprintf(format, actual))
88+
}
89+
func (this *Fixture) AssertDeepEqual(expected, actual interface{}) bool {
90+
return this.Assert(reflect.DeepEqual(expected, actual),
91+
fmt.Sprintf(comparisonFormat, fmt.Sprintf("%#v", expected), fmt.Sprintf("%#v", actual)))
92+
}
93+
94+
const comparisonFormat = "Expected: [%s]\nActual: [%s]"
95+
96+
func (this *Fixture) Error(args ...interface{}) { this.fail(fmt.Sprint(args...)) }
97+
func (this *Fixture) Errorf(f string, args ...interface{}) { this.fail(fmt.Sprintf(f, args...)) }
98+
99+
func (this *Fixture) Print(a ...interface{}) { fmt.Fprint(this.log, a...) }
100+
func (this *Fixture) Printf(format string, a ...interface{}) { fmt.Fprintf(this.log, format, a...) }
101+
func (this *Fixture) Println(a ...interface{}) { fmt.Fprintln(this.log, a...) }
102+
103+
func (this *Fixture) Write(p []byte) (int, error) { return this.log.Write(p) }
104+
func (this *Fixture) Failed() bool { return this.t.Failed() }
105+
func (this *Fixture) Name() string { return this.t.Name() }
106+
107+
func (this *Fixture) Finalize() {
108+
if r := recover(); r != nil {
109+
this.recoverPanic(r)
110+
}
111+
112+
if this.t.Failed() || (this.verbose && this.log.Len() > 0) {
113+
this.t.Log("\n" + strings.TrimSpace(this.log.String()) + "\n")
114+
}
115+
}
116+
func (this *Fixture) recoverPanic(r interface{}) {
117+
this.Println("PANIC:", r)
118+
buffer := make([]byte, 1024*16)
119+
runtime.Stack(buffer, false)
120+
this.Println(strings.TrimSpace(string(buffer)))
121+
this.t.Fail()
122+
}
123+
124+
// assertion is a copy of github.com/smartystreets/assertions.assertion.
125+
type assertion func(actual interface{}, expected ...interface{}) string

utilities_for_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import (
55
"strings"
66
"testing"
77

8-
"github.com/smartystreets/gunit"
8+
"github.com/smartystreets/assertions/internal/unit"
99
)
1010

1111
/**************************************************************************/
1212

1313
func TestAssertionsFixture(t *testing.T) {
14-
gunit.Run(new(AssertionsFixture), t)
14+
unit.Run(new(AssertionsFixture), t)
1515
}
1616

1717
type AssertionsFixture struct {
18-
*gunit.Fixture
18+
*unit.Fixture
1919
}
2020

2121
func (this *AssertionsFixture) Setup() {

0 commit comments

Comments
 (0)