From cb0a72d682e5c9bb36dc92d2084c66b47fe01357 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Mon, 1 Nov 2021 15:59:59 -0300 Subject: [PATCH] test: nil renderer, options and screen --- nil_renderer_test.go | 16 ++++++++ options_test.go | 94 ++++++++++++++++++++++++++++++++++++++++++++ screen_test.go | 66 +++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 nil_renderer_test.go create mode 100644 options_test.go create mode 100644 screen_test.go diff --git a/nil_renderer_test.go b/nil_renderer_test.go new file mode 100644 index 0000000..7364484 --- /dev/null +++ b/nil_renderer_test.go @@ -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") + } +} diff --git a/options_test.go b/options_test.go new file mode 100644 index 0000000..4b75994 --- /dev/null +++ b/options_test.go @@ -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) + } + } + }) +} diff --git a/screen_test.go b/screen_test.go new file mode 100644 index 0000000..7bfdf2e --- /dev/null +++ b/screen_test.go @@ -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")) + }) + }) + +}