From cdd535cc717201bb9814e798b86efe65b326dd98 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 11 Mar 2014 17:37:04 -0400 Subject: [PATCH] Made Stack.SetStretchy() panic on invalid index, ending that group of functions. Also added the tests for those functions who did not have tests already written. --- stack.go | 7 +++++-- test/main.go | 40 +++++++++++++++++++++++++++++++++++++--- todo.md | 1 - 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/stack.go b/stack.go index cb65bb5..2ead5dc 100644 --- a/stack.go +++ b/stack.go @@ -48,7 +48,7 @@ func NewVerticalStack(controls ...Control) *Stack { } // SetStretchy marks a control in a Stack as stretchy. This cannot be called once the Window containing the Stack has been opened. -// (TODO action on invalid index) +// It panics if index is out of range. func (s *Stack) SetStretchy(index int) { s.lock.Lock() defer s.lock.Unlock() @@ -56,7 +56,10 @@ func (s *Stack) SetStretchy(index int) { if s.created { panic("call to Stack.SetStretchy() after Stack has been created") } - s.stretchy[index] = true // TODO explicitly check for index out of bounds? + if index < 0 || index > len(s.stretchy) { + panic(fmt.Errorf("index %d out of range in Stack.SetStretchy()", index)) + } + s.stretchy[index] = true } func (s *Stack) make(window *sysData) error { diff --git a/test/main.go b/test/main.go index 6d0a5c0..b2d0fb0 100644 --- a/test/main.go +++ b/test/main.go @@ -37,7 +37,7 @@ 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) { +func invalidTest(c *Combobox, l *Listbox, s *Stack, g *Grid) { x := func(what string ) { if j := recover(); j == nil { MsgBoxError("test", "%s: no panic", what) @@ -71,6 +71,40 @@ func invalidTest(c *Combobox, l *Listbox) { func() { defer x("Listbox.Delete > len"); l.Delete(c.Len() + 5); panic(nil) }() + if s != nil { + func() { + defer x("Stack.SetStretchy < 0"); s.SetStretchy(-5); panic(nil) + }() + func() { + defer x("Stack.SetStretchy > len"); s.SetStretchy(5555); panic(nil) + }() + } + if g != nil { + func() { + defer x("Grid.SetFilling x < 0"); g.SetFilling(-5, 0); panic(nil) + }() + func() { + defer x("Grid.SetFilling x > len"); g.SetFilling(5555, 0); panic(nil) + }() + func() { + defer x("Grid.SetFilling y < 0"); g.SetFilling(0, -5); panic(nil) + }() + func() { + defer x("Grid.SetFilling y > len"); g.SetFilling(0, 5555); panic(nil) + }() + func() { + defer x("Grid.SetStretchy x < 0"); g.SetStretchy(-5, 0); panic(nil) + }() + func() { + defer x("Grid.SetStretchy x > len"); g.SetStretchy(5555, 0); panic(nil) + }() + func() { + defer x("Grid.SetStretchy y < 0"); g.SetStretchy(0, -5); panic(nil) + }() + func() { + defer x("Grid.SetStretchy y > len"); g.SetStretchy(0, 5555); panic(nil) + }() + } MsgBox("test", "all working as intended") } @@ -120,7 +154,7 @@ func myMain() { s.SetStretchy(0) s.SetStretchy(1) if *invalidBefore { - invalidTest(cb1, lb1) + invalidTest(cb1, lb1, s, NewGrid(1, Space())) } err := w.Open(s) if err != nil { @@ -187,7 +221,7 @@ mainloop: } pbar.SetProgress(prog) case <-invalidButton.Clicked: - invalidTest(cb1, lb1) + invalidTest(cb1, lb1, nil, nil) } } w.Hide() diff --git a/todo.md b/todo.md index 87d5c76..b730440 100644 --- a/todo.md +++ b/todo.md @@ -17,7 +17,6 @@ so I don't forget: - Combobox/Listbox.Select (with Listbox.Select allowing bulk) - Checkbox.Check or Checkbox.SetChecked - Listbox.SelectAll -- have methods that take indices panic on invalid index, like the Stack and Grid stretchy methods - make the Windows implementation of message boxes run on uitask - ensure MsgBoxError can run if initialization failed if things change ever - should Labels be selectable?