Added bounds checks for Combobox.InsertBefore() and Combobox.Delete().
This commit is contained in:
parent
a67eaaf9ba
commit
3e47b00eda
24
combobox.go
24
combobox.go
|
@ -53,31 +53,49 @@ func (c *Combobox) Append(what ...string) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
// InsertBefore inserts a new item in the Combobox before the item at the given position. (TODO action if before is out of bounds)
|
||||
// InsertBefore inserts a new item in the Combobox before the item at the given position. It panics if the given index is out of bounds.
|
||||
func (c *Combobox) InsertBefore(what string, before int) (err error) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
var m []string
|
||||
|
||||
if c.created {
|
||||
if before < 0 || before >= c.sysData.len() {
|
||||
goto badrange
|
||||
}
|
||||
return c.sysData.insertBefore(what, before)
|
||||
}
|
||||
m := make([]string, 0, len(c.initItems) + 1)
|
||||
if before < 0 || before >= len(c.initItems) {
|
||||
goto badrange
|
||||
}
|
||||
m = make([]string, 0, len(c.initItems) + 1)
|
||||
m = append(m, c.initItems[:before]...)
|
||||
m = append(m, what)
|
||||
c.initItems = append(m, c.initItems[before:]...)
|
||||
return nil
|
||||
badrange:
|
||||
panic(fmt.Errorf("index %d out of range in Combobox.InsertBefore()", before))
|
||||
}
|
||||
|
||||
// Delete removes the given item from the Combobox. (TODO action if index is out of bounds)
|
||||
// Delete removes the given item from the Combobox. It panics if the given index is out of bounds.
|
||||
func (c *Combobox) Delete(index int) error {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
if c.created {
|
||||
if index < 0 || index >= c.sysData.len() {
|
||||
goto badrange
|
||||
}
|
||||
return c.sysData.delete(index)
|
||||
}
|
||||
if index < 0 || index >= len(c.initItems) {
|
||||
goto badrange
|
||||
}
|
||||
c.initItems = append(c.initItems[:index], c.initItems[index + 1:]...)
|
||||
return nil
|
||||
badrange:
|
||||
panic(fmt.Errorf("index %d out of range in Combobox.Delete()", index))
|
||||
}
|
||||
|
||||
// Selection returns the current selection.
|
||||
|
|
37
test/main.go
37
test/main.go
|
@ -37,6 +37,35 @@ func gridWindow() (*Window, error) {
|
|||
|
||||
var macCrashTest = flag.Bool("maccrash", false, "attempt crash on Mac OS X on deleting too far (debug lack of panic on 32-bit)")
|
||||
|
||||
func invalidTest(c *Combobox, l *Listbox) {
|
||||
x := func(what string ) {
|
||||
if j := recover(); j == nil {
|
||||
MsgBoxError("test", "%s: no panic", what)
|
||||
panic("invalid test fail")
|
||||
} else {
|
||||
println("got", j.(error).Error())
|
||||
}
|
||||
}
|
||||
|
||||
func() {
|
||||
defer x("Combobox.InsertBefore < 0"); c.InsertBefore("xxx", -5); panic(nil)
|
||||
}()
|
||||
func() {
|
||||
defer x("Combobox.InsertBefore > len"); c.InsertBefore("xxx", c.Len() + 5); panic(nil)
|
||||
}()
|
||||
func() {
|
||||
defer x("Combobox.Delete < 0"); c.Delete(-5); panic(nil)
|
||||
}()
|
||||
func() {
|
||||
defer x("Listbox.Delete > len"); c.Delete(c.Len() + 5); panic(nil)
|
||||
}()
|
||||
// TODO
|
||||
_ = l
|
||||
MsgBox("test", "all working as intended")
|
||||
}
|
||||
|
||||
var invalidBefore = flag.Bool("invalid", false, "run invalid test before opening window")
|
||||
|
||||
func myMain() {
|
||||
w := NewWindow("Main Window", 320, 240)
|
||||
w.Closing = Event()
|
||||
|
@ -56,7 +85,8 @@ func myMain() {
|
|||
prog := 0
|
||||
incButton := NewButton("Inc")
|
||||
decButton := NewButton("Dec")
|
||||
sincdec := NewHorizontalStack(incButton, decButton)
|
||||
invalidButton := NewButton("Run Invalid Test")
|
||||
sincdec := NewHorizontalStack(incButton, decButton, invalidButton)
|
||||
password := NewPasswordEdit()
|
||||
s0 := NewVerticalStack(s2, c, cb1, cb2, e, s3, pbar, sincdec, Space(), password)
|
||||
s0.SetStretchy(8)
|
||||
|
@ -79,6 +109,9 @@ func myMain() {
|
|||
s := NewHorizontalStack(s1, s0)
|
||||
s.SetStretchy(0)
|
||||
s.SetStretchy(1)
|
||||
if *invalidBefore {
|
||||
invalidTest(cb1, lb1)
|
||||
}
|
||||
err := w.Open(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -146,6 +179,8 @@ mainloop:
|
|||
prog = 0
|
||||
}
|
||||
pbar.SetProgress(prog)
|
||||
case <-invalidButton.Clicked:
|
||||
invalidTest(cb1, lb1)
|
||||
}
|
||||
}
|
||||
w.Hide()
|
||||
|
|
Loading…
Reference in New Issue