1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
package tea
import (
"bytes"
"os"
"sync/atomic"
"testing"
)
func TestOptions(t *testing.T) {
t.Run("output", func(t *testing.T) {
var b bytes.Buffer
p := NewProgram(nil, WithOutput(&b))
if f, ok := p.output.(*os.File); ok {
t.Errorf("expected output to custom, got %v", f.Fd())
}
})
t.Run("custom 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.input)
}
if p.inputType != customInput {
t.Errorf("expected startup options to have custom input set, got %v", p.input)
}
})
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("without signals", func(t *testing.T) {
p := NewProgram(nil, WithoutSignals())
if atomic.LoadUint32(&p.ignoreSignals) == 0 {
t.Errorf("ignore signals should have been set")
}
})
t.Run("filter", func(t *testing.T) {
p := NewProgram(nil, WithFilter(func(_ Model, msg Msg) Msg { return msg }))
if p.filter == nil {
t.Errorf("expected filter to be set")
}
})
t.Run("input options", func(t *testing.T) {
exercise := func(t *testing.T, opt ProgramOption, expect inputType) {
p := NewProgram(nil, opt)
if p.inputType != expect {
t.Errorf("expected input type %s, got %s", expect, p.inputType)
}
}
t.Run("tty input", func(t *testing.T) {
exercise(t, WithInputTTY(), ttyInput)
})
t.Run("custom input", func(t *testing.T) {
var b bytes.Buffer
exercise(t, WithInput(&b), customInput)
})
})
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("alt screen", func(t *testing.T) {
exercise(t, WithAltScreen(), withAltScreen)
})
t.Run("bracketed paste disabled", func(t *testing.T) {
exercise(t, WithoutBracketedPaste(), withoutBracketedPaste)
})
t.Run("ansi compression", func(t *testing.T) {
exercise(t, WithANSICompressor(), withANSICompressor)
})
t.Run("without catch panics", func(t *testing.T) {
exercise(t, WithoutCatchPanics(), withoutCatchPanics)
})
t.Run("without signal handler", func(t *testing.T) {
exercise(t, WithoutSignalHandler(), withoutSignalHandler)
})
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(), WithoutBracketedPaste(), WithAltScreen(), WithInputTTY())
for _, opt := range []startupOptions{withMouseAllMotion, withoutBracketedPaste, withAltScreen} {
if !p.startupOptions.has(opt) {
t.Errorf("expected startup options have %v, got %v", opt, p.startupOptions)
}
if p.inputType != ttyInput {
t.Errorf("expected input to be %v, got %v", opt, p.startupOptions)
}
}
})
}
|