Skip to content

Commit 22e4bcd

Browse files
author
rj
committed
Implemented quickcheck for updating the button widget. With the framework in place, adding additional control should only be a few lines.
1 parent 11e80e3 commit 22e4bcd

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

button_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,20 @@ func TestButtonUpdate(t *testing.T) {
113113
&Button{Text: "DB", Default: true},
114114
&Button{Text: "EB", Disabled: true},
115115
})
116+
117+
t.Run("QuickCheck", func(t *testing.T) {
118+
if testing.Short() {
119+
t.Skip("skipping test in short mode")
120+
}
121+
122+
updater, closer := testingUpdateWidget(t)
123+
defer closer()
124+
125+
f := func(text string, disabled, def bool) bool {
126+
return updater(&Button{Text: text, Disabled: disabled, Default: def})
127+
}
128+
if err := quick.Check(f, &quick.Config{Values: buttonValues}); err != nil {
129+
t.Errorf("quick: %s", err)
130+
}
131+
})
116132
}

widget_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package goey
22

33
import (
44
"bytes"
5+
"errors"
56
"reflect"
67
"runtime"
78
"testing"
@@ -494,3 +495,85 @@ func testingUpdateWidgets(t *testing.T, widgets []base.Widget, update []base.Wid
494495
t.Errorf("Failed to run GUI loop, %s", err)
495496
}
496497
}
498+
499+
func testingUpdateWidget(t *testing.T) (updater func(base.Widget) bool, closer func()) {
500+
ready := make(chan *Window, 1)
501+
done := make(chan struct{})
502+
503+
go func() {
504+
init := func() error {
505+
// Create the window. Some of the tests here are not expected in
506+
// production code, but we can be a little paranoid here.
507+
window, err := NewWindow(t.Name(), nil)
508+
if err != nil {
509+
t.Errorf("Failed to create window, %s", err)
510+
return nil
511+
}
512+
if window == nil {
513+
t.Errorf("Unexpected nil for window")
514+
return nil
515+
}
516+
517+
// Check that the controls that were mounted match with the list
518+
if len(window.children()) != 0 {
519+
t.Errorf("Want len(window.Children())!=0")
520+
}
521+
522+
ready <- window
523+
return nil
524+
}
525+
526+
err := loop.Run(init)
527+
if err != nil {
528+
t.Errorf("Failed to run GUI loop, %s", err)
529+
}
530+
close(done)
531+
}()
532+
533+
window := <-ready
534+
535+
updater = func(w base.Widget) bool {
536+
err := loop.Do(func() error {
537+
err := window.SetChild(w)
538+
if err != nil {
539+
return err
540+
}
541+
542+
child := window.Child()
543+
if n1, n2 := child.Kind(), w.Kind(); n1 != n2 {
544+
return errors.New("child's kind does not match widget's kind")
545+
} else if widget, ok := child.(Proper); ok {
546+
data := widget.Props()
547+
if n1, n2 := data.Kind(), w.Kind(); n1 != n2 {
548+
return errors.New("child's prop's kind does not match widget's kind")
549+
}
550+
if !equal(t, data, w) {
551+
return errors.New("child's prop's not equal to widget")
552+
}
553+
} else {
554+
return errors.New("child does not support props")
555+
}
556+
return nil
557+
})
558+
559+
if err != nil {
560+
t.Errorf("Error during widget update, %s", err)
561+
return false
562+
}
563+
return true
564+
}
565+
closer = func() {
566+
// Close the window
567+
err := loop.Do(func() error {
568+
window.Close()
569+
return nil
570+
})
571+
if err != nil {
572+
t.Errorf("Error in Do, %s", err)
573+
}
574+
575+
// Wait for the GUI loop to terminate
576+
<-done
577+
}
578+
return updater, closer
579+
}

0 commit comments

Comments
 (0)