-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing.go
50 lines (39 loc) · 938 Bytes
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package sg
import (
"errors"
"sync"
)
// TestingClient is a client that aggregates the sent mails in it's inbox. Use
// it to introspect mail during testing.
type TestingClient struct {
Inbox []*Mail
mu sync.Mutex
}
// Send sends a transactional mail to the testing client inbox field.
func (c *TestingClient) Send(mail *Mail) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Inbox = append(c.Inbox, mail)
return nil
}
// Last returns the last sent email.
func (c *TestingClient) Last() (*Mail, error) {
c.mu.Lock()
defer c.mu.Unlock()
if len(c.Inbox) == 0 {
return nil, errors.New("sg: inbox is empty")
}
return c.Inbox[len(c.Inbox)-1], nil
}
// Empty empties the client inbox.
func (c *TestingClient) Empty() {
c.mu.Lock()
defer c.mu.Unlock()
if c != nil {
c.Inbox = make([]*Mail, 0)
}
}
// NewTestingClient creates a new testing client.
func NewTestingClient() *TestingClient {
return new(TestingClient)
}