func testSlice(body jquery.JQuery, cases []sliceCase) { slices := jq("<div>") for _, c := range cases { logInfo(fmt.Sprintf("test case: %#v", c)) min, max, step := c.mms() j, e := htmlctrl.Slice(c.slice(), c.name(), "slice-id", "slice-class", min, max, step, c.valid()) if e != nil { logError(fmt.Sprintf("%s: unexpected error: %s", c.name(), e)) } if title := j.Attr("title"); title != c.name() { logError(fmt.Sprintf("%s: title is %s, expected %s", c.name(), title, c.name())) } slices.Append(j) c := c slices.Append(jq("<button>").SetText("verify "+c.name()).Call(jquery.CLICK, func() { log(c.name(), c.slice()) })) } body.Append(slices) }
func testSlices(body jquery.JQuery) { logInfo("begin testSlices") logInfo("begin testSlice bool") cases := []sliceCase{ &sliceBoolCase{"bool1", []bool{}}, &sliceBoolCase{"bool2", []bool{true, false}}, } _, e := htmlctrl.Slice(cases[0], "error", "slice-id", "slice-class", 0, 0, 0, nil) if e == nil { logError("expected error when passing non-ptr to slice") } _, e = htmlctrl.Slice(&e, "error", "slice-id", "slice-class", 0, 0, 0, nil) if e == nil { logError("expected error when passing ptr to non-slice") } testSlice(body, cases) logInfo("begin testSlice *bool") b1, b2 := true, false cases = []sliceCase{ &sliceBoolPtrCase{"[]*bool1", []*bool{&b1, &b2}, htmlctrl.ValidateBool(func(b bool) bool { log("bool is locked at true") return b })}, &sliceBoolPtrCase{"[]*bool2", []*bool{}, nil}, } testSlice(body, cases) logInfo("begin testSlice int") cases = []sliceCase{ &sliceIntCase{"[]int1", []int{2, 4}, 0, 50, 2, htmlctrl.ValidateInt(func(i int) bool { allowed := i != 3 && i != 5 && i != 7 if !allowed { log("int may not be 3, 5, or 7") } return allowed })}, &sliceIntCase{"[]int2", []int{}, 0, 0, 1, nil}, } testSlice(body, cases) logInfo("begin testSlice *int") i1, i2 := 1, 22 cases = []sliceCase{ &sliceIntPtrCase{"[]*int1", []*int{&i1, &i2}, 0, 50, 2, htmlctrl.ValidateInt(func(i int) bool { allowed := i != 3 && i != 5 && i != 7 if !allowed { log("int may not be 3, 5, or 7") } return allowed })}, &sliceIntPtrCase{"[]*int2", []*int{}, 0, 0, 1, nil}, } testSlice(body, cases) logInfo("begin testSlice float64") cases = []sliceCase{ &sliceFloat64Case{"[]float64 1", []float64{2.1, 4.2}, 0, 50, 2.1, htmlctrl.ValidateFloat64(func(f float64) bool { allowed := f != 3 && f != 5 && f != 7 if !allowed { log("float64 may not be 3, 5, or 7") } return allowed })}, &sliceFloat64Case{"[]float64 2", []float64{}, 0, 0, 1, nil}, } testSlice(body, cases) logInfo("begin testSlice *float64") f1, f2 := 1.1, 22.2 cases = []sliceCase{ &sliceFloat64PtrCase{"[]*float64 1", []*float64{&f1, &f2}, 0, 50, 2, htmlctrl.ValidateFloat64(func(f float64) bool { allowed := f != 3 && f != 5 && f != 7 if !allowed { log("float64 may not be 3, 5, or 7") } return allowed })}, &sliceFloat64PtrCase{"[]*float64 2", []*float64{}, 0, 0, 1, nil}, } testSlice(body, cases) logInfo("begin testSlice string") cases = []sliceCase{ &sliceStringCase{"[]string1", []string{"a", "b"}, htmlctrl.ValidateString(func(s string) bool { allowed := s != "c" && s != "d" if !allowed { log("string may not be c, d") } return allowed })}, &sliceStringCase{"[]string2", []string{}, nil}, } testSlice(body, cases) logInfo("begin testSlice *string") s1, s2 := "ab", "cd" cases = []sliceCase{ &sliceStringPtrCase{"[]*string1", []*string{&s1, &s2}, htmlctrl.ValidateString(func(s string) bool { allowed := s != "c" && s != "d" if !allowed { log("string may not be c, d") } return allowed })}, &sliceStringPtrCase{"[]*string2", []*string{}, nil}, } testSlice(body, cases) logInfo("begin testSlice []int") cases = []sliceCase{ &sliceIntSliceCase{"[][]int1", [][]int{{2, 4}, {8, 16}}, 0, 50, 2, htmlctrl.ValidateInt(func(i int) bool { allowed := i != 3 && i != 5 && i != 7 if !allowed { log("int may not be 3, 5, or 7") } return allowed })}, &sliceIntSliceCase{"[][]int2", [][]int{}, 0, 0, 1, nil}, } testSlice(body, cases) logInfo("begin testSlice *[]*int") is1, is2 := []*int{&i1, &i2}, []*int{} cases = []sliceCase{ &sliceIntPtrSliceCase{"[]*[]*int1", []*[]*int{&is1, &is2}, 0, 50, 2, htmlctrl.ValidateInt(func(i int) bool { allowed := i != 3 && i != 5 && i != 7 if !allowed { log("int may not be 3, 5, or 7") } return allowed })}, &sliceIntPtrSliceCase{"[]*[]*int2", []*[]*int{}, 0, 0, 1, nil}, } testSlice(body, cases) logInfo("end testSlices") }