From 6fe1bcf384a34fdfc1704ff98ee5151a75d031a2 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 5 Jul 2017 19:45:56 +0200 Subject: pkg/email: add AddAddrContext/RemoveAddrContext Replace extractBugID function with more general AddAddrContext/RemoveAddrContext. --- pkg/email/parser_test.go | 81 +++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 32 deletions(-) (limited to 'pkg/email/parser_test.go') diff --git a/pkg/email/parser_test.go b/pkg/email/parser_test.go index 7b2aa9a0f..d05a597be 100644 --- a/pkg/email/parser_test.go +++ b/pkg/email/parser_test.go @@ -23,16 +23,55 @@ func TestExtractCommand(t *testing.T) { } } -func TestExtractBugID(t *testing.T) { - for i, test := range extractBugIDTests { - t.Run(fmt.Sprint(i), func(t *testing.T) { - bugID, own := extractBugID(test.email, `"Foo Bar" `) - if bugID != test.bugID || own != test.own { - t.Logf("expect: own=%v %q", test.own, test.bugID) - t.Logf("got : own=%v %q", test.own, bugID) - t.Fail() - } - }) +func TestAddRemoveAddrContext(t *testing.T) { + email := `"Foo Bar" ` + email00, context00, err := RemoveAddrContext(email) + if err != nil { + t.Fatal(err) + } + if email != email00 { + t.Fatalf("want: %q, got %q", email, email00) + } + if context00 != "" { + t.Fatalf("want context: %q, got %q", "", context00) + } + context1 := "context1" + email1, err := AddAddrContext(email, context1) + if err != nil { + t.Fatal(err) + } + want1 := `"Foo Bar" ` + if want1 != email1 { + t.Fatalf("want: %q, got %q", want1, email1) + } + context2 := "context2" + email2, err := AddAddrContext(email1, context2) + if err != nil { + t.Fatal(err) + } + want2 := `"Foo Bar" ` + if want2 != email2 { + t.Fatalf("want: %q, got %q", want2, email2) + } + email1, context20, err := RemoveAddrContext(email2) + if err != nil { + t.Fatal(err) + } + if want1 != email1 { + t.Fatalf("want: %q, got %q", want1, email1) + } + if context2 != context20 { + t.Fatalf("want context: %q, got %q", context2, context20) + } + email0, context10, err := RemoveAddrContext(email1) + if err != nil { + t.Fatal(err) + } + if email != email0 { + t.Fatalf("want: %q, got %q", email, email0) + } + if context1 != context10 { + t.Fatalf("want context: %q, got %q", context1, context10) } } @@ -86,28 +125,6 @@ line 2 }, } -var extractBugIDTests = []struct { - email string - bugID string - own bool -}{ - { - `foo@bar.com`, - ``, - true, - }, - { - `foo+123@baz.com`, - ``, - false, - }, - { - `foo+123@bar.com`, - `123`, - true, - }, -} - var parseTests = []struct { email string res *Email -- cgit mrf-deployment