Browse Source

Clean up tests

tablepad
Trevor Slocum 2 years ago
parent
commit
717c3842bc
  1. 27
      box_test.go
  2. 25
      button_test.go
  3. 36
      checkbox_test.go
  4. 29
      progressbar_test.go
  5. 65
      textview_test.go
  6. 43
      treeview_test.go
  7. 18
      util_test.go

27
box_test.go

@ -2,8 +2,6 @@ package cview
import (
"testing"
"github.com/gdamore/tcell"
)
const (
@ -14,14 +12,11 @@ const (
func TestBox(t *testing.T) {
t.Parallel()
b, sc, err := testBox()
if err != nil {
t.Error(err)
}
b := NewBox()
if b.GetTitle() != "" {
t.Errorf("failed to initalize Box: incorrect initial state: expected blank title, got %s", b.GetTitle())
t.Errorf("failed to initialize Box: incorrect initial state: expected blank title, got %s", b.GetTitle())
} else if b.border {
t.Errorf("failed to initalize Box: incorrect initial state: expected no border, got border")
t.Errorf("failed to initialize Box: incorrect initial state: expected no border, got border")
}
b.SetTitle(testBoxTitleA)
@ -44,16 +39,10 @@ func TestBox(t *testing.T) {
t.Errorf("failed to update Box: incorrect state: expected no border, got border")
}
b.Draw(sc)
}
func testBox() (*Box, tcell.Screen, error) {
b := NewBox()
sc := tcell.NewSimulationScreen("UTF-8")
sc.SetSize(80, 24)
_ = NewApplication().SetRoot(b, true).SetScreen(sc)
app, err := newTestApp(b)
if err != nil {
t.Errorf("failed to initialize Application: %s", err)
}
return b, sc, nil
b.Draw(app.screen)
}

25
button_test.go

@ -2,8 +2,6 @@ package cview
import (
"testing"
"github.com/gdamore/tcell"
)
const (
@ -14,12 +12,9 @@ const (
func TestButton(t *testing.T) {
t.Parallel()
b, sc, err := testButton()
if err != nil {
t.Error(err)
}
b := NewButton(testButtonLabelA)
if b.GetLabel() != testButtonLabelA {
t.Errorf("failed to initalize Button: incorrect label: expected %s, got %s", testButtonLabelA, b.GetLabel())
t.Errorf("failed to initialize Button: incorrect label: expected %s, got %s", testButtonLabelA, b.GetLabel())
}
b.SetLabel(testButtonLabelB)
@ -32,16 +27,10 @@ func TestButton(t *testing.T) {
t.Errorf("failed to update Button: incorrect label: expected %s, got %s", testButtonLabelA, b.GetLabel())
}
b.Draw(sc)
}
func testButton() (*Button, tcell.Screen, error) {
b := NewButton(testButtonLabelA)
sc := tcell.NewSimulationScreen("UTF-8")
sc.SetSize(80, 24)
_ = NewApplication().SetRoot(b, true).SetScreen(sc)
app, err := newTestApp(b)
if err != nil {
t.Errorf("failed to initialize Application: %s", err)
}
return b, sc, nil
b.Draw(app.screen)
}

36
checkbox_test.go

@ -2,8 +2,6 @@ package cview
import (
"testing"
"github.com/gdamore/tcell"
)
const (
@ -14,14 +12,16 @@ const (
func TestCheckBox(t *testing.T) {
t.Parallel()
c, sc, err := testCheckBox()
if err != nil {
t.Error(err)
}
c := NewCheckBox()
if c.IsChecked() {
t.Errorf("failed to initalize CheckBox: incorrect initial state: expected unchecked, got checked")
} else if c.GetLabel() != testCheckBoxLabelA {
t.Errorf("failed to initalize CheckBox: incorrect label: expected %s, got %s", testCheckBoxLabelA, c.GetLabel())
t.Errorf("failed to initialize CheckBox: incorrect initial state: expected unchecked, got checked")
} else if c.GetLabel() != "" {
t.Errorf("failed to initialize CheckBox: incorrect label: expected '', got %s", c.GetLabel())
}
c.SetLabel(testCheckBoxLabelA)
if c.GetLabel() != testCheckBoxLabelA {
t.Errorf("failed to set CheckBox label: incorrect label: expected %s, got %s", testCheckBoxLabelA, c.GetLabel())
}
c.SetLabel(testCheckBoxLabelB)
@ -39,18 +39,10 @@ func TestCheckBox(t *testing.T) {
t.Errorf("failed to update CheckBox state: incorrect state: expected unchecked, got checked")
}
c.Draw(sc)
}
func testCheckBox() (*CheckBox, tcell.Screen, error) {
c := NewCheckBox()
sc := tcell.NewSimulationScreen("UTF-8")
sc.SetSize(80, 24)
_ = NewApplication().SetRoot(c, true).SetScreen(sc)
c.SetLabel(testCheckBoxLabelA)
app, err := newTestApp(c)
if err != nil {
t.Errorf("failed to initialize Application: %s", err)
}
return c, sc, nil
c.Draw(app.screen)
}

29
progressbar_test.go

@ -2,23 +2,18 @@ package cview
import (
"testing"
"github.com/gdamore/tcell"
)
func TestProgressBar(t *testing.T) {
t.Parallel()
p, sc, err := testProgressBar()
if err != nil {
t.Error(err)
}
p := NewProgressBar()
if p.GetProgress() != 0 {
t.Errorf("failed to initalize ProgressBar: incorrect initial state: expected 0 progress, got %d", p.GetProgress())
t.Errorf("failed to initialize ProgressBar: incorrect initial state: expected 0 progress, got %d", p.GetProgress())
} else if p.GetMax() != 100 {
t.Errorf("failed to initalize ProgressBar: incorrect initial state: expected 100 max, got %d", p.GetMax())
t.Errorf("failed to initialize ProgressBar: incorrect initial state: expected 100 max, got %d", p.GetMax())
} else if p.Complete() {
t.Errorf("failed to initalize ProgressBar: incorrect initial state: expected incomplete, got complete")
t.Errorf("failed to initialize ProgressBar: incorrect initial state: expected incomplete, got complete")
}
p.AddProgress(25)
@ -56,16 +51,10 @@ func TestProgressBar(t *testing.T) {
t.Errorf("failed to update ProgressBar: incorrect state: expected incomplete, got complete")
}
p.Draw(sc)
}
func testProgressBar() (*ProgressBar, tcell.Screen, error) {
p := NewProgressBar()
sc := tcell.NewSimulationScreen("UTF-8")
sc.SetSize(80, 24)
_ = NewApplication().SetRoot(p, true).SetScreen(sc)
app, err := newTestApp(p)
if err != nil {
t.Errorf("failed to initialize Application: %s", err)
}
return p, sc, nil
p.Draw(app.screen)
}

65
textview_test.go

@ -4,8 +4,6 @@ import (
"bytes"
"fmt"
"testing"
"github.com/gdamore/tcell"
)
const (
@ -44,18 +42,16 @@ func TestTextViewWrite(t *testing.T) {
t.Parallel()
var (
tv, _, err = testTextView(c)
tv = tvc(NewTextView(), c)
expectedData []byte
n int
err error
)
if err != nil {
t.Error(err)
}
if c.app {
expectedData, err = prepareAppendTextView(tv)
if err != nil {
t.Error(err)
t.Errorf("failed to prepare append TextView: %s", err)
}
expectedData = append(expectedData, randomData...)
@ -91,17 +87,15 @@ func BenchmarkTextViewWrite(b *testing.B) {
b.Run(c.String(), func(b *testing.B) {
var (
tv, _, err = testTextView(c)
n int
tv = tvc(NewTextView(), c)
n int
err error
)
if err != nil {
b.Error(err)
}
if c.app {
_, err = prepareAppendTextView(tv)
if err != nil {
b.Error(err)
b.Errorf("failed to prepare append TextView: %s", err)
}
}
@ -135,31 +129,30 @@ func TestTextViewDraw(t *testing.T) {
t.Run(c.String(), func(t *testing.T) {
t.Parallel()
var (
tv, sc, err = testTextView(c)
n int
)
tv := tvc(NewTextView(), c)
app, err := newTestApp(tv)
if err != nil {
t.Error(err)
t.Errorf("failed to initialize Application: %s", err)
}
if c.app {
_, err = prepareAppendTextView(tv)
if err != nil {
t.Error(err)
t.Errorf("failed to prepare append TextView: %s", err)
}
tv.Draw(sc)
tv.Draw(app.screen)
}
n, err = tv.Write(randomData)
n, err := tv.Write(randomData)
if err != nil {
t.Errorf("failed to write (successfully wrote %d) bytes: %s", n, err)
} else if n != randomDataSize {
t.Errorf("failed to write: expected to write %d bytes, wrote %d", randomDataSize, n)
}
tv.Draw(sc)
tv.Draw(app.screen)
})
}
}
@ -169,24 +162,23 @@ func BenchmarkTextViewDraw(b *testing.B) {
c := c // Capture
b.Run(c.String(), func(b *testing.B) {
var (
tv, sc, err = testTextView(c)
n int
)
tv := tvc(NewTextView(), c)
app, err := newTestApp(tv)
if err != nil {
b.Error(err)
b.Errorf("failed to initialize Application: %s", err)
}
if c.app {
_, err = prepareAppendTextView(tv)
if err != nil {
b.Error(err)
b.Errorf("failed to prepare append TextView: %s", err)
}
tv.Draw(sc)
tv.Draw(app.screen)
}
n, err = tv.Write(randomData)
n, err := tv.Write(randomData)
if err != nil {
b.Errorf("failed to write (successfully wrote %d) bytes: %s", n, err)
} else if n != randomDataSize {
@ -197,7 +189,7 @@ func BenchmarkTextViewDraw(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
tv.Draw(sc)
tv.Draw(app.screen)
}
})
}
@ -265,17 +257,6 @@ func cl(v bool) rune {
return 'F'
}
func testTextView(c *textViewTestCase) (*TextView, tcell.Screen, error) {
tv := NewTextView()
sc := tcell.NewSimulationScreen("UTF-8")
sc.SetSize(80, 24)
_ = NewApplication().SetRoot(tv, true).SetScreen(sc)
return tvc(tv, c), sc, nil
}
func prepareAppendTextView(t *TextView) ([]byte, error) {
var b []byte
for i := 0; i < appendSetupWriteCount; i++ {

43
treeview_test.go

@ -2,8 +2,6 @@ package cview
import (
"testing"
"github.com/gdamore/tcell"
)
const (
@ -14,16 +12,18 @@ const (
func TestTreeView(t *testing.T) {
t.Parallel()
tr, sc, err := testTreeView()
if err != nil {
t.Error(err)
}
tr := NewTreeView()
if tr.GetRoot() != nil {
t.Errorf("failed to initalize TreeView: expected nil root node, got %v", tr.GetRoot())
t.Errorf("failed to initialize TreeView: expected nil root node, got %v", tr.GetRoot())
} else if tr.GetCurrentNode() != nil {
t.Errorf("failed to initalize TreeView: expected nil current node, got %v", tr.GetCurrentNode())
t.Errorf("failed to initialize TreeView: expected nil current node, got %v", tr.GetCurrentNode())
} else if tr.GetRowCount() != 0 {
t.Errorf("failed to initalize TreeView: incorrect row count: expected 0, got %d", tr.GetRowCount())
t.Errorf("failed to initialize TreeView: incorrect row count: expected 0, got %d", tr.GetRowCount())
}
app, err := newTestApp(tr)
if err != nil {
t.Errorf("failed to initialize Application: %s", err)
}
rootNode := NewTreeNode(treeViewTextA)
@ -32,16 +32,16 @@ func TestTreeView(t *testing.T) {
}
tr.SetRoot(rootNode)
tr.Draw(sc)
tr.Draw(app.screen)
if tr.GetRoot() != rootNode {
t.Errorf("failed to initalize TreeView: expected root node A, got %v", tr.GetRoot())
t.Errorf("failed to initialize TreeView: expected root node A, got %v", tr.GetRoot())
} else if tr.GetRowCount() != 1 {
t.Errorf("failed to initalize TreeView: incorrect row count: expected 1, got %d", tr.GetRowCount())
t.Errorf("failed to initialize TreeView: incorrect row count: expected 1, got %d", tr.GetRowCount())
}
tr.SetCurrentNode(rootNode)
if tr.GetCurrentNode() != rootNode {
t.Errorf("failed to initalize TreeView: expected current node A, got %v", tr.GetCurrentNode())
t.Errorf("failed to initialize TreeView: expected current node A, got %v", tr.GetCurrentNode())
}
childNode := NewTreeNode(treeViewTextB)
@ -50,21 +50,10 @@ func TestTreeView(t *testing.T) {
}
rootNode.AddChild(childNode)
tr.Draw(sc)
tr.Draw(app.screen)
if tr.GetRoot() != rootNode {
t.Errorf("failed to initalize TreeView: expected root node A, got %v", tr.GetRoot())
t.Errorf("failed to initialize TreeView: expected root node A, got %v", tr.GetRoot())
} else if tr.GetRowCount() != 2 {
t.Errorf("failed to initalize TreeView: incorrect row count: expected 1, got %d", tr.GetRowCount())
t.Errorf("failed to initialize TreeView: incorrect row count: expected 1, got %d", tr.GetRowCount())
}
}
func testTreeView() (*TreeView, tcell.Screen, error) {
t := NewTreeView()
sc := tcell.NewSimulationScreen("UTF-8")
sc.SetSize(80, 24)
_ = NewApplication().SetRoot(t, true).SetScreen(sc)
return t, sc, nil
}

18
util_test.go

@ -0,0 +1,18 @@
package cview
import (
"github.com/gdamore/tcell"
)
// newTestApp returns a new application connected to a simulation screen.
func newTestApp(root Primitive) (*Application, error) {
// Initialize simulation screen
sc := tcell.NewSimulationScreen("UTF-8")
sc.SetSize(80, 24)
// Initialize application
app := NewApplication().
SetScreen(sc).
SetRoot(root, true)
return app, nil
}
Loading…
Cancel
Save