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

137 lines
3.2 KiB
Go
Raw Normal View History

2022-01-26 23:38:46 -08:00
package engine
import (
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/ansi"
"github.com/jandedobbeleer/oh-my-posh/src/mock"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/segments"
2022-12-28 08:30:48 -08:00
"github.com/gookit/config/v2"
2022-01-26 06:54:36 -08:00
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
)
func testClearDefaultConfig() {
config.Default().ClearAll()
}
2022-01-26 06:54:36 -08:00
func TestParseMappedLocations(t *testing.T) {
defer testClearDefaultConfig()
cases := []struct {
Case string
JSON string
}{
{Case: "new format", JSON: `{ "properties": { "mapped_locations": {"folder1": "one","folder2": "two"} } }`},
{Case: "old format", JSON: `{ "properties": { "mapped_locations": [["folder1", "one"], ["folder2", "two"]] } }`},
}
for _, tc := range cases {
config.ClearAll()
config.WithOptions(func(opt *config.Options) {
opt.DecoderConfig = &mapstructure.DecoderConfig{
TagName: "config",
}
})
err := config.LoadStrings(config.JSON, tc.JSON)
assert.NoError(t, err)
var segment Segment
err = config.BindStruct("", &segment)
assert.NoError(t, err)
mappedLocations := segment.Properties.GetKeyValueMap(segments.MappedLocations, make(map[string]string))
assert.Equal(t, "two", mappedLocations["folder2"])
}
}
func TestEscapeGlyphs(t *testing.T) {
defer testClearDefaultConfig()
cases := []struct {
Input string
Expected string
}{
{Input: "󰉋", Expected: "\\udb80\\ude4b"},
{Input: "a", Expected: "a"},
{Input: "\ue0b4", Expected: "\\ue0b4"},
{Input: "\ufd03", Expected: "\\ufd03"},
{Input: "}", Expected: "}"},
2022-02-02 04:55:12 -08:00
{Input: "🏚", Expected: "🏚"},
}
for _, tc := range cases {
2023-01-27 05:29:22 -08:00
assert.Equal(t, tc.Expected, escapeGlyphs(tc.Input, false), tc.Input)
}
}
2022-10-13 11:01:51 -07:00
func TestGetPalette(t *testing.T) {
2023-01-04 11:44:29 -08:00
palette := ansi.Palette{
2022-10-13 11:01:51 -07:00
"red": "#ff0000",
"blue": "#0000ff",
}
cases := []struct {
Case string
2023-01-04 11:44:29 -08:00
Palettes *ansi.Palettes
Palette ansi.Palette
ExpectedPalette ansi.Palette
2022-10-13 11:01:51 -07:00
}{
{
Case: "match",
2023-01-04 11:44:29 -08:00
Palettes: &ansi.Palettes{
2022-10-13 11:01:51 -07:00
Template: "{{ .Shell }}",
2023-01-04 11:44:29 -08:00
List: map[string]ansi.Palette{
2022-10-13 11:01:51 -07:00
"bash": palette,
"zsh": {
"red": "#ff0001",
"blue": "#0000fb",
},
},
},
ExpectedPalette: palette,
},
{
Case: "no match, no fallback",
2023-01-04 11:44:29 -08:00
Palettes: &ansi.Palettes{
2022-10-13 11:01:51 -07:00
Template: "{{ .Shell }}",
2023-01-04 11:44:29 -08:00
List: map[string]ansi.Palette{
2022-10-13 11:01:51 -07:00
"fish": palette,
"zsh": {
"red": "#ff0001",
"blue": "#0000fb",
},
},
},
ExpectedPalette: nil,
},
{
Case: "no match, default",
2023-01-04 11:44:29 -08:00
Palettes: &ansi.Palettes{
2022-10-13 11:01:51 -07:00
Template: "{{ .Shell }}",
2023-01-04 11:44:29 -08:00
List: map[string]ansi.Palette{
2022-10-13 11:01:51 -07:00
"zsh": {
"red": "#ff0001",
"blue": "#0000fb",
},
},
},
Palette: palette,
ExpectedPalette: palette,
},
{
Case: "no palettes",
ExpectedPalette: nil,
},
}
for _, tc := range cases {
env := &mock.MockedEnvironment{}
2022-11-09 11:27:54 -08:00
env.On("TemplateCache").Return(&platform.TemplateCache{
2022-10-13 11:01:51 -07:00
Env: map[string]string{},
Shell: "bash",
})
cfg := &Config{
env: env,
Palette: tc.Palette,
Palettes: tc.Palettes,
}
got := cfg.getPalette()
assert.Equal(t, tc.ExpectedPalette, got, tc.Case)
}
}