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

64 lines
1.7 KiB
Go
Raw Normal View History

2022-01-26 23:38:46 -08:00
package engine
import (
2022-08-09 01:16:33 -07:00
"io/ioutil" //nolint:staticcheck,nolintlint
2022-01-26 04:09:21 -08:00
"oh-my-posh/color"
"os"
2022-05-05 09:06:31 -07:00
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
2022-05-05 09:06:31 -07:00
var cases = []struct {
Case string
Config string
}{
{Case: ".omp.json suffix", Config: "~/jandedobbeleer.omp.json"},
{Case: ".omp.yaml suffix", Config: "~/jandedobbeleer.omp.yaml"},
{Case: ".omp.yml suffix", Config: "~/jandedobbeleer.omp.yml"},
{Case: ".omp.toml suffix", Config: "~/jandedobbeleer.omp.toml"},
{Case: ".json suffix", Config: "~/jandedobbeleer.json"},
{Case: ".yaml suffix", Config: "~/jandedobbeleer.yaml"},
{Case: ".yml suffix", Config: "~/jandedobbeleer.yml"},
{Case: ".toml suffix", Config: "~/jandedobbeleer.toml"},
}
func runImageTest(config, content string) (string, error) {
2022-01-26 23:38:46 -08:00
poshImagePath := "jandedobbeleer.png"
file, err := ioutil.TempFile("", poshImagePath)
if err != nil {
2022-05-05 09:06:31 -07:00
return "", err
}
defer os.Remove(file.Name())
2022-01-26 04:09:21 -08:00
ansi := &color.Ansi{}
ansi.InitPlain()
image := &ImageRenderer{
2022-01-26 23:38:46 -08:00
AnsiString: content,
Ansi: ansi,
}
2022-05-05 09:06:31 -07:00
image.Init(config)
2022-01-26 23:38:46 -08:00
err = image.SavePNG()
2022-05-05 09:06:31 -07:00
if err == nil {
os.Remove(image.Path)
}
return filepath.Base(image.Path), err
}
func TestStringImageFileWithText(t *testing.T) {
2022-05-05 09:06:31 -07:00
for _, tc := range cases {
filename, err := runImageTest(tc.Config, "foobar")
assert.Equal(t, "jandedobbeleer.png", filename, tc.Case)
assert.NoError(t, err)
}
}
func TestStringImageFileWithANSI(t *testing.T) {
prompt := ` jan  `
2022-05-05 09:06:31 -07:00
for _, tc := range cases {
filename, err := runImageTest(tc.Config, prompt)
assert.Equal(t, "jandedobbeleer.png", filename, tc.Case)
assert.NoError(t, err)
}
}