package main
import (
"encoding/json"
"strings"
"testing"
)
func fieldNamed(t *testing.T, p Page, name string) Field {
t.Helper()
for _, f := range p.Forms {
for _, fl := range f.Fields {
if fl.Name == name {
return fl
}
}
}
t.Fatalf("no field %s in %+v", name, p.Forms)
return Field{}
}
// 1530: a radio group is ONE field that answers the checked button's value, and every button is an option.
func TestARadioGroupIsOneFieldAnsweringTheCheckedButton(t *testing.T) {
p := ParsePage([]byte(`
`))
if n := len(p.Forms[0].Fields); n != 1 {
t.Fatalf("%d fields, want the radio group as one: %+v", n, p.Forms[0].Fields)
}
f := fieldNamed(t, p, "R")
if f.Type != "radio" || f.Value != "0" || f.Label != "Mode" {
t.Errorf("group = %+v", f)
}
if len(f.Options) != 2 || f.Options[0] != (Option{Value: "1", Label: "Enabled"}) || f.Options[1] != (Option{Value: "0", Label: "Disabled"}) {
t.Errorf("options = %+v", f.Options)
}
}
// 1530: a checkbox answers whether it is checked, both ways.
func TestACheckboxAnswersCheckedOrNot(t *testing.T) {
p := ParsePage([]byte(``))
on, off := fieldNamed(t, p, "on"), fieldNamed(t, p, "off")
if on.Checked == nil || !*on.Checked || off.Checked == nil || *off.Checked {
t.Errorf("on = %+v, off = %+v", on, off)
}
raw, _ := json.Marshal(off)
if !strings.Contains(string(raw), `"checked":false`) {
t.Errorf("an unchecked box says so in JSON: %s", raw)
}
}
// 1530: a select answers the selected option's value, else the first, with its options as value and text.
func TestASelectAnswersTheSelectedOptionElseTheFirst(t *testing.T) {
p := ParsePage([]byte(``))
s, d := fieldNamed(t, p, "s"), fieldNamed(t, p, "d")
if s.Value != "3" || d.Value != "0" {
t.Errorf("selected %q, default %q", s.Value, d.Value)
}
if len(s.Options) != 2 || s.Options[1] != (Option{Value: "3", Text: "Deutsch"}) {
t.Errorf("options = %+v", s.Options)
}
}
// 1530: a field with no