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

168 lines
4.3 KiB
Go
Raw Normal View History

2022-01-26 23:38:46 -08:00
package engine
2019-03-13 04:14:30 -07:00
import (
2020-10-02 07:58:25 -07:00
"encoding/json"
2019-03-13 04:14:30 -07:00
"testing"
2022-12-28 08:30:48 -08:00
"github.com/jandedobbeleer/oh-my-posh/mock"
"github.com/jandedobbeleer/oh-my-posh/platform"
"github.com/jandedobbeleer/oh-my-posh/properties"
"github.com/jandedobbeleer/oh-my-posh/segments"
2019-03-13 04:14:30 -07:00
"github.com/stretchr/testify/assert"
)
const (
2021-02-15 23:36:37 -08:00
cwd = "Projects/oh-my-posh"
)
2019-03-13 04:14:30 -07:00
func TestMapSegmentWriterCanMap(t *testing.T) {
sc := &Segment{
2022-01-26 05:10:18 -08:00
Type: SESSION,
2019-03-13 04:14:30 -07:00
}
env := new(mock.MockedEnvironment)
err := sc.mapSegmentWithWriter(env)
2020-09-17 07:51:29 -07:00
assert.NoError(t, err)
2019-03-13 04:14:30 -07:00
assert.NotNil(t, sc.writer)
}
func TestMapSegmentWriterCannotMap(t *testing.T) {
sc := &Segment{
Type: "nilwriter",
}
env := new(mock.MockedEnvironment)
err := sc.mapSegmentWithWriter(env)
2020-09-17 07:51:29 -07:00
assert.Error(t, err)
2019-03-13 04:14:30 -07:00
}
2020-10-02 07:58:25 -07:00
2021-03-20 11:32:15 -07:00
func TestParseTestConfig(t *testing.T) {
2020-10-02 07:58:25 -07:00
segmentJSON :=
`
{
"type": "path",
"style": "powerline",
2020-10-15 23:37:43 -07:00
"powerline_symbol": "\uE0B0",
2020-10-02 07:58:25 -07:00
"foreground": "#ffffff",
"background": "#61AFEF",
"properties": {
"style": "folder",
2021-02-27 20:05:51 -08:00
"exclude_folders": [
"/super/secret/project"
2020-10-02 07:58:25 -07:00
]
}
}
`
segment := &Segment{}
err := json.Unmarshal([]byte(segmentJSON), segment)
assert.NoError(t, err)
}
2021-02-27 20:05:51 -08:00
func TestShouldIncludeFolder(t *testing.T) {
cases := []struct {
Case string
Included bool
Excluded bool
Expected bool
2021-02-27 20:05:51 -08:00
}{
{Case: "Include", Included: true, Excluded: false, Expected: true},
{Case: "Exclude", Included: false, Excluded: true, Expected: false},
{Case: "Include & Exclude", Included: true, Excluded: true, Expected: false},
{Case: "!Include & !Exclude", Included: false, Excluded: false, Expected: false},
2021-02-27 20:05:51 -08:00
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
2022-11-09 11:27:54 -08:00
env.On("GOOS").Return(platform.LINUX)
env.On("Home").Return("")
env.On("Pwd").Return(cwd)
env.On("DirMatchesOneOf", cwd, []string{"Projects/oh-my-posh"}).Return(tc.Included)
env.On("DirMatchesOneOf", cwd, []string{"Projects/nope"}).Return(tc.Excluded)
2021-02-27 20:05:51 -08:00
segment := &Segment{
Properties: properties.Map{
properties.IncludeFolders: []string{"Projects/oh-my-posh"},
properties.ExcludeFolders: []string{"Projects/nope"},
2021-02-27 20:05:51 -08:00
},
env: env,
2021-02-27 20:05:51 -08:00
}
got := segment.shouldIncludeFolder()
2021-02-27 20:05:51 -08:00
assert.Equal(t, tc.Expected, got, tc.Case)
}
2020-10-02 07:58:25 -07:00
}
func TestGetColors(t *testing.T) {
cases := []struct {
Case string
Background bool
ExpectedColor string
Templates []string
DefaultColor string
Region string
Profile string
}{
{Case: "No template - foreground", ExpectedColor: "color", Background: false, DefaultColor: "color"},
{Case: "No template - background", ExpectedColor: "color", Background: true, DefaultColor: "color"},
{Case: "Nil template", ExpectedColor: "color", DefaultColor: "color", Templates: nil},
{
Case: "Template - default",
ExpectedColor: "color",
DefaultColor: "color",
Templates: []string{
"{{if contains \"john\" .Profile}}color2{{end}}",
},
Profile: "doe",
},
{
Case: "Template - override",
ExpectedColor: "color2",
DefaultColor: "color",
Templates: []string{
"{{if contains \"john\" .Profile}}color2{{end}}",
},
Profile: "john",
},
{
Case: "Template - override multiple",
ExpectedColor: "color3",
DefaultColor: "color",
Templates: []string{
"{{if contains \"doe\" .Profile}}color2{{end}}",
"{{if contains \"john\" .Profile}}color3{{end}}",
},
Profile: "john",
},
{
Case: "Template - override multiple no match",
ExpectedColor: "color",
DefaultColor: "color",
Templates: []string{
"{{if contains \"doe\" .Profile}}color2{{end}}",
"{{if contains \"philip\" .Profile}}color3{{end}}",
},
Profile: "john",
},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
2022-11-09 11:27:54 -08:00
env.On("TemplateCache").Return(&platform.TemplateCache{
Env: make(map[string]string),
})
segment := &Segment{
2022-01-26 06:54:36 -08:00
writer: &segments.Aws{
Profile: tc.Profile,
Region: tc.Region,
},
2022-01-18 00:48:47 -08:00
env: env,
}
if tc.Background {
segment.Background = tc.DefaultColor
segment.BackgroundTemplates = tc.Templates
color := segment.background()
assert.Equal(t, tc.ExpectedColor, color, tc.Case)
continue
}
segment.Foreground = tc.DefaultColor
segment.ForegroundTemplates = tc.Templates
color := segment.foreground()
assert.Equal(t, tc.ExpectedColor, color, tc.Case)
}
}