2021-08-09 20:48:44 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTextSegment(t *testing.T) {
|
|
|
|
cases := []struct {
|
2021-10-06 12:50:52 -07:00
|
|
|
Case string
|
|
|
|
ExpectedString string
|
|
|
|
Text string
|
|
|
|
ExpectedDisabled bool
|
2021-08-09 20:48:44 -07:00
|
|
|
}{
|
|
|
|
{Case: "standard text", ExpectedString: "hello", Text: "hello"},
|
|
|
|
{Case: "template text with env var", ExpectedString: "hello world", Text: "{{ .Env.HELLO }} world"},
|
|
|
|
{Case: "template text with shell name", ExpectedString: "hello world from terminal", Text: "{{ .Env.HELLO }} world from {{ .Shell }}"},
|
|
|
|
{Case: "template text with folder", ExpectedString: "hello world in posh", Text: "{{ .Env.HELLO }} world in {{ .Folder }}"},
|
|
|
|
{Case: "template text with user", ExpectedString: "hello Posh", Text: "{{ .Env.HELLO }} {{ .User }}"},
|
2021-10-06 12:50:52 -07:00
|
|
|
{Case: "empty text", Text: "", ExpectedDisabled: true},
|
|
|
|
{Case: "empty template result", Text: "{{ .Env.WORLD }}", ExpectedDisabled: true},
|
2021-08-09 20:48:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
env := new(MockedEnvironment)
|
2022-01-15 11:37:46 -08:00
|
|
|
env.On("getcwd").Return("/usr/home/posh")
|
|
|
|
env.On("homeDir").Return("/usr/home")
|
|
|
|
env.On("getPathSeperator").Return("/")
|
|
|
|
env.On("isRunningAsRoot").Return(true)
|
|
|
|
env.On("getShellName").Return("terminal")
|
2021-08-09 20:48:44 -07:00
|
|
|
env.On("getenv", "HELLO").Return("hello")
|
2021-10-06 12:50:52 -07:00
|
|
|
env.On("getenv", "WORLD").Return("")
|
2022-01-15 11:37:46 -08:00
|
|
|
env.On("getCurrentUser").Return("Posh")
|
|
|
|
env.On("getHostName").Return("MyHost", nil)
|
2022-01-12 14:39:34 -08:00
|
|
|
env.onTemplate()
|
2021-11-26 01:37:33 -08:00
|
|
|
txt := &text{
|
|
|
|
env: env,
|
2022-01-01 11:08:08 -08:00
|
|
|
props: properties{
|
2021-08-09 20:48:44 -07:00
|
|
|
TextProperty: tc.Text,
|
|
|
|
},
|
|
|
|
}
|
2021-10-06 12:50:52 -07:00
|
|
|
assert.Equal(t, tc.ExpectedDisabled, !txt.enabled(), tc.Case)
|
2021-08-09 20:48:44 -07:00
|
|
|
assert.Equal(t, tc.ExpectedString, txt.string(), tc.Case)
|
|
|
|
}
|
|
|
|
}
|