Skip to content

Commit ada3659

Browse files
authored
make EqualStringSlice to generic EqualSliceValues (#2179)
just a fly-by refactoring
1 parent b7af776 commit ada3659

File tree

7 files changed

+60
-57
lines changed

7 files changed

+60
-57
lines changed

server/forge/gitea/gitea_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func Test_gitea(t *testing.T) {
161161
g.Assert(b).IsNotNil()
162162
g.Assert(err).IsNil()
163163
g.Assert(b.Event).Equal(model.EventPull)
164-
g.Assert(utils.EqualStringSlice(b.ChangedFiles, []string{"README.md"})).IsTrue()
164+
g.Assert(utils.EqualSliceValues(b.ChangedFiles, []string{"README.md"})).IsTrue()
165165
})
166166
})
167167
}

server/forge/gitea/helper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func Test_parse(t *testing.T) {
104104
g.Assert(pipeline.Message).Equal(hook.Commits[0].Message)
105105
g.Assert(pipeline.Avatar).Equal("http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
106106
g.Assert(pipeline.Author).Equal(hook.Sender.UserName)
107-
g.Assert(utils.EqualStringSlice(pipeline.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue()
107+
g.Assert(utils.EqualSliceValues(pipeline.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue()
108108
})
109109

110110
g.It("Should return a Repo struct from a push hook", func() {

server/forge/gitea/parse_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Test_parser(t *testing.T) {
6464
g.Assert(r).IsNotNil()
6565
g.Assert(b).IsNotNil()
6666
g.Assert(b.Event).Equal(model.EventPush)
67-
g.Assert(utils.EqualStringSlice(b.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue()
67+
g.Assert(utils.EqualSliceValues(b.ChangedFiles, []string{"CHANGELOG.md", "app/controller/application.rb"})).IsTrue()
6868
})
6969
})
7070
g.Describe("given a push hook from an branch creation", func() {

shared/utils/slices.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,29 @@ func MergeSlices[T any](slices ...[]T) []T {
3131
}
3232
return result
3333
}
34+
35+
// EqualSliceValues compare two slices if they have equal values independent of how they are sorted
36+
func EqualSliceValues[E comparable](s1, s2 []E) bool {
37+
if len(s1) != len(s2) {
38+
return false
39+
}
40+
41+
m1 := sliceToCountMap(s1)
42+
m2 := sliceToCountMap(s2)
43+
44+
for k, v := range m1 {
45+
if m2[k] != v {
46+
return false
47+
}
48+
}
49+
50+
return true
51+
}
52+
53+
func sliceToCountMap[E comparable](list []E) map[E]int {
54+
m := make(map[E]int)
55+
for i := range list {
56+
m[list[i]]++
57+
}
58+
return m
59+
}

shared/utils/slices_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,34 @@ func TestMergeSlices(t *testing.T) {
2727
resultIS := MergeSlices([]int{}, []int{1, 2}, []int{4}, nil)
2828
assert.EqualValues(t, []int{1, 2, 4}, resultIS)
2929
}
30+
31+
func TestEqualSliceValues(t *testing.T) {
32+
tests := []struct {
33+
in1 []string
34+
in2 []string
35+
out bool
36+
}{{
37+
in1: []string{"", "ab", "12", "ab"},
38+
in2: []string{"12", "ab"},
39+
out: false,
40+
}, {
41+
in1: nil,
42+
in2: nil,
43+
out: true,
44+
}, {
45+
in1: []string{"AA", "AA", "2", " "},
46+
in2: []string{"2", "AA", " ", "AA"},
47+
out: true,
48+
}, {
49+
in1: []string{"AA", "AA", "2", " "},
50+
in2: []string{"2", "2", " ", "AA"},
51+
out: false,
52+
}}
53+
54+
for _, tc := range tests {
55+
assert.EqualValues(t, tc.out, EqualSliceValues(tc.in1, tc.in2), "could not correctly process input: '%#v', %#v", tc.in1, tc.in2)
56+
}
57+
58+
assert.True(t, EqualSliceValues([]bool{true, false, false}, []bool{false, false, true}))
59+
assert.False(t, EqualSliceValues([]bool{true, false, false}, []bool{true, false, true}))
60+
}

shared/utils/strings.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,3 @@ func DedupStrings(src []string) []string {
3434

3535
return dst
3636
}
37-
38-
// EqualStringSlice compare two string slices if they have equal values independent of how they are sorted
39-
func EqualStringSlice(l1, l2 []string) bool {
40-
if len(l1) != len(l2) {
41-
return false
42-
}
43-
44-
m1 := sliceToCountMap(l1)
45-
m2 := sliceToCountMap(l2)
46-
47-
for k, v := range m1 {
48-
if m2[k] != v {
49-
return false
50-
}
51-
}
52-
53-
return true
54-
}
55-
56-
func sliceToCountMap(list []string) map[string]int {
57-
m := make(map[string]int)
58-
for i := range list {
59-
m[list[i]]++
60-
}
61-
return m
62-
}

shared/utils/strings_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,3 @@ func TestDedupStrings(t *testing.T) {
4646
}
4747
}
4848
}
49-
50-
func TestEqualStringSlice(t *testing.T) {
51-
tests := []struct {
52-
in1 []string
53-
in2 []string
54-
out bool
55-
}{{
56-
in1: []string{"", "ab", "12", "ab"},
57-
in2: []string{"12", "ab"},
58-
out: false,
59-
}, {
60-
in1: nil,
61-
in2: nil,
62-
out: true,
63-
}, {
64-
in1: []string{"AA", "AA", "2", " "},
65-
in2: []string{"2", "AA", " ", "AA"},
66-
out: true,
67-
}, {
68-
in1: []string{"AA", "AA", "2", " "},
69-
in2: []string{"2", "2", " ", "AA"},
70-
out: false,
71-
}}
72-
73-
for _, tc := range tests {
74-
assert.EqualValues(t, tc.out, EqualStringSlice(tc.in1, tc.in2), "could not correctly process input: '%#v', %#v", tc.in1, tc.in2)
75-
}
76-
}

0 commit comments

Comments
 (0)