2022-01-26 23:38:46 -08:00
|
|
|
|
package engine
|
2021-04-04 11:28:41 -07:00
|
|
|
|
|
|
|
|
|
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"
|
2021-04-04 11:28:41 -07:00
|
|
|
|
"os"
|
2022-05-05 09:06:31 -07:00
|
|
|
|
"path/filepath"
|
2021-04-04 11:28:41 -07:00
|
|
|
|
"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"
|
2021-04-04 11:28:41 -07:00
|
|
|
|
file, err := ioutil.TempFile("", poshImagePath)
|
|
|
|
|
if err != nil {
|
2022-05-05 09:06:31 -07:00
|
|
|
|
return "", err
|
2021-04-04 11:28:41 -07:00
|
|
|
|
}
|
|
|
|
|
defer os.Remove(file.Name())
|
2022-01-26 04:09:21 -08:00
|
|
|
|
ansi := &color.Ansi{}
|
2022-06-12 10:36:49 -07:00
|
|
|
|
ansi.InitPlain()
|
2021-04-04 11:28:41 -07:00
|
|
|
|
image := &ImageRenderer{
|
2022-01-26 23:38:46 -08:00
|
|
|
|
AnsiString: content,
|
|
|
|
|
Ansi: ansi,
|
2021-04-04 11:28:41 -07:00
|
|
|
|
}
|
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
|
2021-04-04 11:28:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2021-04-04 11:28:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStringImageFileWithANSI(t *testing.T) {
|
2022-02-03 08:36:37 -08:00
|
|
|
|
prompt := `[38;2;40;105;131m[0m[48;2;40;105;131m[38;2;224;222;244m jan [0m[38;2;40;105;131m[0m[38;2;224;222;244m [0m`
|
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)
|
|
|
|
|
}
|
2021-04-04 11:28:41 -07:00
|
|
|
|
}
|