options_test.go
This file
Path
options_test.go
106 lines · 2.5 KiB · Go
Last changed by
fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5
History of this fileTake it
1package tea23import (4 "bytes"5 "context"6 "os"7 "sync/atomic"8 "testing"9)1011func TestOptions(t *testing.T) {12 t.Run("output", func(t *testing.T) {13 t.Parallel()14 var b bytes.Buffer15 p := NewProgram(nil, WithOutput(&b))16 if f, ok := p.output.(*os.File); ok {17 t.Errorf("expected output to custom, got %v", f.Fd())18 }19 })2021 t.Run("renderer", func(t *testing.T) {22 t.Parallel()23 p := NewProgram(nil, WithoutRenderer())24 if !p.disableRenderer {25 t.Errorf("expected renderer to be a nilRenderer, got %v", p.renderer)26 }27 })2829 t.Run("without signals", func(t *testing.T) {30 t.Parallel()31 p := NewProgram(nil, WithoutSignals())32 if atomic.LoadUint32(&p.ignoreSignals) == 0 {33 t.Errorf("ignore signals should have been set")34 }35 })3637 t.Run("filter", func(t *testing.T) {38 t.Parallel()39 p := NewProgram(nil, WithFilter(func(_ Model, msg Msg) Msg { return msg }))40 if p.filter == nil {41 t.Errorf("expected filter to be set")42 }43 })4445 t.Run("external context", func(t *testing.T) {46 t.Parallel()47 extCtx, extCancel := context.WithCancel(context.Background())48 defer extCancel()4950 p := NewProgram(nil, WithContext(extCtx))51 if p.externalCtx != extCtx || p.externalCtx == context.Background() {52 t.Errorf("expected passed in external context, got default")53 }54 })5556 t.Run("input options", func(t *testing.T) {57 exercise := func(t *testing.T, opt ProgramOption, fn func(*Program)) {58 p := NewProgram(nil, opt)59 fn(p)60 }6162 t.Run("nil input", func(t *testing.T) {63 t.Parallel()64 exercise(t, WithInput(nil), func(p *Program) {65 if !p.disableInput || p.input != nil {66 t.Errorf("expected input to be disabled, got %v", p.input)67 }68 })69 })7071 t.Run("custom input", func(t *testing.T) {72 t.Parallel()73 var b bytes.Buffer74 exercise(t, WithInput(&b), func(p *Program) {75 if p.input != &b {76 t.Errorf("expected input to be custom, got %v", p.input)77 }78 })79 })80 })8182 t.Run("startup options", func(t *testing.T) {83 exercise := func(t *testing.T, opt ProgramOption, fn func(*Program)) {84 p := NewProgram(nil, opt)85 fn(p)86 }8788 t.Run("without catch panics", func(t *testing.T) {89 t.Parallel()90 exercise(t, WithoutCatchPanics(), func(p *Program) {91 if !p.disableCatchPanics {92 t.Errorf("expected catch panics to be disabled")93 }94 })95 })9697 t.Run("without signal handler", func(t *testing.T) {98 t.Parallel()99 exercise(t, WithoutSignalHandler(), func(p *Program) {100 if !p.disableSignalHandler {101 t.Errorf("expected signal handler to be disabled")102 }103 })104 })105 })106}