2021-11-01 14:59:59 -04:00
|
|
|
package tea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-11-07 11:49:00 -05:00
|
|
|
"sync/atomic"
|
2021-11-01 14:59:59 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOptions(t *testing.T) {
|
|
|
|
t.Run("output", func(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
p := NewProgram(nil, WithOutput(&b))
|
2022-06-04 09:14:03 -04:00
|
|
|
if p.output.TTY() != nil {
|
|
|
|
t.Errorf("expected output to custom, got %v", p.output.TTY().Fd())
|
2021-11-01 14:59:59 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 14:55:25 -04:00
|
|
|
t.Run("custom input", func(t *testing.T) {
|
2021-11-01 14:59:59 -04:00
|
|
|
var b bytes.Buffer
|
|
|
|
p := NewProgram(nil, WithInput(&b))
|
|
|
|
if p.input != &b {
|
2022-06-04 09:17:30 -04:00
|
|
|
t.Errorf("expected input to custom, got %v", p.input)
|
2021-11-01 14:59:59 -04:00
|
|
|
}
|
2023-05-05 14:55:25 -04:00
|
|
|
if p.inputType != customInput {
|
2022-06-04 09:17:30 -04:00
|
|
|
t.Errorf("expected startup options to have custom input set, got %v", p.input)
|
2021-11-01 14:59:59 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-04 15:33:25 -04:00
|
|
|
t.Run("without signals", func(t *testing.T) {
|
|
|
|
p := NewProgram(nil, WithoutSignals())
|
2023-11-07 11:49:00 -05:00
|
|
|
if atomic.LoadUint32(&p.ignoreSignals) == 0 {
|
2023-05-04 15:33:25 -04:00
|
|
|
t.Errorf("ignore signals should have been set")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-17 16:02:55 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 14:55:25 -04:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2021-11-01 14:59:59 -04:00
|
|
|
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("ansi compression", func(t *testing.T) {
|
|
|
|
exercise(t, WithANSICompressor(), withANSICompressor)
|
|
|
|
})
|
|
|
|
|
2022-10-07 15:48:50 -04:00
|
|
|
t.Run("without catch panics", func(t *testing.T) {
|
|
|
|
exercise(t, WithoutCatchPanics(), withoutCatchPanics)
|
|
|
|
})
|
|
|
|
|
2022-10-07 14:24:23 -04:00
|
|
|
t.Run("without signal handler", func(t *testing.T) {
|
|
|
|
exercise(t, WithoutSignalHandler(), withoutSignalHandler)
|
|
|
|
})
|
|
|
|
|
2021-11-01 14:59:59 -04:00
|
|
|
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())
|
2023-05-05 14:55:25 -04:00
|
|
|
for _, opt := range []startupOptions{withMouseAllMotion, withAltScreen} {
|
2021-11-01 14:59:59 -04:00
|
|
|
if !p.startupOptions.has(opt) {
|
|
|
|
t.Errorf("expected startup options have %v, got %v", opt, p.startupOptions)
|
|
|
|
}
|
2023-05-05 14:55:25 -04:00
|
|
|
if p.inputType != ttyInput {
|
|
|
|
t.Errorf("expected input to be %v, got %v", opt, p.startupOptions)
|
|
|
|
}
|
2021-11-01 14:59:59 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|