From 87e54cac019aa042e988074c0073f8f0ad568f0b Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 11 Oct 2020 13:32:21 +0200 Subject: [PATCH] refactor(git): add tests --- segment_git_test.go | 86 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/segment_git_test.go b/segment_git_test.go index dc58a257..a658388f 100755 --- a/segment_git_test.go +++ b/segment_git_test.go @@ -287,3 +287,89 @@ func TestParseGitBranchInfoNoRemote(t *testing.T) { assert.Equal(t, "master", got["local"]) assert.Empty(t, got["upstream"]) } + +func TestGitStatusUnmerged(t *testing.T) { + expected := " working: x1" + status := &gitStatus{ + unmerged: 1, + } + assert.Equal(t, expected, status.string("working:")) +} + +func TestGitStatusUnmergedModified(t *testing.T) { + expected := " working: ~3 x1" + status := &gitStatus{ + unmerged: 1, + modified: 3, + } + assert.Equal(t, expected, status.string("working:")) +} + +func TestGitStatusEmpty(t *testing.T) { + expected := "" + status := &gitStatus{} + assert.Equal(t, expected, status.string("working:")) +} + +func TestParseGitStatsWorking(t *testing.T) { + g := &git{} + output := []string{ + "## amazing-feat", + " M change.go", + "DD change.go", + " ? change.go", + " ? change.go", + " A change.go", + " U change.go", + " R change.go", + " C change.go", + } + status := g.parseGitStats(output, true) + assert.Equal(t, 3, status.modified) + assert.Equal(t, 1, status.unmerged) + assert.Equal(t, 1, status.added) + assert.Equal(t, 1, status.deleted) + assert.Equal(t, 2, status.untracked) +} + +func TestParseGitStatsStaging(t *testing.T) { + g := &git{} + output := []string{ + "## amazing-feat", + " M change.go", + "DD change.go", + " ? change.go", + "?? change.go", + " A change.go", + "DU change.go", + "MR change.go", + "AC change.go", + } + status := g.parseGitStats(output, false) + assert.Equal(t, 1, status.modified) + assert.Equal(t, 0, status.unmerged) + assert.Equal(t, 1, status.added) + assert.Equal(t, 2, status.deleted) + assert.Equal(t, 1, status.untracked) +} + +func TestParseGitStatsNoChanges(t *testing.T) { + g := &git{} + expected := &gitStatus{} + output := []string{ + "## amazing-feat", + } + status := g.parseGitStats(output, false) + assert.Equal(t, expected, status) +} + +func TestParseGitStatsInvalidLine(t *testing.T) { + g := &git{} + expected := &gitStatus{} + output := []string{ + "## amazing-feat", + "#", + } + status := g.parseGitStats(output, false) + assert.Equal(t, expected, status) +}