package rowrule
import (
"slices"
"strings"
"testing"
xhtml "golang.org/x/net/html"
"amadan.net/rastrillo/fiesta/internal/markup"
)
// read is markup as Breaches wants it: respelled, then parsed.
func read(t *testing.T, src string) *xhtml.Node {
t.Helper()
respelled, _ := markup.Respell([]byte(src))
doc, err := xhtml.Parse(strings.NewReader(string(respelled)))
if err != nil {
t.Fatalf("parsing %q: %v", src, err)
}
return doc
}
// TestTheRowCheckNamesEachBreach: each way a dropdown can break the
// rule is named by its own message, in both spellings, and each shape
// the rule allows passes. A checker that only counted breaches would
// pass a page that broke the rule one way and was fixed another.
func TestTheRowCheckNamesEachBreach(t *testing.T) {
const header = `
T
`
cases := []struct {
name, markup string
want []string
}{
{"alone at the top of the page, above its title",
`` + header + ``,
[]string{"a dropdown sits in main[rst-page], not in a row", "a dropdown comes before the page header"}},
{"in a bare div",
`
`,
[]string{"a dropdown sits in div, not in a row"}},
{"in a toolbar above the page's title",
`
` + header + ``,
[]string{"a dropdown comes before the page header"}},
{"a page header with no title",
``,
[]string{"a page header holds a dropdown and no h1"}},
{"a section head with no heading",
`
`,
[]string{"a section head holds a dropdown and no heading"}},
{"class-spelled, alone in the body",
``,
[]string{"a dropdown sits in body, not in a row"}},
{"in a form that lays itself out",
``,
[]string{"a dropdown sits in form[rst-search], not in a row"}},
{"a heading inside the menu is not the section's",
`
S
P
`,
[]string{"a section head holds a dropdown and no heading"}},
{"in a toolbar inside a page header with no title",
`
Menu
`,
[]string{"a page header holds a dropdown and no h1"}},
{"class-spelled, in a toolbar inside a page header with no title",
`
Menu
`,
[]string{"a page header holds a dropdown and no h1"}},
{"in a toolbar inside a section head with no heading",
`
Menu
`,
[]string{"a section head holds a dropdown and no heading"}},
{"class-spelled, in a toolbar inside a section head with no heading",
`
Menu
`,
[]string{"a section head holds a dropdown and no heading"}},
{"a class list the respeller cannot read",
``,
[]string{`a class list could not be respelled, so the check cannot read it: `}},
{"escaped markup in a code sample",
`
class="rst-box"
`, nil},
{"in a toolbar inside a page header with its title",
`
T
Menu
`, nil},
{"class-spelled, in a toolbar inside a section head with its heading",
`
P
Menu
`, nil},
{"class-spelled, in a section head with its heading",
`
P
`, nil},
{"in a toolbar, inside a form, after the toolbar's search",
`
` +
`
`, nil},
{"in a toolbar, inside a search's form",
`
`, nil},
{"in the shell's bar, before the page header",
`
` + header + ``, nil},
{"in a menu in the rail's foot, before the page header",
`
`, nil},
{"in the page header beside its title",
`
T
`, nil},
{"in the footer row, after the page header",
`` + header + ``, nil},
}
for _, c := range cases {
if got := Breaches(read(t, c.markup), nil); !slices.Equal(got, c.want) {
t.Errorf("%s:\n got %q\n want %q", c.name, got, c.want)
}
}
}
// TestAPageNamesItsOwnRows: a page that lays out its own bars names
// them by class, and a menu in one of them is in a row. A name a menu's
// row does not carry, or one written as an attribute, is no row: the
// page's names extend Fiesta's rows, they do not loosen the rule for
// the rest of the page.
func TestAPageNamesItsOwnRows(t *testing.T) {
bar := map[string]string{"site-bar": "the site's theme bar"}
cases := []struct {
name, markup string
rows map[string]string
want []string
}{
{"in a row the page names",
`
Theme
`, bar, nil},
{"in a form inside a row the page names",
``, bar, nil},
{"in a div the page does not name",
`
`, bar,
[]string{"a dropdown sits in div, not in a row"}},
{"the name written as an attribute",
`
`, bar,
[]string{"a dropdown sits in div, not in a row"}},
{"a row the page names, with no names passed",
`
`, nil,
[]string{"a dropdown sits in div, not in a row"}},
{"a Fiesta row, beside the page's names",
`
`, bar, nil},
}
for _, c := range cases {
if got := Breaches(read(t, c.markup), c.rows); !slices.Equal(got, c.want) {
t.Errorf("%s:\n got %q\n want %q", c.name, got, c.want)
}
}
}