mirror of
https://github.com/nikoksr/notify.git
synced 2025-03-19 21:08:48 +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:
parent
5c3235363c
commit
bfafa2acb7
@ -66,6 +66,15 @@ func New() *Notify {
|
|||||||
return NewWithOptions()
|
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.
|
// Create the package level Notify instance.
|
||||||
var std = New()
|
var std = New()
|
||||||
|
|
||||||
|
@ -4,6 +4,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
|
||||||
|
"github.com/nikoksr/notify/service/mail"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
@ -64,3 +66,34 @@ func TestDefault(t *testing.T) {
|
|||||||
t.Error("Default() did not return the default Notifier")
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user