From 0dac5dc6af9f71c9c05897a94c6fd2139ca6e3a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20K=C3=B6ser?= Date: Tue, 26 Apr 2022 01:17:52 +0200 Subject: [PATCH] test: fill gaps in library root --- notify_test.go | 19 +++++++++++++++++++ send_test.go | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/notify_test.go b/notify_test.go index b1af06d..c04efca 100644 --- a/notify_test.go +++ b/notify_test.go @@ -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") + } +} diff --git a/send_test.go b/send_test.go index 501b35e..354217a 100644 --- a/send_test.go +++ b/send_test.go @@ -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) + } }