package addressparsing_test import ( "fmt" "strings" "testing" "pkg.jfrech.com/brief/internal/addressparsing" ) func HelperFixQuotesAndCommentsPretty(s string) string { buf := make([]byte, len(s)) for j := range buf { switch s[j] { case '"': buf[j] = 'Q' case '(': buf[j] = 'L' case ')': buf[j] = 'R' case '\\': buf[j] = 'B' default: buf[j] = '.' } } return string(buf) } type CaseFixQuotesAndComments struct { S string Fix string } func CasesFixQuotesAndComments() []CaseFixQuotesAndComments { return []CaseFixQuotesAndComments{ CaseFixQuotesAndComments{"", ""}, CaseFixQuotesAndComments{"))()", ""}, CaseFixQuotesAndComments{"))()(", ")"}, CaseFixQuotesAndComments{"))()(<(", "))"}, CaseFixQuotesAndComments{")))", ""}, CaseFixQuotesAndComments{"<<{(", ")"}, CaseFixQuotesAndComments{"Hallo, du da.", ""}, CaseFixQuotesAndComments{"Hallo, du( da.", ")"}, CaseFixQuotesAndComments{` " \" " `, ``,}, CaseFixQuotesAndComments{` " \" `, `"`,}, CaseFixQuotesAndComments{`" "`, ``,}, CaseFixQuotesAndComments{`" \"`, `"`,}, CaseFixQuotesAndComments{`"`, `"`,}, CaseFixQuotesAndComments{`("("`, `)`,}, CaseFixQuotesAndComments{`("(`, `")`,}, CaseFixQuotesAndComments{`(((")"`, `)))`,}, CaseFixQuotesAndComments{`(`, `)`,}, CaseFixQuotesAndComments{`a "`, `"`}, CaseFixQuotesAndComments{`a (""`, `)`}, CaseFixQuotesAndComments{`a ("\`, `\")`}, CaseFixQuotesAndComments{`a ("`, `")`}, CaseFixQuotesAndComments{`wakka <"<"@!>`, ""}, CaseFixQuotesAndComments{`wakka <"<"@!">`, "\""}, CaseFixQuotesAndComments{"\"\\", "\\\""}, CaseFixQuotesAndComments{"\"\\\\", "\""}, } } func TestFixQuotesAndComments(t *testing.T) { for j, tc := range CasesFixQuotesAndComments() { s := addressparsing.FixQuotesAndComments(tc.S) if s != tc.S + tc.Fix { pretty := HelperFixQuotesAndCommentsPretty b := new(strings.Builder) fmt.Fprintf(b, " tc.S ............................................=%q\n", tc.S) fmt.Fprintf(b, " addressparsing.FixQuotesAndComments(tc.S) .......=%q\n", s) fmt.Fprintf(b, " tc.S+tc.Fix .....................................=%q\n", tc.S+tc.Fix) fmt.Fprintf(b, " \n") fmt.Fprintf(b, " pretty(tc.S) ....................................=%q\n", pretty(tc.S)) fmt.Fprintf(b, " pretty(addressparsing.FixQuotesAndComments(tc.S))=%q\n", pretty(s)) fmt.Fprintf(b, " pretty(tc.S+tc.Fix) .............................=%q\n", pretty(tc.S+tc.Fix)) t.Errorf("CaseFixQuotes()[%d]:\n%s", j, b.String()) } } } func FuzzFixQuotesAndCommentsIdempotency(f *testing.F) { for _, tc := range CasesFixQuotesAndComments() { f.Add(tc.S) f.Add(tc.S + tc.Fix) } f.Fuzz(func(t *testing.T, s0 string) { s1 := addressparsing.FixQuotesAndComments(s0) s2 := addressparsing.FixQuotesAndComments(s1) if s1 != s2 { pretty := HelperFixQuotesAndCommentsPretty t.Errorf("idempotency contract violated:\n s0=%q,\n s1=%q,\n s2=%q,\n pretty(s0)=%q,\n pretty(s1)=%q,\n pretty(s2)=%q", s0, s1, s2, pretty(s0), pretty(s1), pretty(s2)) } }) } // // BUG(jfrech): 2024-06-12: Insufficient test. func TestContainsLiveAt(t *testing.T) { for s, is := range map[string]bool{ "": false, "@valid": true, "valid@": true, "\"@\"": false, "@\"@\"": true, "@@": true, } { if addressparsing.ContainsLiveAt(s) != is { t.Errorf("addressparsing.ContainsLiveAt(%q) == %t != %t", s, !is, is) } } }