oh-my-posh/src/engine/engine_test.go

89 lines
2.4 KiB
Go
Raw Normal View History

2022-01-26 23:38:46 -08:00
package engine
import (
"errors"
2022-01-26 04:09:21 -08:00
"oh-my-posh/color"
2022-01-26 22:44:35 -08:00
"oh-my-posh/console"
"oh-my-posh/environment"
"oh-my-posh/mock"
2022-03-21 23:41:36 -07:00
"oh-my-posh/shell"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCanWriteRPrompt(t *testing.T) {
cases := []struct {
Case string
Expected bool
TerminalWidth int
TerminalWidthError error
PromptLength int
RPromptLength int
}{
{Case: "Width Error", Expected: true, TerminalWidthError: errors.New("burp")},
{Case: "Terminal > Prompt enabled", Expected: true, TerminalWidth: 200, PromptLength: 100, RPromptLength: 10},
{Case: "Terminal > Prompt enabled edge", Expected: true, TerminalWidth: 200, PromptLength: 100, RPromptLength: 70},
{Case: "Prompt > Terminal enabled", Expected: true, TerminalWidth: 200, PromptLength: 300, RPromptLength: 70},
{Case: "Terminal > Prompt disabled no breathing", Expected: false, TerminalWidth: 200, PromptLength: 100, RPromptLength: 71},
{Case: "Prompt > Terminal disabled no breathing", Expected: false, TerminalWidth: 200, PromptLength: 300, RPromptLength: 80},
{Case: "Prompt > Terminal disabled no room", Expected: true, TerminalWidth: 200, PromptLength: 400, RPromptLength: 80},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("TerminalWidth").Return(tc.TerminalWidth, tc.TerminalWidthError)
2022-01-26 23:38:46 -08:00
engine := &Engine{
Env: env,
}
engine.rpromptLength = tc.RPromptLength
engine.currentLineLength = tc.PromptLength
got := engine.canWriteRPrompt()
assert.Equal(t, tc.Expected, got, tc.Case)
}
}
func BenchmarkEngineRender(b *testing.B) {
for i := 0; i < b.N; i++ {
2022-03-12 13:04:08 -08:00
engineRender()
}
}
2022-03-12 13:04:08 -08:00
func engineRender() {
env := &environment.ShellEnvironment{}
2022-03-12 13:04:08 -08:00
env.Init(false)
defer env.Close()
2022-01-31 04:33:36 -08:00
cfg := LoadConfig(env)
defer testClearDefaultConfig()
2022-01-26 04:09:21 -08:00
ansi := &color.Ansi{}
ansi.InitPlain(env.Shell())
2022-01-26 04:09:21 -08:00
writerColors := cfg.MakeColors(env)
writer := &color.AnsiWriter{
Ansi: ansi,
2022-03-21 23:41:36 -07:00
TerminalBackground: shell.ConsoleBackgroundColor(env, cfg.TerminalBackground),
2022-01-26 04:09:21 -08:00
AnsiColors: writerColors,
}
2022-01-26 22:44:35 -08:00
consoleTitle := &console.Title{
Env: env,
Ansi: ansi,
Template: cfg.ConsoleTitleTemplate,
}
2022-01-26 23:38:46 -08:00
engine := &Engine{
Config: cfg,
Env: env,
Writer: writer,
ConsoleTitle: consoleTitle,
Ansi: ansi,
}
2022-03-12 13:04:08 -08:00
engine.PrintPrimary()
}
2022-01-26 04:09:21 -08:00
func BenchmarkEngineRenderPalette(b *testing.B) {
for i := 0; i < b.N; i++ {
2022-03-12 13:04:08 -08:00
engineRender()
2022-01-26 04:09:21 -08:00
}
}