|
| 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 |
0 commit comments