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
|
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) {
|
func (c *Combobox) InsertBefore(what string, before int) (err error) {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
|
var m []string
|
||||||
|
|
||||||
if c.created {
|
if c.created {
|
||||||
|
if before < 0 || before >= c.sysData.len() {
|
||||||
|
goto badrange
|
||||||
|
}
|
||||||
return c.sysData.insertBefore(what, before)
|
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, c.initItems[:before]...)
|
||||||
m = append(m, what)
|
m = append(m, what)
|
||||||
c.initItems = append(m, c.initItems[before:]...)
|
c.initItems = append(m, c.initItems[before:]...)
|
||||||
return nil
|
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 {
|
func (c *Combobox) Delete(index int) error {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
if c.created {
|
if c.created {
|
||||||
|
if index < 0 || index >= c.sysData.len() {
|
||||||
|
goto badrange
|
||||||
|
}
|
||||||
return c.sysData.delete(index)
|
return c.sysData.delete(index)
|
||||||
}
|
}
|
||||||
|
if index < 0 || index >= len(c.initItems) {
|
||||||
|
goto badrange
|
||||||
|
}
|
||||||
c.initItems = append(c.initItems[:index], c.initItems[index + 1:]...)
|
c.initItems = append(c.initItems[:index], c.initItems[index + 1:]...)
|
||||||
return nil
|
return nil
|
||||||
|
badrange:
|
||||||
|
panic(fmt.Errorf("index %d out of range in Combobox.Delete()", index))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Selection returns the current selection.
|
// 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)")
|
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() {
|
func myMain() {
|
||||||
w := NewWindow("Main Window", 320, 240)
|
w := NewWindow("Main Window", 320, 240)
|
||||||
w.Closing = Event()
|
w.Closing = Event()
|
||||||
|
@ -56,7 +85,8 @@ func myMain() {
|
||||||
prog := 0
|
prog := 0
|
||||||
incButton := NewButton("Inc")
|
incButton := NewButton("Inc")
|
||||||
decButton := NewButton("Dec")
|
decButton := NewButton("Dec")
|
||||||
sincdec := NewHorizontalStack(incButton, decButton)
|
invalidButton := NewButton("Run Invalid Test")
|
||||||
|
sincdec := NewHorizontalStack(incButton, decButton, invalidButton)
|
||||||
password := NewPasswordEdit()
|
password := NewPasswordEdit()
|
||||||
s0 := NewVerticalStack(s2, c, cb1, cb2, e, s3, pbar, sincdec, Space(), password)
|
s0 := NewVerticalStack(s2, c, cb1, cb2, e, s3, pbar, sincdec, Space(), password)
|
||||||
s0.SetStretchy(8)
|
s0.SetStretchy(8)
|
||||||
|
@ -79,6 +109,9 @@ func myMain() {
|
||||||
s := NewHorizontalStack(s1, s0)
|
s := NewHorizontalStack(s1, s0)
|
||||||
s.SetStretchy(0)
|
s.SetStretchy(0)
|
||||||
s.SetStretchy(1)
|
s.SetStretchy(1)
|
||||||
|
if *invalidBefore {
|
||||||
|
invalidTest(cb1, lb1)
|
||||||
|
}
|
||||||
err := w.Open(s)
|
err := w.Open(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -146,6 +179,8 @@ mainloop:
|
||||||
prog = 0
|
prog = 0
|
||||||
}
|
}
|
||||||
pbar.SetProgress(prog)
|
pbar.SetProgress(prog)
|
||||||
|
case <-invalidButton.Clicked:
|
||||||
|
invalidTest(cb1, lb1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.Hide()
|
w.Hide()
|
||||||
|
|
Loading…
Reference in New Issue