From 0e82190a469888c14b9c44e8e912fbf8cc602669 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 8 Jun 2020 22:54:53 -0400 Subject: [PATCH] Clean up tests to use `if want, got := X, Y; want != got {`. This reduces a lot of duplictaive expressions in tests. --- pop3/conn_test.go | 38 ++++++++++------------ pop3_test.go | 38 +++++++++++----------- smtp/conn_test.go | 41 ++++++++++++------------ smtp/relay_test.go | 78 ++++++++++++++++++++-------------------------- 4 files changed, 89 insertions(+), 106 deletions(-) diff --git a/pop3/conn_test.go b/pop3/conn_test.go index 21e45aa..cb6fc2a 100644 --- a/pop3/conn_test.go +++ b/pop3/conn_test.go @@ -197,26 +197,22 @@ func TestExampleSession(t *testing.T) { responseOK(t, conn) ok(t, conn.PrintfLine("STAT")) - line = responseOK(t, conn) - expected := "+OK 2 320" - if line != expected { - t.Errorf("STAT expected %q, got %q", expected, line) + if want, got := "+OK 2 320", responseOK(t, conn); want != got { + t.Errorf("STAT want %q, got %q", want, got) } ok(t, conn.PrintfLine("LIST")) responseOK(t, conn) lines, err := conn.ReadDotLines() ok(t, err) - if len(lines) != 2 { - t.Errorf("LIST expected 2 lines, got %d", len(lines)) + if want, got := 2, len(lines); want != got { + t.Errorf("LIST want %d lines, got %d", want, got) } - expected = "1 120" - if lines[0] != expected { - t.Errorf("LIST line 0 expected %q, got %q", expected, lines[0]) + if want, got := "1 120", lines[0]; want != got { + t.Errorf("LIST line 0 want %q, got %q", want, got) } - expected = "2 200" - if lines[1] != expected { - t.Errorf("LIST line 1 expected %q, got %q", expected, lines[1]) + if want, got := "2 200", lines[1]; want != got { + t.Errorf("LIST line 1 expected %q, got %q", want, got) } ok(t, conn.PrintfLine("QUIT")) @@ -334,9 +330,9 @@ func TestRetr(t *testing.T) { return "" } - expected := []string{"hello"} - if !reflect.DeepEqual(resp, expected) { - t.Errorf("Expected %v, got %v", expected, resp) + want := []string{"hello"} + if !reflect.DeepEqual(resp, want) { + t.Errorf("Want %v, got %v", want, resp) } return "" @@ -353,9 +349,9 @@ func TestRetr(t *testing.T) { return "" } - expected := []string{"this", "is a", ".", "test"} - if !reflect.DeepEqual(resp, expected) { - t.Errorf("Expected %v, got %v", expected, resp) + want := []string{"this", "is a", ".", "test"} + if !reflect.DeepEqual(resp, want) { + t.Errorf("Want %v, got %v", want, resp) } return "" @@ -385,12 +381,12 @@ func TestUidl(t *testing.T) { return "" } - expected := []string{ + want := []string{ fmt.Sprintf("1 %p", s.mb.msgs[1]), fmt.Sprintf("3 %p", s.mb.msgs[3]), } - if !reflect.DeepEqual(resp, expected) { - t.Errorf("Expected %v, got %v", expected, resp) + if !reflect.DeepEqual(resp, want) { + t.Errorf("Want %v, got %v", want, resp) } return "" diff --git a/pop3_test.go b/pop3_test.go index 9567748..2ec1f52 100644 --- a/pop3_test.go +++ b/pop3_test.go @@ -86,9 +86,9 @@ func TestOpenMailboxAuth(t *testing.T) { } for i, c := range cases { mb, err := s.OpenMailbox(c.user, c.pass) - actual := (mb != nil && err == nil) - if actual != c.ok { - t.Errorf("Expected error=%v for case %d (%#v), got %v (error=%v, mb=%v)", c.ok, i, c, actual, err, mb) + got := (mb != nil && err == nil) + if got != c.ok { + t.Errorf("Expected error=%v for case %d (%#v), got %v (error=%v, mb=%v)", c.ok, i, c, got, err, mb) } } } @@ -125,7 +125,7 @@ func TestBasicListener(t *testing.T) { _, err = conn.ReadLine() if err != nil { - t.Errorf("Failed to read line: %v\n", err) + t.Errorf("Failed to read line: %v", err) return } } @@ -192,8 +192,8 @@ func TestMailbox(t *testing.T) { t.Errorf("Failed to list messages: %v", err) } - if len(msgs) != 2 { - t.Errorf("Expected 2 messages, got %d", len(msgs)) + if want, got := 2, len(msgs); want != got { + t.Errorf("Want %d messages, got %d", want, got) } if mb.GetMessage(0) != nil { @@ -207,20 +207,18 @@ func TestMailbox(t *testing.T) { t.Errorf("Failed to look up message by ID") } - if msgs[0].UniqueID() != "a" { - t.Errorf("Expected message #1 unique ID to be a, got %s", msgs[0].UniqueID()) + if want, got := "a", msgs[0].UniqueID(); want != got { + t.Errorf("Want message #1 unique ID to be %s, got %s", want, got) } - expectedSize := 1024 * 10 - if msgs[0].Size() != expectedSize { - t.Errorf("Expected message #1 size to be %v, got %v", expectedSize, msgs[0].Size()) + if want, got := 1024*10, msgs[0].Size(); want != got { + t.Errorf("Want message #1 size to be %v, got %v", want, got) } - if msgs[1].UniqueID() != "b" { - t.Errorf("Expected message #2 unique ID to be b, got %s", msgs[0].UniqueID()) + if want, got := "b", msgs[1].UniqueID(); want != got { + t.Errorf("Want message #2 unique ID to be %s, got %s", want, got) } - expectedSize = 1024 * 3 - if msgs[1].Size() != expectedSize { - t.Errorf("Expected message #2 size to be %v, got %v", expectedSize, msgs[0].Size()) + if want, got := 1024*3, msgs[1].Size(); want != got { + t.Errorf("Want message #2 size to be %v, got %v", want, got) } // Test message contents. @@ -267,11 +265,11 @@ func TestMailbox(t *testing.T) { t.Errorf("Failed to list messages: %v", err) } - if len(msgs) != 1 { - t.Errorf("Number of messages should be 1, got %d", len(msgs)) + if want, got := 1, len(msgs); want != got { + t.Errorf("Number of messages should be %d, got %d", want, got) } - if msgs[0].UniqueID() != "b" { - t.Errorf("Message Unique ID should be b, got %s", msgs[0].UniqueID()) + if want, got := "b", msgs[0].UniqueID(); want != got { + t.Errorf("Message Unique ID should be %s, got %s", want, got) } } diff --git a/smtp/conn_test.go b/smtp/conn_test.go index e1e1501..0dc8263 100644 --- a/smtp/conn_test.go +++ b/smtp/conn_test.go @@ -300,15 +300,14 @@ func TestGetReceivedInfo(t *testing.T) { actual := conn.getReceivedInfo(envelope) actualLines := strings.SplitAfter(string(actual), crlf) - if len(actualLines) != len(test.expect) { - t.Errorf("wrong numbber of lines, expected %d, got %d", len(test.expect), len(actualLines)) + if want, got := len(test.expect), len(actualLines); want != got { + t.Errorf("wrong numbber of lines, want %d, got %d", want, got) continue } for i, line := range actualLines { - expect := test.expect[i] - if expect != strings.TrimLeft(line, " ") { - t.Errorf("Expected equal string %q, got %q", expect, line) + if want, got := test.expect[i], strings.TrimLeft(line, " "); want != got { + t.Errorf("want equal string %q, got %q", want, got) } } } @@ -497,8 +496,8 @@ func TestBasicRelay(t *testing.T) { }}, }) - if len(server.relayed) != 1 { - t.Errorf("Expected 1 relayed message, got %d", len(server.relayed)) + if want, got := 1, len(server.relayed); want != got { + t.Errorf("Want %d relayed message, got %d", want, got) } } @@ -521,23 +520,23 @@ func TestSendAsRelay(t *testing.T) { }}, }) - if len(server.relayed) != 1 { - t.Fatalf("Expected 1 relayed message, got %d", len(server.relayed)) + if want, got := 1, len(server.relayed); want != got { + t.Fatalf("Want %d relayed message, got %d", want, got) } replaced := "source@example.com" original := "mailbox@example.com" en := server.relayed[0] - if en.MailFrom.Address != replaced { - t.Errorf("Expected mail to be from %q, got %q", replaced, en.MailFrom.Address) + if want, got := replaced, en.MailFrom.Address; want != got { + t.Errorf("Want mail to be from %q, got %q", want, got) } - if len(en.RcptTo) != 1 { - t.Errorf("Expected 1 recipient, got %d", len(en.RcptTo)) + if want, got := 1, len(en.RcptTo); want != got { + t.Errorf("Want %d recipient, got %d", want, got) } - if en.RcptTo[0].Address != "valid@dest.xyz" { - t.Errorf("Unexpected RcptTo %q", en.RcptTo[0].Address) + if want, got := "valid@dest.xyz", en.RcptTo[0].Address; want != got { + t.Errorf("Unexpected RcptTo %q", got) } msg := string(en.Data) @@ -583,15 +582,15 @@ func TestSendMultipleRelay(t *testing.T) { original := "mailbox@example.com" en := server.relayed[0] - if en.MailFrom.Address != replaced { - t.Errorf("Expected mail to be from %q, got %q", replaced, en.MailFrom.Address) + if want, got := replaced, en.MailFrom.Address; want != got { + t.Errorf("Want mail to be from %q, got %q", want, got) } - if len(en.RcptTo) != 2 { - t.Errorf("Expected 2 recipient, got %d", len(en.RcptTo)) + if want, got := 2, len(en.RcptTo); want != got { + t.Errorf("Want %d recipients, got %d", want, got) } - if en.RcptTo[0].Address != "valid@dest.xyz" { - t.Errorf("Unexpected RcptTo %q", en.RcptTo[0].Address) + if want, got := "valid@dest.xyz", en.RcptTo[0].Address; want != got { + t.Errorf("Unexpected RcptTo %q", got) } msg := string(en.Data) diff --git a/smtp/relay_test.go b/smtp/relay_test.go index 6ad6a08..cbced01 100644 --- a/smtp/relay_test.go +++ b/smtp/relay_test.go @@ -47,22 +47,22 @@ func TestRelayRoundTrip(t *testing.T) { host, port, _ := net.SplitHostPort(l.Addr().String()) relayMessageToHost(s, env, zap.NewNop(), env.RcptTo[0].Address, host, port) - if len(s.messages) != 1 { - t.Errorf("Expected 1 message to be delivered, got %d", len(s.messages)) + if want, got := 1, len(s.messages); want != got { + t.Errorf("Want %d message to be delivered, got %d", want, got) return } received := s.messages[0] - if env.MailFrom.Address != received.MailFrom.Address { - t.Errorf("Expected MailFrom %s, got %s", env.MailFrom.Address, received.MailFrom.Address) + if want, got := env.MailFrom.Address, received.MailFrom.Address; want != got { + t.Errorf("Want MailFrom %s, got %s", want, got) } - if len(received.RcptTo) != 1 { - t.Errorf("Expected 1 RcptTo, got %d", len(received.RcptTo)) + if want, got := 1, len(received.RcptTo); want != got { + t.Errorf("Want %d RcptTo, got %d", want, got) return } - if env.RcptTo[0].Address != received.RcptTo[0].Address { - t.Errorf("Expected RcptTo %s, got %s", env.RcptTo[0].Address, received.RcptTo[0].Address) + if want, got := env.RcptTo[0].Address, received.RcptTo[0].Address; want != got { + t.Errorf("Want RcptTo %s, got %s", want, got) } if !bytes.HasSuffix(received.Data, env.Data) { @@ -86,15 +86,15 @@ func TestDeliveryFailureMessage(t *testing.T) { errorStr2 := "general error 122" deliverRelayFailure(s, env, zap.NewNop(), env.RcptTo[0].Address, errorStr1, fmt.Errorf(errorStr2)) - if len(s.messages) != 1 { - t.Errorf("Expected 1 failure notification, got %d", len(s.messages)) + if want, got := 1, len(s.messages); want != got { + t.Errorf("Want %d failure notification, got %d", want, got) return } failure := s.messages[0] - if failure.RcptTo[0].Address != env.MailFrom.Address { - t.Errorf("Failure message should be delivered to sender %s, actually %s", env.MailFrom.Address, failure.RcptTo[0].Address) + if want, got := env.MailFrom.Address, failure.RcptTo[0].Address; want != got { + t.Errorf("Failure message should be delivered to sender %s, actually %s", want, got) } // Read the failure message. @@ -112,25 +112,22 @@ func TestDeliveryFailureMessage(t *testing.T) { return } - expected := "multipart/report" - if mediatype != expected { - t.Errorf("Expected MIME type of %q, got %q", expected, mediatype) + if want, got := "multipart/report", mediatype; want != got { + t.Errorf("Want MIME type of %q, got %q", want, got) } - expected = "delivery-status" - if mtheaders["report-type"] != expected { - t.Errorf("Expected report-type of %q, got %q", expected, mtheaders["report-type"]) + if want, got := "delivery-status", mtheaders["report-type"]; want != got { + t.Errorf("Want report-type of %q, got %q", want, got) } boundary := mtheaders["boundary"] - expected = "Delivery Status Notification (Failure)" - if msg.Header["Subject"][0] != expected { - t.Errorf("Subject did not match %q, got %q", expected, mtheaders["Subject"]) + if want, got := "Delivery Status Notification (Failure)", msg.Header["Subject"][0]; want != got { + t.Errorf("Want Subject field %q, got %q", want, got) } - if msg.Header["To"][0] != "<"+env.MailFrom.Address+">" { - t.Errorf("To field does not match %q, got %q", env.MailFrom.Address, msg.Header["To"][0]) + if want, got := "<"+env.MailFrom.Address+">", msg.Header["To"][0]; want != got { + t.Errorf("Want To field %q, got %q", want, got) } // Parse the multi-part messsage. @@ -142,9 +139,8 @@ func TestDeliveryFailureMessage(t *testing.T) { } // First part is the human-readable error. - expected = "text/plain; charset=UTF-8" - if part.Header["Content-Type"][0] != expected { - t.Errorf("Part 0 type expected %q, got %q", expected, part.Header["Content-Type"][0]) + if want, got := "text/plain; charset=UTF-8", part.Header["Content-Type"][0]; want != got { + t.Errorf("Part 0 type want %q, got %q", want, got) } content, err := ioutil.ReadAll(part) @@ -158,9 +154,8 @@ func TestDeliveryFailureMessage(t *testing.T) { t.Errorf("Missing Delivery Failure") } - expected = fmt.Sprintf("%s:\n%s", errorStr1, errorStr2) - if !strings.Contains(contentStr, expected) { - t.Errorf("Missing error string %q", expected) + if want := fmt.Sprintf("%s:\n%s", errorStr1, errorStr2); !strings.Contains(contentStr, want) { + t.Errorf("Missing error string %q", want) } // Second part is the status information. @@ -170,9 +165,8 @@ func TestDeliveryFailureMessage(t *testing.T) { return } - expected = "message/delivery-status" - if part.Header["Content-Type"][0] != expected { - t.Errorf("Part 1 type expected %q, got %q", expected, part.Header["Content-Type"][0]) + if want, got := "message/delivery-status", part.Header["Content-Type"][0]; want != got { + t.Errorf("Part 1 type want %q, got %q", want, got) } content, err = ioutil.ReadAll(part) @@ -182,19 +176,16 @@ func TestDeliveryFailureMessage(t *testing.T) { } contentStr = string(content) - expected = "Original-Envelope-ID: " + env.ID + "\n" - if !strings.Contains(contentStr, expected) { - t.Errorf("Missing %q in %q", expected, contentStr) + if want := "Original-Envelope-ID: " + env.ID + "\n"; !strings.Contains(contentStr, want) { + t.Errorf("Missing %q in %q", want, contentStr) } - expected = "Reporting-UA: " + env.EHLO + "\n" - if !strings.Contains(contentStr, expected) { - t.Errorf("Missing %q in %q", expected, contentStr) + if want := "Reporting-UA: " + env.EHLO + "\n"; !strings.Contains(contentStr, want) { + t.Errorf("Missing %q in %q", want, contentStr) } - expected = "Reporting-MTA: dns; localhost [127.0.0.1]\n" - if !strings.Contains(contentStr, expected) { - t.Errorf("Missing %q in %q", expected, contentStr) + if want := "Reporting-MTA: dns; localhost [127.0.0.1]\n"; !strings.Contains(contentStr, want) { + t.Errorf("Missing %q in %q", want, contentStr) } // Third part is the original message. @@ -204,9 +195,8 @@ func TestDeliveryFailureMessage(t *testing.T) { return } - expected = "message/rfc822" - if part.Header["Content-Type"][0] != expected { - t.Errorf("Part 2 type expected %q, got %q", expected, part.Header["Content-Type"][0]) + if want, got := "message/rfc822", part.Header["Content-Type"][0]; want != got { + t.Errorf("Part 2 type want %q, got %q", want, got) } content, err = ioutil.ReadAll(part) -- 2.43.5