From 2008c3951616f9b366b320e805dcbd0c5da6ce96 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Fri, 22 Oct 2021 20:18:40 +1100 Subject: add tests for dealing with remotes --- pkg/gui/custom_commands_test.go | 63 +++++++++++++++++++++++++++++++++++++++++ pkg/gui/gui_test.go | 60 ++------------------------------------- 2 files changed, 65 insertions(+), 58 deletions(-) create mode 100644 pkg/gui/custom_commands_test.go (limited to 'pkg/gui') diff --git a/pkg/gui/custom_commands_test.go b/pkg/gui/custom_commands_test.go new file mode 100644 index 000000000..d51617c09 --- /dev/null +++ b/pkg/gui/custom_commands_test.go @@ -0,0 +1,63 @@ +package gui + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGuiGenerateMenuCandidates(t *testing.T) { + type scenario struct { + testName string + cmdOut string + filter string + valueFormat string + labelFormat string + test func([]commandMenuEntry, error) + } + + scenarios := []scenario{ + { + "Extract remote branch name", + "upstream/pr-1", + "(?P[a-z_]+)/(?P.*)", + "{{ .branch }}", + "Remote: {{ .remote }}", + func(actualEntry []commandMenuEntry, err error) { + assert.NoError(t, err) + assert.EqualValues(t, "pr-1", actualEntry[0].value) + assert.EqualValues(t, "Remote: upstream", actualEntry[0].label) + }, + }, + { + "Multiple named groups with empty labelFormat", + "upstream/pr-1", + "(?P[a-z]*)/(?P.*)", + "{{ .branch }}|{{ .remote }}", + "", + func(actualEntry []commandMenuEntry, err error) { + assert.NoError(t, err) + assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value) + assert.EqualValues(t, "pr-1|upstream", actualEntry[0].label) + }, + }, + { + "Multiple named groups with group ids", + "upstream/pr-1", + "(?P[a-z]*)/(?P.*)", + "{{ .group_2 }}|{{ .group_1 }}", + "Remote: {{ .group_1 }}", + func(actualEntry []commandMenuEntry, err error) { + assert.NoError(t, err) + assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value) + assert.EqualValues(t, "Remote: upstream", actualEntry[0].label) + }, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + s.test(NewDummyGui().GenerateMenuCandidates(s.cmdOut, s.filter, s.valueFormat, s.labelFormat)) + }) + } +} diff --git a/pkg/gui/gui_test.go b/pkg/gui/gui_test.go index 20fec1b37..6e7a26b79 100644 --- a/pkg/gui/gui_test.go +++ b/pkg/gui/gui_test.go @@ -56,8 +56,8 @@ func Test(t *testing.T) { updateSnapshots, record, speedEnv, - func(t *testing.T, expected string, actual string) { - assert.Equal(t, expected, actual, fmt.Sprintf("expected:\n%s\nactual:\n%s\n", expected, actual)) + func(t *testing.T, expected string, actual string, prefix string) { + assert.Equal(t, expected, actual, fmt.Sprintf("Unexpected %s. Expected:\n%s\nActual:\n%s\n", prefix, expected, actual)) }, includeSkipped, ) @@ -81,59 +81,3 @@ func runCmdHeadless(cmd *exec.Cmd) error { return f.Close() } - -func TestGuiGenerateMenuCandidates(t *testing.T) { - type scenario struct { - testName string - cmdOut string - filter string - valueFormat string - labelFormat string - test func([]commandMenuEntry, error) - } - - scenarios := []scenario{ - { - "Extract remote branch name", - "upstream/pr-1", - "(?P[a-z_]+)/(?P.*)", - "{{ .branch }}", - "Remote: {{ .remote }}", - func(actualEntry []commandMenuEntry, err error) { - assert.NoError(t, err) - assert.EqualValues(t, "pr-1", actualEntry[0].value) - assert.EqualValues(t, "Remote: upstream", actualEntry[0].label) - }, - }, - { - "Multiple named groups with empty labelFormat", - "upstream/pr-1", - "(?P[a-z]*)/(?P.*)", - "{{ .branch }}|{{ .remote }}", - "", - func(actualEntry []commandMenuEntry, err error) { - assert.NoError(t, err) - assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value) - assert.EqualValues(t, "pr-1|upstream", actualEntry[0].label) - }, - }, - { - "Multiple named groups with group ids", - "upstream/pr-1", - "(?P[a-z]*)/(?P.*)", - "{{ .group_2 }}|{{ .group_1 }}", - "Remote: {{ .group_1 }}", - func(actualEntry []commandMenuEntry, err error) { - assert.NoError(t, err) - assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value) - assert.EqualValues(t, "Remote: upstream", actualEntry[0].label) - }, - }, - } - - for _, s := range scenarios { - t.Run(s.testName, func(t *testing.T) { - s.test(NewDummyGui().GenerateMenuCandidates(s.cmdOut, s.filter, s.valueFormat, s.labelFormat)) - }) - } -} -- cgit v1.2.3