|
| 1 | +package neko |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/mock" |
| 7 | +) |
| 8 | + |
| 9 | +// Simple tuple around a test description and the work |
| 10 | +type testModern struct { |
| 11 | + Name string |
| 12 | + Func func(t *testing.T) |
| 13 | +} |
| 14 | + |
| 15 | +// Keeps track of mocks, setups, and tests so they can be |
| 16 | +// later coordinated. |
| 17 | + |
| 18 | +type OrganizerModern struct { |
| 19 | + t *testing.T |
| 20 | + |
| 21 | + only *testModern |
| 22 | + mocks []*mock.Mock |
| 23 | + setup []func() |
| 24 | + cleanup []func() |
| 25 | + tests []testModern |
| 26 | +} |
| 27 | + |
| 28 | +// Create a new OrganizerModern against testing's T interface |
| 29 | +func Modern(t *testing.T) *OrganizerModern { |
| 30 | + return &OrganizerModern{t: t} |
| 31 | +} |
| 32 | + |
| 33 | +// Track a github.com/stretchr/testify/mock.Mock along with the tests |
| 34 | +func (o *OrganizerModern) CheckMock(m *mock.Mock) { |
| 35 | + o.mocks = append(o.mocks, m) |
| 36 | +} |
| 37 | + |
| 38 | +// Add some work to be done before each test |
| 39 | +func (o *OrganizerModern) Setup(f func()) { |
| 40 | + o.setup = append(o.setup, f) |
| 41 | +} |
| 42 | + |
| 43 | +// Add some work to be done after each test |
| 44 | +func (o *OrganizerModern) Cleanup(f func()) { |
| 45 | + o.cleanup = append(o.cleanup, f) |
| 46 | +} |
| 47 | + |
| 48 | +// Add a test. |
| 49 | +func (o *OrganizerModern) It(name string, f func(t *testing.T)) { |
| 50 | + o.tests = append(o.tests, testModern{name, f}) |
| 51 | +} |
| 52 | + |
| 53 | +func (o *OrganizerModern) Only(name string, f func(t *testing.T)) { |
| 54 | + o.only = &testModern{name, f} |
| 55 | +} |
| 56 | + |
| 57 | +// Useful by allowing the developer to simply add 'N' before |
| 58 | +// It to disable a block. |
| 59 | +func (o *OrganizerModern) NIt(name string, f func(t *testing.T)) { |
| 60 | + o.tests = append(o.tests, testModern{name, nil}) |
| 61 | +} |
| 62 | + |
| 63 | +// Coordinate running the tests with the setups and mocks |
| 64 | +func (o *OrganizerModern) Run() { |
| 65 | + if o.only != nil { |
| 66 | + o.runTest(o.only) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + for _, test := range o.tests { |
| 71 | + o.runTest(&test) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (o *OrganizerModern) runTest(test *testModern) { |
| 76 | + if test.Func == nil { |
| 77 | + o.t.Logf("==== DISABLED: %s ====", test.Name) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + o.t.Run(test.Name, func(t *testing.T) { |
| 82 | + for _, mock := range o.mocks { |
| 83 | + mock.ExpectedCalls = nil |
| 84 | + mock.Calls = nil |
| 85 | + } |
| 86 | + |
| 87 | + for _, setup := range o.setup { |
| 88 | + setup() |
| 89 | + } |
| 90 | + |
| 91 | + defer o.runCleanup() |
| 92 | + |
| 93 | + test.Func(t) |
| 94 | + |
| 95 | + for _, mock := range o.mocks { |
| 96 | + mock.AssertExpectations(o.t) |
| 97 | + } |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +func (o *OrganizerModern) runCleanup() { |
| 102 | + for _, cleanup := range o.cleanup { |
| 103 | + cleanup() |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// Have fun with neko! |
| 108 | +func (o *OrganizerModern) Meow() { |
| 109 | + o.Run() |
| 110 | +} |
0 commit comments