2022-01-26 06:54:36 -08:00
package segments
2021-12-11 13:08:47 -08:00
import (
"testing"
2023-01-05 12:57:38 -08:00
"github.com/jandedobbeleer/oh-my-posh/src/properties"
2024-07-02 03:02:57 -07:00
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
2024-07-03 00:04:11 -07:00
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
2022-12-28 08:30:48 -08:00
2021-12-11 13:08:47 -08:00
"github.com/stretchr/testify/assert"
)
func TestScmStatusChanged ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected bool
Status ScmStatus
} {
{
Case : "No changes" ,
Expected : false ,
Status : ScmStatus { } ,
} ,
{
Case : "Added" ,
Expected : true ,
Status : ScmStatus {
Added : 1 ,
} ,
} ,
{
Case : "Moved" ,
Expected : true ,
Status : ScmStatus {
Moved : 1 ,
} ,
} ,
{
Case : "Modified" ,
Expected : true ,
Status : ScmStatus {
Modified : 1 ,
} ,
} ,
{
Case : "Deleted" ,
Expected : true ,
Status : ScmStatus {
Deleted : 1 ,
} ,
} ,
{
Case : "Unmerged" ,
Expected : true ,
Status : ScmStatus {
Unmerged : 1 ,
} ,
} ,
}
for _ , tc := range cases {
assert . Equal ( t , tc . Expected , tc . Status . Changed ( ) , tc . Case )
}
}
2023-06-25 23:44:17 -07:00
func TestScmStatusString ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected string
Status ScmStatus
} {
{
Case : "Unmerged" ,
Expected : "x1" ,
Status : ScmStatus {
Unmerged : 1 ,
} ,
} ,
{
Case : "Unmerged and Modified" ,
Expected : "~3 x1" ,
Status : ScmStatus {
Unmerged : 1 ,
Modified : 3 ,
} ,
} ,
{
Case : "Empty" ,
Status : ScmStatus { } ,
} ,
{
Case : "Format override" ,
Expected : "Added: 1" ,
Status : ScmStatus {
Added : 1 ,
Formats : map [ string ] string {
"Added" : "Added: %d" ,
} ,
} ,
} ,
2021-12-11 13:08:47 -08:00
}
2023-06-25 23:44:17 -07:00
for _ , tc := range cases {
assert . Equal ( t , tc . Expected , tc . Status . String ( ) , tc . Case )
2021-12-11 13:08:47 -08:00
}
}
func TestTruncateBranch ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected string
Branch string
FullBranch bool
2023-09-19 10:25:35 -07:00
MaxLength any
2021-12-11 13:08:47 -08:00
} {
{ Case : "No limit" , Expected : "are-belong-to-us" , Branch : "/all-your-base/are-belong-to-us" , FullBranch : false } ,
{ Case : "No limit - larger" , Expected : "are-belong" , Branch : "/all-your-base/are-belong-to-us" , FullBranch : false , MaxLength : 10.0 } ,
{ Case : "No limit - smaller" , Expected : "all-your-base" , Branch : "/all-your-base" , FullBranch : false , MaxLength : 13.0 } ,
{ Case : "Invalid setting" , Expected : "all-your-base" , Branch : "/all-your-base" , FullBranch : false , MaxLength : "burp" } ,
{ Case : "Lower than limit" , Expected : "all-your-base" , Branch : "/all-your-base" , FullBranch : false , MaxLength : 20.0 } ,
{ Case : "No limit - full branch" , Expected : "/all-your-base/are-belong-to-us" , Branch : "/all-your-base/are-belong-to-us" , FullBranch : true } ,
{ Case : "No limit - larger - full branch" , Expected : "/all-your-base" , Branch : "/all-your-base/are-belong-to-us" , FullBranch : true , MaxLength : 14.0 } ,
{ Case : "No limit - smaller - full branch " , Expected : "/all-your-base" , Branch : "/all-your-base" , FullBranch : true , MaxLength : 14.0 } ,
{ Case : "Invalid setting - full branch" , Expected : "/all-your-base" , Branch : "/all-your-base" , FullBranch : true , MaxLength : "burp" } ,
{ Case : "Lower than limit - full branch" , Expected : "/all-your-base" , Branch : "/all-your-base" , FullBranch : true , MaxLength : 20.0 } ,
}
for _ , tc := range cases {
2022-01-26 04:53:35 -08:00
props := properties . Map {
2021-12-11 13:08:47 -08:00
BranchMaxLength : tc . MaxLength ,
FullBranchPath : tc . FullBranch ,
}
2022-01-26 05:10:18 -08:00
p := & Plastic {
2021-12-11 13:08:47 -08:00
scm : scm {
props : props ,
} ,
}
assert . Equal ( t , tc . Expected , p . truncateBranch ( tc . Branch ) , tc . Case )
}
}
func TestTruncateBranchWithSymbol ( t * testing . T ) {
cases := [ ] struct {
Case string
Expected string
Branch string
FullBranch bool
2023-09-19 10:25:35 -07:00
MaxLength any
TruncateSymbol any
2021-12-11 13:08:47 -08:00
} {
{ Case : "No limit" , Expected : "are-belong-to-us" , Branch : "/all-your-base/are-belong-to-us" , FullBranch : false , TruncateSymbol : "..." } ,
{ Case : "No limit - larger" , Expected : "are-belong..." , Branch : "/all-your-base/are-belong-to-us" , FullBranch : false , MaxLength : 10.0 , TruncateSymbol : "..." } ,
{ Case : "No limit - smaller" , Expected : "all-your-base" , Branch : "/all-your-base" , FullBranch : false , MaxLength : 13.0 , TruncateSymbol : "..." } ,
{ Case : "Invalid setting" , Expected : "all-your-base" , Branch : "/all-your-base" , FullBranch : false , MaxLength : "burp" , TruncateSymbol : "..." } ,
{ Case : "Lower than limit" , Expected : "all-your-base" , Branch : "/all-your-base" , FullBranch : false , MaxLength : 20.0 , TruncateSymbol : "..." } ,
{ Case : "No limit - full branch" , Expected : "/all-your-base/are-belong-to-us" , Branch : "/all-your-base/are-belong-to-us" , FullBranch : true , TruncateSymbol : "..." } ,
{ Case : "No limit - larger - full branch" , Expected : "/all-your-base..." , Branch : "/all-your-base/are-belong-to-us" , FullBranch : true , MaxLength : 14.0 , TruncateSymbol : "..." } ,
{ Case : "No limit - smaller - full branch " , Expected : "/all-your-base" , Branch : "/all-your-base" , FullBranch : true , MaxLength : 14.0 , TruncateSymbol : "..." } ,
{ Case : "Invalid setting - full branch" , Expected : "/all-your-base" , Branch : "/all-your-base" , FullBranch : true , MaxLength : "burp" , TruncateSymbol : "..." } ,
{ Case : "Lower than limit - full branch" , Expected : "/all-your-base" , Branch : "/all-your-base" , FullBranch : true , MaxLength : 20.0 , TruncateSymbol : "..." } ,
}
for _ , tc := range cases {
2022-01-26 04:53:35 -08:00
props := properties . Map {
2021-12-11 13:08:47 -08:00
BranchMaxLength : tc . MaxLength ,
TruncateSymbol : tc . TruncateSymbol ,
FullBranchPath : tc . FullBranch ,
}
2022-01-26 05:10:18 -08:00
p := & Plastic {
2021-12-11 13:08:47 -08:00
scm : scm {
props : props ,
} ,
}
assert . Equal ( t , tc . Expected , p . truncateBranch ( tc . Branch ) , tc . Case )
}
}
2022-09-14 05:50:08 -07:00
func TestHasCommand ( t * testing . T ) {
cases := [ ] struct {
Case string
ExpectedCommand string
Command string
GOOS string
IsWslSharedPath bool
NativeFallback bool
} {
2024-07-02 03:02:57 -07:00
{ Case : "On Windows" , ExpectedCommand : "git.exe" , GOOS : runtime . WINDOWS } ,
2022-09-14 05:50:08 -07:00
{ Case : "Cache" , ExpectedCommand : "git.exe" , Command : "git.exe" } ,
{ Case : "Non Windows" , ExpectedCommand : "git" } ,
{ Case : "Iside WSL2, non shared" , ExpectedCommand : "git" } ,
{ Case : "Iside WSL2, shared" , ExpectedCommand : "git.exe" , IsWslSharedPath : true } ,
{ Case : "Iside WSL2, shared fallback" , ExpectedCommand : "git" , IsWslSharedPath : true , NativeFallback : true } ,
}
for _ , tc := range cases {
2024-07-03 00:04:11 -07:00
env := new ( mock . Environment )
2022-09-14 05:50:08 -07:00
env . On ( "GOOS" ) . Return ( tc . GOOS )
env . On ( "InWSLSharedDrive" ) . Return ( tc . IsWslSharedPath )
env . On ( "HasCommand" , "git" ) . Return ( true )
env . On ( "HasCommand" , "git.exe" ) . Return ( ! tc . NativeFallback )
s := & scm {
env : env ,
props : properties . Map {
NativeFallback : tc . NativeFallback ,
} ,
command : tc . Command ,
}
_ = s . hasCommand ( GITCOMMAND )
assert . Equal ( t , tc . ExpectedCommand , s . command , tc . Case )
}
}