1
0
mirror of https://github.com/nikoksr/notify.git synced 2024-11-24 08:22:18 +02:00

test: fill gaps in library root

This commit is contained in:
Niko Köser 2022-04-26 01:17:52 +02:00
parent 23b572dfb2
commit 0dac5dc6af
No known key found for this signature in database
GPG Key ID: F3F28C118DAA6375
2 changed files with 40 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package notify
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
@ -97,3 +98,21 @@ func TestNewWithServices(t *testing.T) {
}
}
}
func TestGlobal(t *testing.T) {
t.Parallel()
ctx := context.Background()
if err := Send(ctx, "subject", "message"); err != nil {
t.Errorf("Send() with no receivers returned error: %v", err)
}
UseServices(mail.New("", ""), nil)
if len(std.notifiers) != 1 {
t.Errorf("UseServices(mail.New()) was expected to have 1 notifier but had %d", len(std.notifiers))
}
if err := Send(ctx, "subject", "message"); err == nil {
t.Error("Send() with invalid mail returned no error")
}
}

View File

@ -38,4 +38,25 @@ func TestNotifySend(t *testing.T) {
if err := n.Send(ctx, "subject", "message"); err == nil {
t.Errorf("Send() invalid mail returned no error: %v", err)
}
// After disabling the Notifier, Send() should return silently.
n.WithOptions(Disable)
if err := n.Send(ctx, "subject", "message"); err != nil {
t.Errorf("Send() of disabled Notifier returned error: %v", err)
}
n.WithOptions(Enable)
// Smuggle in a nil service. This usually never happens, since UseServices filters out nil services. But, it's good
// to test anyway.
n.notifiers = make([]Notifier, 0)
n.notifiers = append(n.notifiers, nil)
if err := n.Send(ctx, "subject", "message"); err != nil {
t.Errorf("Send() of disabled Notifier returned no error: %v", err)
}
if r := recover(); r != nil {
t.Errorf("Send() with nil service panicked: %v", r)
}
}