Skip to content
quarry Artifact [d91a49f3e7]
Login

Artifact [d91a49f3e7]

Artifact d91a49f3e7e6e2bf3d756fa6ee4427f994102fe36e112b4c9f2f21e95ba688e3:


package web

import (
	"html/template"
	"testing"
)

// A note that opens by repeating the check-in's subject as a heading
// says the same words twice, one under the other; the heading goes and
// the rest stays. A heading that says something else is the note's own
// and stays.
func TestDropNoteLead(t *testing.T) {
	cases := []struct {
		name, note, subject, want string
	}{
		{"same words", `<h1 id="a">Fix the  shift key</h1><p>body</p>`, "fix the shift key", `<p>body</p>`},
		{"heading is a prefix of the subject", `<h2>Fix the shift</h2><p>body</p>`, "Fix the shift key firing twice", `<p>body</p>`},
		{"subject is a prefix of the heading", `<h3 id="x">Fix the shift key: design notes</h3><p>body</p>`, "Fix the shift key", `<p>body</p>`},
		{"leading space and nested markup", "\n <h1><a href=\"#x\">Fix</a> the <code>shift</code> key</h1>\n<p>body</p>", "Fix the shift key", "<p>body</p>"},
		{"entities are read", `<h1>Fix &amp; mend</h1><p>body</p>`, "Fix & mend", `<p>body</p>`},
		{"heading ends mid-word", `<h1>Fix the shi</h1><p>body</p>`, "Fix the shift key", `<h1>Fix the shi</h1><p>body</p>`},
		{"another subject", `<h1>Design</h1><p>body</p>`, "Fix the shift key", `<h1>Design</h1><p>body</p>`},
		{"deeper heading", `<h4>Fix the shift key</h4><p>body</p>`, "Fix the shift key", `<h4>Fix the shift key</h4><p>body</p>`},
		{"heading not first", `<p>intro</p><h1>Fix the shift key</h1>`, "Fix the shift key", `<p>intro</p><h1>Fix the shift key</h1>`},
		{"empty heading", `<h1></h1><p>body</p>`, "Fix", `<h1></h1><p>body</p>`},
		{"empty subject", `<h1>Fix</h1><p>body</p>`, "", `<h1>Fix</h1><p>body</p>`},
		{"nothing left is still a note", `<h1>Fix</h1>`, "Fix", ``},
	}
	for _, c := range cases {
		if got := dropNoteLead(template.HTML(c.note), c.subject); string(got) != c.want {
			t.Errorf("FAIL %s: got %q, want %q", c.name, got, c.want)
		}
	}
}