Skip to content

Commit 44b0e20

Browse files
committed
feat(learning):第30条 使用接口提高代码的可测试性281
Please enter the commit message for your changes. Lines starting
1 parent 8747f4e commit 44b0e20

File tree

11 files changed

+250
-6
lines changed

11 files changed

+250
-6
lines changed

chapter5/sources/horizontal-composition-3.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func bar(w http.ResponseWriter, r *http.Request) {
1414
}
1515

1616
func main() {
17-
http.HandleFunc("/", greetings)
18-
http.HandleFunc("/bar", bar)
19-
http.ListenAndServe(":8080", nil)
20-
// http.ListenAndServe(":8080", http.HandlerFunc(greetings))
17+
// http.HandleFunc("/", greetings)
18+
// http.HandleFunc("/bar", bar)
19+
// http.ListenAndServe(":8080", nil)
20+
http.ListenAndServe(":8080", http.HandlerFunc(greetings))
2121
}

chapter5/sources/send_mail_with_disclaimer/v2/example_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func ExampleSendMailWithDisclaimer() {
2626
e: email.NewEmail(),
2727
}
2828
err := mail.SendMailWithDisclaimer(adapter, "gopher mail test v2",
29-
"YOUR_MAILBOX",
30-
[]string{"DEST_MAILBOX"},
29+
30+
[]string{"[email protected]"},
3131
"hello, gopher",
3232
"smtp.163.com:25",
3333
smtp.PlainAuth("", "YOUR_EMAIL_ACCOUNT", "YOUR_EMAIL_PASSWD!", "smtp.163.com"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/GoProgrammingFromBeginnerToMaster/chapter5/sources/send_mail_with_disclaimer2/v1
2+
3+
go 1.17
4+
5+
require github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible // indirect
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
2+
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package mail
2+
3+
import (
4+
"net/smtp"
5+
6+
"github.com/jordan-wright/email"
7+
)
8+
9+
func attachDisclaimer(s string) string {
10+
return "\n\n" + s
11+
}
12+
13+
func SendMailWithDisclaimer(subject, from string, to, bcc, cc []string, text, server string, a smtp.Auth) error {
14+
e := email.NewEmail()
15+
e.From = from
16+
e.To = to
17+
e.Bcc = bcc
18+
e.Cc = cc
19+
e.Subject = subject
20+
e.Text = []byte(attachDisclaimer(text))
21+
return e.Send(server, a)
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package mail_test
2+
3+
import (
4+
"net/smtp"
5+
"testing"
6+
7+
mail "github.com/GoProgrammingFromBeginnerToMaster/chapter5/sources/send_mail_with_disclaimer2/v1"
8+
)
9+
10+
func TestSendMailWithDisclaimer(t *testing.T) {
11+
type args struct {
12+
subject string
13+
from string
14+
to []string
15+
bcc []string
16+
cc []string
17+
text string
18+
server string
19+
a smtp.Auth
20+
}
21+
tests := []struct {
22+
name string
23+
args args
24+
wantErr bool
25+
}{
26+
// TODO: Add test cases.
27+
{"base case",
28+
args{"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")},
29+
false,
30+
},
31+
}
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
if err := mail.SendMailWithDisclaimer(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 {
35+
t.Errorf("SendMailWithDisclaimer() error = %v, wantErr %v", err, tt.wantErr)
36+
}
37+
})
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
"github.com/jordan-wright/email"
9+
)
10+
11+
type MailSenderAdapter struct {
12+
e *email.Email
13+
}
14+
15+
func (s *MailSenderAdapter) Send(subject, from string, to, bcc, cc []string, text, server string, a smtp.Auth) error {
16+
s.e.Subject = subject
17+
s.e.From = from
18+
s.e.To = to
19+
s.e.Bcc = bcc
20+
s.e.Cc = cc
21+
s.e.Text = []byte(text)
22+
return s.e.Send(server, a)
23+
}
24+
25+
func TestSendMailWithDisclaimerWithMailSenderAdapter(t *testing.T) {
26+
type args struct {
27+
subject string
28+
from string
29+
to []string
30+
bcc []string
31+
cc []string
32+
text string
33+
server string
34+
a smtp.Auth
35+
}
36+
tests := []struct {
37+
name string
38+
s mail.MailSender
39+
args args
40+
wantErr bool
41+
}{
42+
// TODO: Add test cases.
43+
{"base case",
44+
&MailSenderAdapter{e: email.NewEmail()},
45+
args{"subject", "[email protected]", []string{"[email protected]"}, []string{"[email protected]"}, []string{"[email protected]"}, "content", "smtp.163.com:25", smtp.PlainAuth("", "[email protected]", "123", "smtp.163.com")},
46+
false,
47+
},
48+
}
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
if err := mail.SendMailWithDisclaimer(tt.s, 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 {
52+
t.Errorf("SendMailWithDisclaimer() error = %v, wantErr %v", err, tt.wantErr)
53+
}
54+
want := tt.args.text + "\n\n" + mail.DISCLAIMER
55+
got := string((tt.s.(*MailSenderAdapter)).e.Text)
56+
if want != got {
57+
t.Errorf("want: %s, got: %s", want, got)
58+
}
59+
})
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/GoProgrammingFromBeginnerToMaster/chapter5/sources/send_mail_with_disclaimer2/v2
2+
3+
go 1.17
4+
5+
require github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible // indirect
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
2+
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package mail
2+
3+
import "net/smtp"
4+
5+
const (
6+
DISCLAIMER = "disclaimer content here"
7+
)
8+
9+
type MailSender interface {
10+
Send(subject, from string, to, bcc, cc []string, text, server string, a smtp.Auth) error
11+
}
12+
13+
func attachDisclaimer(s string) string {
14+
return s + "\n\n" + DISCLAIMER
15+
}
16+
17+
func SendMailWithDisclaimer(sender MailSender, subject, from string, to, bcc, cc []string, text, server string, a smtp.Auth) error {
18+
return sender.Send(subject, from, to, bcc, cc, attachDisclaimer(text), server, a)
19+
}
20+
21+
// "-->" means dependent on
22+
// SendMailWithDisclaimer --> MailSender --> FakeMailSender
23+
// --> MailSenderAdapter --> "github.com/jordan-wright/email"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)