1
0
mirror of https://github.com/nikoksr/notify.git synced 2025-03-17 20:57:58 +02:00

feat(notify): Add NewWithServices() constructor function

AddNewWithServices() accepts a variadic list of services and returns a
new, by New() created, Notify instance with the list of services set as
its notifiers.

If no services are given it's the functionally identical to just calling
New().

Calling NewWithServices() with a list of services is functionally equal
to calling New() and then UseServices().
This commit is contained in:
Niko Köser 2022-04-25 23:53:28 +02:00
parent 5c3235363c
commit bfafa2acb7
No known key found for this signature in database
GPG Key ID: F3F28C118DAA6375
2 changed files with 42 additions and 0 deletions

View File

@ -66,6 +66,15 @@ func New() *Notify {
return NewWithOptions()
}
// NewWithServices returns a new instance of Notify with the given services. By default, the Notify instance is enabled.
// If no services are provided, it returns a new Notify instance with default options.
func NewWithServices(services ...Notifier) *Notify {
n := New()
n.UseServices(services...)
return n
}
// Create the package level Notify instance.
var std = New()

View File

@ -4,6 +4,8 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/nikoksr/notify/service/mail"
)
func TestNew(t *testing.T) {
@ -64,3 +66,34 @@ func TestDefault(t *testing.T) {
t.Error("Default() did not return the default Notifier")
}
}
func TestNewWithServices(t *testing.T) {
t.Parallel()
n1 := NewWithServices()
if n1 == nil {
t.Fatal("NewWithServices() returned nil")
}
n2 := NewWithServices(nil)
if n2 == nil {
t.Fatal("NewWithServices(nil) returned nil")
}
if len(n2.notifiers) != 0 {
t.Error("NewWithServices(nil) did not return empty Notifier")
}
mailService := mail.New("", "")
n3 := NewWithServices(mailService)
if n3 == nil {
t.Fatal("NewWithServices(mail.New()) returned nil")
}
if len(n3.notifiers) != 1 {
t.Errorf("NewWithServices(mail.New()) was expected to have 1 notifier but had %d", len(n3.notifiers))
} else {
diff := cmp.Diff(n3.notifiers[0], mailService, cmp.AllowUnexported(mail.Mail{}))
if diff != "" {
t.Errorf("NewWithServices(mail.New()) did not correctly use service:\n%s", diff)
}
}
}