Skip to content

Commit 907a8a5

Browse files
committed
add test and reformat
1 parent de8f0b3 commit 907a8a5

File tree

1 file changed

+51
-48
lines changed

1 file changed

+51
-48
lines changed

internal/utils/config_test.go

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,60 @@
11
package utils
22

33
import (
4-
"k8s.io/utils/diff"
5-
"reflect"
4+
"github.com/stretchr/testify/assert"
65
"testing"
76
)
87

9-
// Please, one func per test for readability
10-
118
func TestParseCustomLabels(t *testing.T) {
12-
testCases := []struct {
13-
given string
14-
expect map[string]string
15-
}{
16-
{
17-
given: "test=123,123=test",
18-
expect: map[string]string{"test": "123", "123": "test"},
19-
//description: "",
20-
21-
},
22-
{
23-
given: "test=123123=test",
24-
expect: map[string]string{"test": "123123=test"},
25-
},
26-
{
27-
given: "test123,123test",
28-
expect: map[string]string{},
29-
},
30-
{
31-
given: "customer=ca,creator=kubi,test=123123=test",
32-
expect: map[string]string{"test": "123123=test"},
33-
},
34-
{
35-
given: "creator=kubi",
36-
expect: map[string]string{},
37-
},
38-
}
39-
40-
for index, testCase := range testCases {
41-
result := parseCustomLabels(testCase.given)
42-
if !reflect.DeepEqual(testCase.expect, result) {
43-
t.Errorf("Test Case %d, Unexpected result\nDiff:\n %s",
44-
index,
45-
diff.ObjectGoPrintSideBySide(testCase.expect, result))
46-
}
47-
48-
}
499

50-
}
10+
t.Run("properly formatted k/v string should return two labels", func(t *testing.T) {
11+
result := parseCustomLabels("test=123,123=test")
12+
assert.Equal(t, result, map[string]string{"test": "123", "123": "test"})
13+
})
14+
15+
t.Run("two equal without a coma should return a single label containing an equal", func(t *testing.T) {
16+
result := parseCustomLabels("test=123123=test")
17+
assert.Equal(t, result, map[string]string{"test": "123123=test"})
18+
})
19+
20+
t.Run("two separated strings without equals should not return labels", func(t *testing.T) {
21+
result := parseCustomLabels("test123,123test")
22+
assert.Equal(t, result, map[string]string{})
23+
})
24+
25+
t.Run("three k/v containing two illegals key should return a single label", func(t *testing.T) {
26+
result := parseCustomLabels("customer=ca,creator=kubi,test=123123")
27+
assert.Equal(t, result, map[string]string{"test": "123123"})
28+
})
29+
30+
t.Run("a single illegal label should not return label", func(t *testing.T) {
31+
result := parseCustomLabels("creator=kubi")
32+
assert.Equal(t, result, map[string]string{})
33+
})
34+
35+
t.Run("a single illegal label should not return label", func(t *testing.T) {
36+
result := parseCustomLabels("creator=kubi")
37+
assert.Equal(t, result, map[string]string{})
38+
})
39+
40+
t.Run("an illegal key should not return label", func(t *testing.T) {
41+
result := parseCustomLabels("creator=")
42+
assert.Equal(t, result, map[string]string{})
43+
})
44+
45+
t.Run("a single key should not return label", func(t *testing.T) {
46+
result := parseCustomLabels("test=")
47+
assert.Equal(t, result, map[string]string{})
48+
})
49+
50+
t.Run("a value should not return label", func(t *testing.T) {
51+
result := parseCustomLabels("=test")
52+
assert.Equal(t, result, map[string]string{})
53+
})
54+
55+
t.Run("a string should not return label", func(t *testing.T) {
56+
result := parseCustomLabels("test")
57+
assert.Equal(t, result, map[string]string{})
58+
})
5159

52-
// Use more combination of failure
53-
//given: "customer==ca",
54-
//given: "cust=omer=ca",
55-
//given: "customer=",
56-
//given: "customer=,",
57-
//given: "=ca",
60+
}

0 commit comments

Comments
 (0)