test: nil renderer, options and screen

This commit is contained in:
Carlos A Becker 2021-11-01 15:59:59 -03:00 committed by Christian Rocha
parent ab89603d28
commit cb0a72d682
3 changed files with 176 additions and 0 deletions

16
nil_renderer_test.go Normal file
View File

@ -0,0 +1,16 @@
package tea
import "testing"
func TestNilRenderer(t *testing.T) {
r := nilRenderer{}
r.start()
r.stop()
r.kill()
r.write("a")
r.repaint()
r.setAltScreen(true)
if r.altScreen() {
t.Errorf("altScreen should always return false")
}
}

94
options_test.go Normal file
View File

@ -0,0 +1,94 @@
package tea
import (
"bytes"
"testing"
)
func TestOptions(t *testing.T) {
t.Run("output", func(t *testing.T) {
var b bytes.Buffer
p := NewProgram(nil, WithOutput(&b))
if p.output != &b {
t.Errorf("expected output to custom, got %v", p.output)
}
})
t.Run("input", func(t *testing.T) {
var b bytes.Buffer
p := NewProgram(nil, WithInput(&b))
if p.input != &b {
t.Errorf("expected input to custom, got %v", p.output)
}
if p.startupOptions&withCustomInput == 0 {
t.Errorf("expected startup options to have custom input set, got %v", p.output)
}
})
t.Run("catch panics", func(t *testing.T) {
p := NewProgram(nil, WithoutCatchPanics())
if p.CatchPanics {
t.Errorf("catch panics should not have been set")
}
})
t.Run("renderer", func(t *testing.T) {
p := NewProgram(nil, WithoutRenderer())
switch p.renderer.(type) {
case *nilRenderer:
return
default:
t.Errorf("expected renderer to be a nilRenderer, got %v", p.renderer)
}
})
t.Run("startup options", func(t *testing.T) {
exercise := func(t *testing.T, opt ProgramOption, expect startupOptions) {
p := NewProgram(nil, opt)
if !p.startupOptions.has(expect) {
t.Errorf("expected startup options have %v, got %v", expect, p.startupOptions)
}
}
t.Run("input tty", func(t *testing.T) {
exercise(t, WithInputTTY(), withInputTTY)
})
t.Run("alt screen", func(t *testing.T) {
exercise(t, WithAltScreen(), withAltScreen)
})
t.Run("ansi compression", func(t *testing.T) {
exercise(t, WithANSICompressor(), withANSICompressor)
})
t.Run("mouse cell motion", func(t *testing.T) {
p := NewProgram(nil, WithMouseAllMotion(), WithMouseCellMotion())
if !p.startupOptions.has(withMouseCellMotion) {
t.Errorf("expected startup options have %v, got %v", withMouseCellMotion, p.startupOptions)
}
if p.startupOptions.has(withMouseAllMotion) {
t.Errorf("expected startup options not have %v, got %v", withMouseAllMotion, p.startupOptions)
}
})
t.Run("mouse all motion", func(t *testing.T) {
p := NewProgram(nil, WithMouseCellMotion(), WithMouseAllMotion())
if !p.startupOptions.has(withMouseAllMotion) {
t.Errorf("expected startup options have %v, got %v", withMouseAllMotion, p.startupOptions)
}
if p.startupOptions.has(withMouseCellMotion) {
t.Errorf("expected startup options not have %v, got %v", withMouseCellMotion, p.startupOptions)
}
})
})
t.Run("multiple", func(t *testing.T) {
p := NewProgram(nil, WithMouseAllMotion(), WithAltScreen(), WithInputTTY())
for _, opt := range []startupOptions{withMouseAllMotion, withAltScreen, withInputTTY} {
if !p.startupOptions.has(opt) {
t.Errorf("expected startup options have %v, got %v", opt, p.startupOptions)
}
}
})
}

66
screen_test.go Normal file
View File

@ -0,0 +1,66 @@
package tea
import (
"bytes"
"io"
"testing"
)
func TestScreen(t *testing.T) {
exercise := func(t *testing.T, fn func(io.Writer), expect []byte) {
var w bytes.Buffer
fn(&w)
if !bytes.Equal(w.Bytes(), expect) {
t.Errorf("expected %q, got %q", expect, w.Bytes())
}
}
t.Run("change scrolling region", func(t *testing.T) {
exercise(t, func(w io.Writer) {
changeScrollingRegion(w, 16, 22)
}, []byte("\x1b[16;22r"))
})
t.Run("line", func(t *testing.T) {
t.Run("clear", func(t *testing.T) {
exercise(t, clearLine, []byte("\x1b[2K"))
})
t.Run("insert", func(t *testing.T) {
exercise(t, func(w io.Writer) {
insertLine(w, 12)
}, []byte("\x1b[12L"))
})
})
t.Run("cursor", func(t *testing.T) {
t.Run("hide", func(t *testing.T) {
exercise(t, hideCursor, []byte("\x1b[?25l"))
})
t.Run("show", func(t *testing.T) {
exercise(t, showCursor, []byte("\x1b[?25h"))
})
t.Run("up", func(t *testing.T) {
exercise(t, cursorUp, []byte("\x1b[1A"))
})
t.Run("down", func(t *testing.T) {
exercise(t, cursorDown, []byte("\x1b[1B"))
})
t.Run("move", func(t *testing.T) {
exercise(t, func(w io.Writer) {
moveCursor(w, 10, 20)
}, []byte("\x1b[10;20H"))
})
t.Run("back", func(t *testing.T) {
exercise(t, func(w io.Writer) {
cursorBack(w, 15)
}, []byte("\x1b[15D"))
})
})
}