Skip to content

Commit

Permalink
issue-37: Remove testify from dependencies closes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
lowitea committed May 26, 2022
1 parent 8ddcec3 commit 57a2176
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 56 deletions.
75 changes: 38 additions & 37 deletions bell_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package bell

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sort"
"sync/atomic"
"testing"
Expand All @@ -19,114 +17,115 @@ func resetSystem() {
globalState = &Events{channels: map[string][]chan Message{}}
}

func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Error(err)
}
}

func assertTrue(t *testing.T, v bool) {
t.Helper()
if v != true {
t.Error("Value must be true")
}
}

// TestListenN checking the function of adding multiple copies of event listeners
func TestListenN(t *testing.T) {
resetSystem()
defer resetSystem()

eventName := "event"
var wasRunning int32
ListenN(eventName, func(Message) { atomic.AddInt32(&wasRunning, 1) }, 3)

require.NoError(t, Ring(eventName, nil))
assertNoError(t, Ring(eventName, nil))
Wait()

assert.Equal(t, int32(1), wasRunning)
assertTrue(t, wasRunning == 1)

require.NoError(t, Ring(eventName, nil))
require.NoError(t, Ring(eventName, nil))
assertNoError(t, Ring(eventName, nil))
assertNoError(t, Ring(eventName, nil))
Wait()

assert.Equal(t, int32(3), wasRunning)
assertTrue(t, wasRunning == 3)
}

// TestListen Testing the function of adding event listeners
func TestListen(t *testing.T) {
resetSystem()
defer resetSystem()

expMessageEvent := "test_event"
expMessageValue := "value"

Listen(expMessageEvent, func(message Message) {
assert.Equal(t, expMessageValue, message)
assertTrue(t, expMessageValue == message)
})

assert.Equal(t, 1, len(globalState.channels))
assert.Equal(t, 1, len(globalState.channels[expMessageEvent]))
assertTrue(t, len(globalState.channels) == 1)
assertTrue(t, len(globalState.channels[expMessageEvent]) == 1)

assert.NotPanics(t, func() {
err := Ring(expMessageEvent, expMessageValue)
assert.NoError(t, err)
})

assert.NotPanics(t, func() {
resetSystem()
})
assertNoError(t, Ring(expMessageEvent, expMessageValue))
}

// TestRing_Fail Checking the correctness of error handling in case of an erroneous ringing
func TestRing_Fail(t *testing.T) {
resetSystem()
defer resetSystem()

err := Ring("undefined_event", func() {})
assert.EqualError(t, err, "channel undefined_event not found")
assertTrue(t, err.Error() == "channel undefined_event not found")
}

// TestRemove Checking if event handlers are removed from storage
func TestRemove(t *testing.T) {
resetSystem()
defer resetSystem()

globalState.channels["test"] = append(globalState.channels["test"], make(chan Message), make(chan Message))
globalState.channels["test2"] = append(globalState.channels["test2"], make(chan Message))

Remove("test")
assert.Equal(t, 1, len(globalState.channels))
assertTrue(t, len(globalState.channels) == 1)

globalState.channels["test3"] = append(globalState.channels["test3"], make(chan Message))
globalState.channels["test4"] = append(globalState.channels["test4"], make(chan Message))
Remove("test2")
assert.Equal(t, 2, len(globalState.channels))
assertTrue(t, len(globalState.channels) == 2)

globalState.channels["test3"] = append(globalState.channels["test3"], make(chan Message))
globalState.channels["test4"] = append(globalState.channels["test4"], make(chan Message))
Remove()
assert.Equal(t, 0, len(globalState.channels))
assertTrue(t, len(globalState.channels) == 0)
}

// TestHas Checking the Correctness of Determining the Existence of Event Listeners
func TestHas(t *testing.T) {
resetSystem()
defer resetSystem()

assert.False(t, Has("test"))
assertTrue(t, !Has("test"))

globalState.channels["test"] = append(globalState.channels["test"], make(chan Message))
assert.True(t, Has("test"))
assertTrue(t, Has("test"))
}

// TestList Checking the correct receipt of the list of events on which handlers are installed
func TestList(t *testing.T) {
resetSystem()
defer resetSystem()

assert.Empty(t, List())
assertTrue(t, len(List()) == 0)

globalState.channels["test"] = append(globalState.channels["test"], make(chan Message), make(chan Message))
globalState.channels["test2"] = append(globalState.channels["test2"], make(chan Message))

actualList := List()
sort.Strings(actualList)

assert.Equal(t, 2, len(actualList))
assert.Equal(t, []string{"test", "test2"}, actualList)
assertTrue(t, len(actualList) == 2)
assertTrue(t, actualList[0] == "test")
assertTrue(t, actualList[1] == "test2")
}

// TestWait Checking Wait function
func TestWait(t *testing.T) {
resetSystem()
defer resetSystem()

eventName := "test"
Expand All @@ -136,16 +135,18 @@ func TestWait(t *testing.T) {
time.Sleep(time.Millisecond)
atomic.StoreInt32(&wasRunning, 1)
})
require.NoError(t, Ring(eventName, nil))
assertNoError(t, Ring(eventName, nil))

Wait()

assert.Equal(t, int32(1), wasRunning)
assertTrue(t, wasRunning == 1)
}

// TestQueue checking function for set queue size
func TestQueue(t *testing.T) {
defer resetSystem()

var size uint = 6
Queue(size)
assert.Equal(t, size, globalState.queueSize)
assertTrue(t, size == globalState.queueSize)
}
8 changes: 0 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
module github.com/nuttech/bell/v2

go 1.17

require github.com/stretchr/testify v1.7.0

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
11 changes: 0 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 57a2176

Please sign in to comment.