|
| 1 | +package mail_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/smtp" |
| 5 | + "testing" |
| 6 | + |
| 7 | + mail "github.com/GoProgrammingFromBeginnerToMaster/chapter5/sources/send_mail_with_disclaimer2/v2" |
| 8 | +) |
| 9 | + |
| 10 | +type FakeMailSender struct { |
| 11 | + subject string |
| 12 | + from string |
| 13 | + to []string |
| 14 | + bcc []string |
| 15 | + cc []string |
| 16 | + text string |
| 17 | + server string |
| 18 | + a smtp.Auth |
| 19 | +} |
| 20 | + |
| 21 | +func (s *FakeMailSender) Send(subject, from string, to, bcc, cc []string, text, server string, a smtp.Auth) error { |
| 22 | + s.subject = subject |
| 23 | + s.from = from |
| 24 | + s.to = to |
| 25 | + s.bcc = bcc |
| 26 | + s.cc = cc |
| 27 | + s.text = text |
| 28 | + s.server = server |
| 29 | + s.a = a |
| 30 | + return nil |
| 31 | +} |
| 32 | + |
| 33 | +func TestSendMailWithDisclaimer(t *testing.T) { |
| 34 | + type args struct { |
| 35 | + sender mail.MailSender |
| 36 | + subject string |
| 37 | + from string |
| 38 | + to []string |
| 39 | + bcc []string |
| 40 | + cc []string |
| 41 | + text string |
| 42 | + server string |
| 43 | + a smtp.Auth |
| 44 | + } |
| 45 | + tests := []struct { |
| 46 | + name string |
| 47 | + args args |
| 48 | + wantErr bool |
| 49 | + }{ |
| 50 | + // TODO: Add test cases. |
| 51 | + {"base case", |
| 52 | + args{ &FakeMailSender{}, "subject", "[email protected]", [] string{ "[email protected]"}, [] string{ "[email protected]"}, [] string{ "[email protected]"}, "text11", "smtp.gmail.com:587", smtp. PlainAuth( "", "[email protected]", "password123", "smtp.gmail.com")}, |
| 53 | + false, |
| 54 | + }, |
| 55 | + } |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + if err := mail.SendMailWithDisclaimer(tt.args.sender, tt.args.subject, tt.args.from, tt.args.to, tt.args.bcc, tt.args.cc, tt.args.text, tt.args.server, tt.args.a); (err != nil) != tt.wantErr { |
| 59 | + t.Errorf("SendMailWithDisclaimer() error = %v, wantErr %v", err, tt.wantErr) |
| 60 | + } |
| 61 | + want := tt.args.text + "\n\n" + mail.DISCLAIMER |
| 62 | + got := (tt.args.sender.(*FakeMailSender)).text |
| 63 | + if got != want { |
| 64 | + t.Errorf("want: %s, got: %s", want, got) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestSendMailWithDisclaimerWithoutTableDrivenTest(t *testing.T) { |
| 71 | + sender := &FakeMailSender{} |
| 72 | + text := "hello world" |
| 73 | + err := mail.SendMailWithDisclaimer( |
| 74 | + sender, |
| 75 | + "subject", "[email protected]", [] string{ "[email protected]"}, [] string{ "[email protected]"}, [] string{ "[email protected]"}, text, "smtp.gmail.com:587", smtp. PlainAuth( "", "[email protected]", "password123", "smtp.gmail.com"), |
| 76 | + ) |
| 77 | + if err != nil { |
| 78 | + t.Errorf("SendMailWithDisclaimer() err: %v\n", err) |
| 79 | + } |
| 80 | + want := text + "\n\n" + mail.DISCLAIMER |
| 81 | + got := sender.text |
| 82 | + if want != got { |
| 83 | + t.Errorf("want: %s, got: %s", want, got) |
| 84 | + } |
| 85 | +} |
0 commit comments