1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-30 10:43:30 +00:00

Fixed unit tests

This commit is contained in:
diamondburned (Forefront) 2020-01-16 21:20:09 -08:00
parent d627690835
commit cfa764b150
2 changed files with 49 additions and 55 deletions

View file

@ -2,11 +2,60 @@ package handler
import (
"reflect"
"strings"
"testing"
"time"
"github.com/diamondburned/arikawa/gateway"
)
func TestCall(t *testing.T) {
var results = make(chan string)
h := &Handler{
handlers: map[uint64]handler{},
}
// Add handler test
rm := h.AddHandler(func(m *gateway.MessageCreateEvent) {
results <- m.Content
})
go h.Call(&gateway.MessageCreateEvent{
Content: "test",
})
if r := <-results; r != "test" {
t.Fatal("Returned results is wrong:", r)
}
// Remove handler test
rm()
go h.Call(&gateway.MessageCreateEvent{
Content: "test",
})
select {
case <-results:
t.Fatal("Unexpected results")
case <-time.After(time.Millisecond):
break
}
// Invalid type test
_, err := h.AddHandlerCheck("this should panic")
if err == nil {
t.Fatal("No errors found")
}
// We don't do anything with the returned callback, as there's none.
if !strings.Contains(err.Error(), "given interface is not a function") {
t.Fatal("Unexpected error:", err)
}
}
func TestHandler(t *testing.T) {
var results = make(chan string)

View file

@ -1,55 +0,0 @@
package session
import (
"strings"
"testing"
"time"
"github.com/diamondburned/arikawa/gateway"
)
func TestSessionCall(t *testing.T) {
var results = make(chan string)
s := &Session{
handlers: map[uint64]handler{},
}
// Add handler test
rm := s.AddHandler(func(m *gateway.MessageCreateEvent) {
results <- m.Content
})
go s.call(&gateway.MessageCreateEvent{
Content: "test",
})
if r := <-results; r != "test" {
t.Fatal("Returned results is wrong:", r)
}
// Remove handler test
rm()
go s.call(&gateway.MessageCreateEvent{
Content: "test",
})
select {
case <-results:
t.Fatal("Unexpected results")
case <-time.After(time.Millisecond):
break
}
// Invalid type test
rm, err := s.AddHandlerCheck("this should panic")
if err == nil {
t.Fatal("No errors found")
}
defer rm()
if !strings.Contains(err.Error(), "given interface is not a function") {
t.Fatal("Unexpected error:", err)
}
}