|
| 1 | +package gmailcontent |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestLooksLikeHTML_DocumentPrefixes(t *testing.T) { |
| 6 | + cases := []struct { |
| 7 | + name string |
| 8 | + html string |
| 9 | + }{ |
| 10 | + {"doctype", `<!doctype html><html><body>Hi</body></html>`}, |
| 11 | + {"html", `<html><body>Hi</body></html>`}, |
| 12 | + {"head", `<head><title>X</title></head>`}, |
| 13 | + {"body", `<body>Hi</body>`}, |
| 14 | + {"meta", `<meta charset="utf-8">`}, |
| 15 | + {"html-contained", `Hello <html> world`}, |
| 16 | + } |
| 17 | + for _, tc := range cases { |
| 18 | + t.Run(tc.name, func(t *testing.T) { |
| 19 | + if !LooksLikeHTML(tc.html) { |
| 20 | + t.Fatalf("expected true for %s", tc.name) |
| 21 | + } |
| 22 | + }) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestLooksLikeHTMLFragment_FragmentMarkers(t *testing.T) { |
| 27 | + cases := []struct { |
| 28 | + name string |
| 29 | + html string |
| 30 | + }{ |
| 31 | + {"style-block", `<style type="text/css">a { color: red; }</style><table><tr><td>Hi</td></tr></table>`}, |
| 32 | + {"table", `<table><tr><td>Hi</td></tr></table>`}, |
| 33 | + {"div", `<div>Hello</div>`}, |
| 34 | + {"p", `<p>Hello</p>`}, |
| 35 | + {"br", `<br>`}, |
| 36 | + {"span", `<span>text</span>`}, |
| 37 | + {"section", `<section>content</section>`}, |
| 38 | + {"blockquote", `<blockquote>quoted</blockquote>`}, |
| 39 | + {"anchor", `<a href="https://example.com">link</a>`}, |
| 40 | + {"img", `<img src="x.png" alt="x">`}, |
| 41 | + {"font", `<font face="Arial">text</font>`}, |
| 42 | + {"comment", `<!-- comment --><div>x</div>`}, |
| 43 | + } |
| 44 | + for _, tc := range cases { |
| 45 | + t.Run(tc.name, func(t *testing.T) { |
| 46 | + if !LooksLikeHTMLFragment(tc.html) { |
| 47 | + t.Fatalf("expected true for fragment %s: %q", tc.name, tc.html) |
| 48 | + } |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestLooksLikeHTML_PlainText(t *testing.T) { |
| 54 | + cases := []struct { |
| 55 | + name string |
| 56 | + text string |
| 57 | + }{ |
| 58 | + {"empty", ""}, |
| 59 | + {"whitespace", " "}, |
| 60 | + {"plain", "Best,\nMartin Kessler"}, |
| 61 | + {"url", "see <https://example.com> for details"}, |
| 62 | + {"less-than", "a < b"}, |
| 63 | + {"angle-path", "<path>"}, |
| 64 | + {"angle-project", "<project>"}, |
| 65 | + { "angle-email", "<[email protected]>"}, |
| 66 | + {"angle-private", "<PRIVATE_PERSON>"}, |
| 67 | + {"angle-in-sentence", "see <path> for details"}, |
| 68 | + {"angle-name", "Best,\n<PRIVATE_PERSON>"}, |
| 69 | + {"angle-multiword", "the <quick brown> fox"}, |
| 70 | + { "angle-a-email", "<[email protected]>"}, |
| 71 | + { "angle-b-email", "<[email protected]>"}, |
| 72 | + { "angle-i-email", "<[email protected]>"}, |
| 73 | + { "angle-p-email", "<[email protected]>"}, |
| 74 | + {"angle-div-class", "<div.class>"}, |
| 75 | + {"angle-a-colon", "<a:hello>"}, |
| 76 | + {"angle-b-dot", "<b.foo>"}, |
| 77 | + } |
| 78 | + for _, tc := range cases { |
| 79 | + t.Run(tc.name, func(t *testing.T) { |
| 80 | + if LooksLikeHTMLFragment(tc.text) { |
| 81 | + t.Fatalf("expected false for %s: %q", tc.name, tc.text) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestLooksLikeHTML_DoesNotBroadenToFragments(t *testing.T) { |
| 88 | + t.Parallel() |
| 89 | + |
| 90 | + for _, value := range []string{"<style>p { color: red }</style>", "<table><tr><td>Hi</td></tr></table>", "<p>Hi</p>"} { |
| 91 | + if LooksLikeHTML(value) { |
| 92 | + t.Errorf("LooksLikeHTML(%q) = true, want legacy document-only classification", value) |
| 93 | + } |
| 94 | + |
| 95 | + if !LooksLikeHTMLFragment(value) { |
| 96 | + t.Errorf("LooksLikeHTMLFragment(%q) = false, want true", value) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments