test: nil renderer, options and screen
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Carlos A Becker · committed by Christian Rocha · parent ab89603 · Browse files · 3 files·+176 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+176 added−0 removed
- other:+176 added−0 removed
Provenance
- Commit
cb0a72d- Tree
b790fa4- Author
Carlos A Becker <caarlos0@gmail.com>- Committer
Christian Rocha <christian@rocha.is>- Parent
ab89603
3 files changed+176 added−0 removed
added:nil_renderer_test.go+16 added
1+package tea2+3+import "testing"4+5+func TestNilRenderer(t *testing.T) {6+ r := nilRenderer{}7+ r.start()8+ r.stop()9+ r.kill()10+ r.write("a")11+ r.repaint()12+ r.setAltScreen(true)13+ if r.altScreen() {14+ t.Errorf("altScreen should always return false")15+ }16+}added:options_test.go+94 added
1+package tea2+3+import (4+ "bytes"5+ "testing"6+)7+8+func TestOptions(t *testing.T) {9+ t.Run("output", func(t *testing.T) {10+ var b bytes.Buffer11+ p := NewProgram(nil, WithOutput(&b))12+ if p.output != &b {13+ t.Errorf("expected output to custom, got %v", p.output)14+ }15+ })16+17+ t.Run("input", func(t *testing.T) {18+ var b bytes.Buffer19+ p := NewProgram(nil, WithInput(&b))20+ if p.input != &b {21+ t.Errorf("expected input to custom, got %v", p.output)22+ }23+ if p.startupOptions&withCustomInput == 0 {24+ t.Errorf("expected startup options to have custom input set, got %v", p.output)25+ }26+ })27+28+ t.Run("catch panics", func(t *testing.T) {29+ p := NewProgram(nil, WithoutCatchPanics())30+ if p.CatchPanics {31+ t.Errorf("catch panics should not have been set")32+ }33+ })34+35+ t.Run("renderer", func(t *testing.T) {36+ p := NewProgram(nil, WithoutRenderer())37+ switch p.renderer.(type) {38+ case *nilRenderer:39+ return40+ default:41+ t.Errorf("expected renderer to be a nilRenderer, got %v", p.renderer)42+ }43+ })44+45+ t.Run("startup options", func(t *testing.T) {46+ exercise := func(t *testing.T, opt ProgramOption, expect startupOptions) {47+ p := NewProgram(nil, opt)48+ if !p.startupOptions.has(expect) {49+ t.Errorf("expected startup options have %v, got %v", expect, p.startupOptions)50+ }51+ }52+53+ t.Run("input tty", func(t *testing.T) {54+ exercise(t, WithInputTTY(), withInputTTY)55+ })56+57+ t.Run("alt screen", func(t *testing.T) {58+ exercise(t, WithAltScreen(), withAltScreen)59+ })60+61+ t.Run("ansi compression", func(t *testing.T) {62+ exercise(t, WithANSICompressor(), withANSICompressor)63+ })64+65+ t.Run("mouse cell motion", func(t *testing.T) {66+ p := NewProgram(nil, WithMouseAllMotion(), WithMouseCellMotion())67+ if !p.startupOptions.has(withMouseCellMotion) {68+ t.Errorf("expected startup options have %v, got %v", withMouseCellMotion, p.startupOptions)69+ }70+ if p.startupOptions.has(withMouseAllMotion) {71+ t.Errorf("expected startup options not have %v, got %v", withMouseAllMotion, p.startupOptions)72+ }73+ })74+75+ t.Run("mouse all motion", func(t *testing.T) {76+ p := NewProgram(nil, WithMouseCellMotion(), WithMouseAllMotion())77+ if !p.startupOptions.has(withMouseAllMotion) {78+ t.Errorf("expected startup options have %v, got %v", withMouseAllMotion, p.startupOptions)79+ }80+ if p.startupOptions.has(withMouseCellMotion) {81+ t.Errorf("expected startup options not have %v, got %v", withMouseCellMotion, p.startupOptions)82+ }83+ })84+ })85+86+ t.Run("multiple", func(t *testing.T) {87+ p := NewProgram(nil, WithMouseAllMotion(), WithAltScreen(), WithInputTTY())88+ for _, opt := range []startupOptions{withMouseAllMotion, withAltScreen, withInputTTY} {89+ if !p.startupOptions.has(opt) {90+ t.Errorf("expected startup options have %v, got %v", opt, p.startupOptions)91+ }92+ }93+ })94+}added:screen_test.go+66 added
1+package tea2+3+import (4+ "bytes"5+ "io"6+ "testing"7+)8+9+func TestScreen(t *testing.T) {10+ exercise := func(t *testing.T, fn func(io.Writer), expect []byte) {11+ var w bytes.Buffer12+ fn(&w)13+ if !bytes.Equal(w.Bytes(), expect) {14+ t.Errorf("expected %q, got %q", expect, w.Bytes())15+ }16+ }17+18+ t.Run("change scrolling region", func(t *testing.T) {19+ exercise(t, func(w io.Writer) {20+ changeScrollingRegion(w, 16, 22)21+ }, []byte("\x1b[16;22r"))22+ })23+24+ t.Run("line", func(t *testing.T) {25+ t.Run("clear", func(t *testing.T) {26+ exercise(t, clearLine, []byte("\x1b[2K"))27+ })28+29+ t.Run("insert", func(t *testing.T) {30+ exercise(t, func(w io.Writer) {31+ insertLine(w, 12)32+ }, []byte("\x1b[12L"))33+ })34+ })35+36+ t.Run("cursor", func(t *testing.T) {37+ t.Run("hide", func(t *testing.T) {38+ exercise(t, hideCursor, []byte("\x1b[?25l"))39+ })40+41+ t.Run("show", func(t *testing.T) {42+ exercise(t, showCursor, []byte("\x1b[?25h"))43+ })44+45+ t.Run("up", func(t *testing.T) {46+ exercise(t, cursorUp, []byte("\x1b[1A"))47+ })48+49+ t.Run("down", func(t *testing.T) {50+ exercise(t, cursorDown, []byte("\x1b[1B"))51+ })52+53+ t.Run("move", func(t *testing.T) {54+ exercise(t, func(w io.Writer) {55+ moveCursor(w, 10, 20)56+ }, []byte("\x1b[10;20H"))57+ })58+59+ t.Run("back", func(t *testing.T) {60+ exercise(t, func(w io.Writer) {61+ cursorBack(w, 15)62+ }, []byte("\x1b[15D"))63+ })64+ })65+66+}