Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

Commit af57352

Browse files
committed
Split off deduplicate()/test
1 parent 1eb9f92 commit af57352

4 files changed

Lines changed: 62 additions & 36 deletions

File tree

cli/cli.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -289,20 +289,3 @@ func execCommand(cmd *cobra.Command, args []string) error {
289289
return syscallExec(src, append([]string{src}, args...), os.Environ())
290290
}
291291

292-
/*
293-
* deduplicate a slice of strings, keeping the order of the elements
294-
*/
295-
func deduplicate(input []string) []string {
296-
var output []string
297-
unique := map[string]interface{}{}
298-
for _, i := range input {
299-
unique[i] = new(interface{})
300-
}
301-
for _, i := range input {
302-
if _, ok := unique[i]; ok {
303-
output = append(output, i)
304-
delete(unique, i)
305-
}
306-
}
307-
return output
308-
}

cli/cli_test.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io/ioutil"
88
"os"
99
"path/filepath"
10-
"strings"
1110
"syscall"
1211
"testing"
1312

@@ -103,24 +102,6 @@ func TestInitEditing(t *testing.T) {
103102
})
104103
}
105104

106-
func TestDeduplicate(t *testing.T) {
107-
t.Run("simple", func(t *testing.T) {
108-
assert.Equal(t, []string{"a"}, deduplicate(strings.Split("aaaaaa", "")))
109-
})
110-
111-
t.Run("two elements", func(t *testing.T) {
112-
assert.Equal(t, []string{"a", "b"}, deduplicate(strings.Split("aaaabb", "")))
113-
})
114-
115-
t.Run("two elements repeated", func(t *testing.T) {
116-
assert.Equal(t, []string{"a", "b"}, deduplicate(strings.Split("ababab", "")))
117-
})
118-
119-
t.Run("maintains ordering", func(t *testing.T) {
120-
assert.Equal(t, []string{"a", "c", "b"}, deduplicate(strings.Split("acbabab", "")))
121-
})
122-
}
123-
124105
func TestCommandFromScript(t *testing.T) {
125106
t.Run("happy path", func(t *testing.T) {
126107
f, err := ioutil.TempFile("", "test-command-from-script")

cli/utils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cli
2+
3+
/*
4+
* deduplicate a slice of strings, keeping the order of the elements
5+
*/
6+
func deduplicate(input []string) []string {
7+
var output []string
8+
unique := map[string]interface{}{}
9+
for _, i := range input {
10+
unique[i] = new(interface{})
11+
}
12+
for _, i := range input {
13+
if _, ok := unique[i]; ok {
14+
output = append(output, i)
15+
delete(unique, i)
16+
}
17+
}
18+
return output
19+
}

cli/utils_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cli
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestDeduplicate(t *testing.T) {
11+
var tests = []struct {
12+
name string
13+
input []string
14+
expected []string
15+
}{
16+
{
17+
"simple",
18+
strings.Split("aaaaaa", ""),
19+
[]string{"a"},
20+
},
21+
{
22+
"two elements",
23+
[]string{"a", "b"},
24+
deduplicate(strings.Split("aaaabb", "")),
25+
},
26+
{
27+
"two elements repeated",
28+
deduplicate(strings.Split("ababab", "")),
29+
[]string{"a", "b"},
30+
},
31+
{
32+
"maintains ordering",
33+
deduplicate(strings.Split("acbaabab", "")),
34+
[]string{"a", "c", "b"},
35+
},
36+
}
37+
38+
for _, test := range tests {
39+
t.Run(test.name, func(t *testing.T) {
40+
assert.Equal(t, test.expected, deduplicate(test.input))
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)