Skip to content

Commit 70a315c

Browse files
committed
chore: fix flaky test
1 parent 0d8bea3 commit 70a315c

7 files changed

Lines changed: 520 additions & 595 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package collections
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestCollections(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Collections Suite")
13+
}

collections/collections_test.go

Lines changed: 128 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,133 @@
11
package collections
22

33
import (
4-
"reflect"
5-
"testing"
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
66
)
77

8-
func Test_MergeMap(t *testing.T) {
9-
type args struct {
10-
a map[string]string
11-
b map[string]string
12-
}
13-
tests := []struct {
14-
name string
15-
args args
16-
want map[string]string
17-
}{
18-
{
19-
name: "no overlaps",
20-
args: args{
21-
a: map[string]string{"name": "flanksource"},
22-
b: map[string]string{"foo": "bar"},
23-
},
24-
want: map[string]string{
25-
"name": "flanksource",
26-
"foo": "bar",
27-
},
28-
},
29-
{
30-
name: "overlaps",
31-
args: args{
32-
a: map[string]string{"name": "flanksource", "foo": "baz"},
33-
b: map[string]string{"foo": "bar"},
34-
},
35-
want: map[string]string{
36-
"name": "flanksource",
37-
"foo": "bar",
38-
},
39-
},
40-
{
41-
name: "overlaps II",
42-
args: args{
43-
a: map[string]string{"name": "github", "foo": "baz"},
44-
b: map[string]string{"name": "flanksource", "foo": "bar"},
45-
},
46-
want: map[string]string{
47-
"name": "flanksource",
48-
"foo": "bar",
49-
},
50-
},
51-
{
52-
name: "ditto",
53-
args: args{
54-
a: map[string]string{"name": "flanksource", "foo": "bar"},
55-
b: map[string]string{"name": "flanksource", "foo": "bar"},
56-
},
57-
want: map[string]string{
58-
"name": "flanksource",
59-
"foo": "bar",
60-
},
61-
},
62-
{
63-
name: "nil a",
64-
args: args{
65-
a: nil,
66-
b: map[string]string{"name": "flanksource", "foo": "bar"},
67-
},
68-
want: map[string]string{
69-
"name": "flanksource",
70-
"foo": "bar",
71-
},
72-
},
73-
{
74-
name: "nil b",
75-
args: args{
76-
a: map[string]string{"name": "flanksource", "foo": "bar"},
77-
b: nil,
78-
},
79-
want: map[string]string{
80-
"name": "flanksource",
81-
"foo": "bar",
82-
},
83-
},
84-
{
85-
name: "both nil",
86-
args: args{
87-
a: nil,
88-
b: nil,
89-
},
90-
want: map[string]string{},
91-
},
92-
}
93-
94-
for _, tt := range tests {
95-
t.Run(tt.name, func(t *testing.T) {
96-
if got := MergeMap(tt.args.a, tt.args.b); !reflect.DeepEqual(got, tt.want) {
97-
t.Errorf("got %v, want %v", got, tt.want)
98-
}
99-
})
100-
}
101-
}
102-
103-
func Test_KeyValueSliceToMap(t *testing.T) {
104-
tests := []struct {
105-
name string
106-
args []string
107-
want map[string]string
108-
}{
109-
{name: "simple", args: []string{"name=flanksource"}, want: map[string]string{"name": "flanksource"}},
110-
{name: "white space", args: []string{" name = flanksource "}, want: map[string]string{"name": "flanksource"}},
111-
{name: "multiple-simple", args: []string{"name=flanksource", "foo=bar"}, want: map[string]string{"name": "flanksource", "foo": "bar"}},
112-
{name: "double-equal", args: []string{"name=foo=bar"}, want: map[string]string{"name": "foo=bar"}},
113-
}
114-
for _, tt := range tests {
115-
t.Run(tt.name, func(t *testing.T) {
116-
if got := KeyValueSliceToMap(tt.args); !reflect.DeepEqual(got, tt.want) {
117-
t.Errorf("got %v, want %v", got, tt.want)
118-
}
119-
})
120-
}
121-
}
8+
var _ = Describe("MergeMap", func() {
9+
It("should merge maps with no overlaps", func() {
10+
a := map[string]string{"name": "flanksource"}
11+
b := map[string]string{"foo": "bar"}
12+
expected := map[string]string{
13+
"name": "flanksource",
14+
"foo": "bar",
15+
}
16+
17+
result := MergeMap(a, b)
18+
19+
Expect(result).To(Equal(expected))
20+
})
21+
22+
It("should merge maps with overlaps, b takes precedence", func() {
23+
a := map[string]string{"name": "flanksource", "foo": "baz"}
24+
b := map[string]string{"foo": "bar"}
25+
expected := map[string]string{
26+
"name": "flanksource",
27+
"foo": "bar",
28+
}
29+
30+
result := MergeMap(a, b)
31+
32+
Expect(result).To(Equal(expected))
33+
})
34+
35+
It("should merge maps with multiple overlaps", func() {
36+
a := map[string]string{"name": "github", "foo": "baz"}
37+
b := map[string]string{"name": "flanksource", "foo": "bar"}
38+
expected := map[string]string{
39+
"name": "flanksource",
40+
"foo": "bar",
41+
}
42+
43+
result := MergeMap(a, b)
44+
45+
Expect(result).To(Equal(expected))
46+
})
47+
48+
It("should handle identical maps", func() {
49+
a := map[string]string{"name": "flanksource", "foo": "bar"}
50+
b := map[string]string{"name": "flanksource", "foo": "bar"}
51+
expected := map[string]string{
52+
"name": "flanksource",
53+
"foo": "bar",
54+
}
55+
56+
result := MergeMap(a, b)
57+
58+
Expect(result).To(Equal(expected))
59+
})
60+
61+
It("should handle nil first map", func() {
62+
var a map[string]string
63+
b := map[string]string{"name": "flanksource", "foo": "bar"}
64+
expected := map[string]string{
65+
"name": "flanksource",
66+
"foo": "bar",
67+
}
68+
69+
result := MergeMap(a, b)
70+
71+
Expect(result).To(Equal(expected))
72+
})
73+
74+
It("should handle nil second map", func() {
75+
a := map[string]string{"name": "flanksource", "foo": "bar"}
76+
var b map[string]string
77+
expected := map[string]string{
78+
"name": "flanksource",
79+
"foo": "bar",
80+
}
81+
82+
result := MergeMap(a, b)
83+
84+
Expect(result).To(Equal(expected))
85+
})
86+
87+
It("should handle both maps nil", func() {
88+
var a, b map[string]string
89+
expected := map[string]string{}
90+
91+
result := MergeMap(a, b)
92+
93+
Expect(result).To(Equal(expected))
94+
})
95+
})
96+
97+
var _ = Describe("KeyValueSliceToMap", func() {
98+
It("should convert simple key=value pair", func() {
99+
args := []string{"name=flanksource"}
100+
expected := map[string]string{"name": "flanksource"}
101+
102+
result := KeyValueSliceToMap(args)
103+
104+
Expect(result).To(Equal(expected))
105+
})
106+
107+
It("should handle whitespace around key=value pairs", func() {
108+
args := []string{" name = flanksource "}
109+
expected := map[string]string{"name": "flanksource"}
110+
111+
result := KeyValueSliceToMap(args)
112+
113+
Expect(result).To(Equal(expected))
114+
})
115+
116+
It("should convert multiple key=value pairs", func() {
117+
args := []string{"name=flanksource", "foo=bar"}
118+
expected := map[string]string{"name": "flanksource", "foo": "bar"}
119+
120+
result := KeyValueSliceToMap(args)
121+
122+
Expect(result).To(Equal(expected))
123+
})
124+
125+
It("should handle values with equal signs", func() {
126+
args := []string{"name=foo=bar"}
127+
expected := map[string]string{"name": "foo=bar"}
128+
129+
result := KeyValueSliceToMap(args)
130+
131+
Expect(result).To(Equal(expected))
132+
})
133+
})

collections/maps_test.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
11
package collections
22

3-
import "testing"
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
)
47

5-
func TestSortedMap(t *testing.T) {
6-
type args struct {
7-
labels map[string]string
8-
}
9-
tests := []struct {
10-
name string
11-
args args
12-
want string
13-
}{
14-
{
15-
name: "simple",
16-
args: args{
17-
labels: map[string]string{
18-
"b": "b",
19-
"a": "a",
20-
"c": "c",
21-
},
22-
},
23-
want: "a=a,b=b,c=c",
24-
},
25-
}
8+
var _ = Describe("SortedMap", func() {
9+
It("should sort map entries alphabetically", func() {
10+
labels := map[string]string{
11+
"b": "b",
12+
"a": "a",
13+
"c": "c",
14+
}
2615

27-
for _, tt := range tests {
28-
t.Run(tt.name, func(t *testing.T) {
29-
if got := SortedMap(tt.args.labels); got != tt.want {
30-
t.Errorf("SortedMap() = %v, want %v", got, tt.want)
31-
}
32-
})
33-
}
34-
}
16+
result := SortedMap(labels)
17+
18+
Expect(result).To(Equal("a=a,b=b,c=c"))
19+
})
20+
})

0 commit comments

Comments
 (0)