Skip to content
Myr

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

3 files+176 added−0 removed

added:nil_renderer_test.go+16 added
1package tea23import "testing"45func 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
1package tea23import (4 "bytes"5 "testing"6)78func 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 })1617 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 })2728 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 })3435 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 })4445 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 }5253 t.Run("input tty", func(t *testing.T) {54 exercise(t, WithInputTTY(), withInputTTY)55 })5657 t.Run("alt screen", func(t *testing.T) {58 exercise(t, WithAltScreen(), withAltScreen)59 })6061 t.Run("ansi compression", func(t *testing.T) {62 exercise(t, WithANSICompressor(), withANSICompressor)63 })6465 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 })7475 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 })8586 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
1package tea23import (4 "bytes"5 "io"6 "testing"7)89func 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 }1718 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 })2324 t.Run("line", func(t *testing.T) {25 t.Run("clear", func(t *testing.T) {26 exercise(t, clearLine, []byte("\x1b[2K"))27 })2829 t.Run("insert", func(t *testing.T) {30 exercise(t, func(w io.Writer) {31 insertLine(w, 12)32 }, []byte("\x1b[12L"))33 })34 })3536 t.Run("cursor", func(t *testing.T) {37 t.Run("hide", func(t *testing.T) {38 exercise(t, hideCursor, []byte("\x1b[?25l"))39 })4041 t.Run("show", func(t *testing.T) {42 exercise(t, showCursor, []byte("\x1b[?25h"))43 })4445 t.Run("up", func(t *testing.T) {46 exercise(t, cursorUp, []byte("\x1b[1A"))47 })4849 t.Run("down", func(t *testing.T) {50 exercise(t, cursorDown, []byte("\x1b[1B"))51 })5253 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 })5859 t.Run("back", func(t *testing.T) {60 exercise(t, func(w io.Writer) {61 cursorBack(w, 15)62 }, []byte("\x1b[15D"))63 })64 })6566}