| 1 |
package util //nolint:revive |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
) |
| 6 |
|
| 7 |
func TestParseLabels(t *testing.T) { |
| 8 |
testCases := []struct { |
| 9 |
input string |
| 10 |
expectedOutput map[string]string |
| 11 |
shouldError bool |
| 12 |
}{ |
| 13 |
{ |
| 14 |
input: "app=myapp,env=prod,version=1.0", |
| 15 |
expectedOutput: map[string]string{ |
| 16 |
"app": "myapp", |
| 17 |
"env": "prod", |
| 18 |
"version": "1.0", |
| 19 |
}, |
| 20 |
shouldError: false, |
| 21 |
}, |
| 22 |
{ |
| 23 |
input: "app=myapp,env=prod,invalid", |
| 24 |
expectedOutput: nil, |
| 25 |
shouldError: true, |
| 26 |
}, |
| 27 |
{ |
| 28 |
input: "app=myapp", |
| 29 |
expectedOutput: map[string]string{ |
| 30 |
"app": "myapp", |
| 31 |
}, |
| 32 |
shouldError: false, |
| 33 |
}, |
| 34 |
{ |
| 35 |
input: "", |
| 36 |
expectedOutput: map[string]string{}, |
| 37 |
shouldError: true, |
| 38 |
}, |
| 39 |
{ |
| 40 |
input: " key = value , another = test ", |
| 41 |
expectedOutput: map[string]string{ |
| 42 |
"key": "value", |
| 43 |
"another": "test", |
| 44 |
}, |
| 45 |
shouldError: false, |
| 46 |
}, |
| 47 |
} |
| 48 |
|
| 49 |
for _, tc := range testCases { |
| 50 |
output, err := ParseLabels(tc.input) |
| 51 |
|
| 52 |
// Check for unexpected errors or missing errors |
| 53 |
if tc.shouldError && err == nil { |
| 54 |
t.Errorf("expected error for input %q but got none", tc.input) |
| 55 |
continue |
| 56 |
} |
| 57 |
if !tc.shouldError && err != nil { |
| 58 |
t.Errorf("did not expect error for input %q but got: %v", tc.input, err) |
| 59 |
continue |
| 60 |
} |
| 61 |
|
| 62 |
// Compare maps if there was no error |
| 63 |
if !tc.shouldError { |
| 64 |
if len(output) != len(tc.expectedOutput) { |
| 65 |
t.Errorf("for input %q, expected map length %d but got %d", tc.input, len(tc.expectedOutput), len(output)) |
| 66 |
} |
| 67 |
for key, expectedValue := range tc.expectedOutput { |
| 68 |
if output[key] != expectedValue { |
| 69 |
t.Errorf("for input %q, expected %q=%q but got %q=%q", tc.input, key, expectedValue, key, output[key]) |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |