Removed unused functions

Signed-off-by: Mehdi Moussaif <m.moussaif42@gmail.com>
This commit is contained in:
Mehdi Moussaif
2023-11-26 16:07:46 +01:00
parent efd2f7e77f
commit 70a010976e
2 changed files with 0 additions and 141 deletions

View File

@@ -5,34 +5,8 @@ import (
"os"
"sort"
"strconv"
"strings"
)
// ConvertLabelsToString converts a map of labels to a semicolon-separated string
func ConvertLabelsToString(labels map[string]string) string {
var builder strings.Builder
delimiter := ""
for k, v := range labels {
builder.WriteString(fmt.Sprintf("%s%s=%s", delimiter, k, v))
delimiter = ";"
}
return builder.String()
}
// ConvertStringToLabels converts a semicolon-separated string to a map of labels
func ConvertStringToLabels(labelsStr string) map[string]string {
labels := make(map[string]string)
labelsSlice := strings.Split(labelsStr, ";")
for _, label := range labelsSlice {
kvSlice := strings.SplitN(label, "=", 2)
if len(kvSlice) != 2 {
continue
}
labels[kvSlice[0]] = kvSlice[1]
}
return labels
}
func StringSlicesAreEqual(a, b []string) bool {
if len(a) != len(b) {
return false

View File

@@ -1,126 +1,11 @@
package cautils
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestConvertLabelsToString(t *testing.T) {
str := "a=b;c=d"
strMap := map[string]string{"a": "b", "c": "d"}
rsrt := ConvertLabelsToString(strMap)
spilltedA := strings.Split(rsrt, ";")
spilltedB := strings.Split(str, ";")
for i := range spilltedA {
exists := false
for j := range spilltedB {
if spilltedB[j] == spilltedA[i] {
exists = true
}
}
if !exists {
t.Errorf("%s != %s", spilltedA[i], spilltedB[i])
}
}
}
func TestConvertLabelsToString_EdgeCases(t *testing.T) {
// Test case 1: Empty map
emptyMap := make(map[string]string)
result := ConvertLabelsToString(emptyMap)
expected := ""
if result != expected {
t.Errorf("Empty map test failed, expected: '%s', got: '%s'", expected, result)
}
// Test case 2: Single pair in map
singlePairMap := map[string]string{"key": "value"}
result = ConvertLabelsToString(singlePairMap)
expected = "key=value"
if result != expected {
t.Errorf("Single pair test failed, expected: '%s', got: '%s'", expected, result)
}
// Test case 3: Multiple pairs in map
multiplePairsMap := map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"}
result = ConvertLabelsToString(multiplePairsMap)
expected = "key1=value1;key2=value2;key3=value3"
if result != expected {
t.Errorf("Multiple pairs test failed, expected: '%s', got: '%s'", expected, result)
}
// Test case 4: Special characters in keys or values
specialCharsMap := map[string]string{"key with spaces": "value with symbols (!@#)", "key\nwith\nnewlines": "value\rwith\rreturns"}
result = ConvertLabelsToString(specialCharsMap)
expected = "key with spaces=value with symbols (!@#);key\nwith\nnewlines=value\rwith\rreturns"
if result != expected {
t.Errorf("Special characters test failed, expected: '%s', got: '%s'", expected, result)
}
// Test case 5: Reserved characters in keys or values
reservedCharsMap := map[string]string{"key=with=equal=sign": "value;with;semicolon", "key;with;semicolon": "value=with=equal=sign"}
result = ConvertLabelsToString(reservedCharsMap)
expected = "key=with=equal=sign=value;with;semicolon;key;with;semicolon=value=with=equal=sign"
if result != expected {
t.Errorf("Reserved characters test failed, expected: '%s', got: '%s'", expected, result)
}
}
func TestConvertStringToLabels(t *testing.T) {
str := "a=b;c=d"
strMap := map[string]string{"a": "b", "c": "d"}
rstrMap := ConvertStringToLabels(str)
if fmt.Sprintf("%v", rstrMap) != fmt.Sprintf("%v", strMap) {
t.Errorf("%s != %s", fmt.Sprintf("%v", rstrMap), fmt.Sprintf("%v", strMap))
}
}
func TestConvertStringToLabels_EdgeCases(t *testing.T) {
// Test case 1: Empty string
result := ConvertStringToLabels("")
expected := map[string]string{}
if fmt.Sprintf("%v", result) != fmt.Sprintf("%v", expected) {
t.Errorf("Empty string test failed, expected: %v, got: %v", expected, result)
}
// Test case 2: Single pair in string
result = ConvertStringToLabels("key=value")
expected = map[string]string{"key": "value"}
if fmt.Sprintf("%v", result) != fmt.Sprintf("%v", expected) {
t.Errorf("Single pair test failed, expected: %v, got: %v", expected, result)
}
// Test case 3: Multiple pairs in string
result = ConvertStringToLabels("key1=value1;key2=value2;key3=value3")
expected = map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"}
if fmt.Sprintf("%v", result) != fmt.Sprintf("%v", expected) {
t.Errorf("Multiple pairs test failed, expected: %v, got: %v", expected, result)
}
// Test case 4: Special characters in string
result = ConvertStringToLabels("key with spaces=value with symbols (!@#);key\nwith\nnewlines=value\rwith\rreturns")
expected = map[string]string{"key with spaces": "value with symbols (!@#)", "key\nwith\nnewlines": "value\rwith\rreturns"}
if fmt.Sprintf("%v", result) != fmt.Sprintf("%v", expected) {
t.Errorf("Special characters test failed, expected: %v, got: %v", expected, result)
}
// Test case 5: Malformed string
result = ConvertStringToLabels("key=value;key2")
expected = map[string]string{"key": "value"}
if fmt.Sprintf("%v", result) != fmt.Sprintf("%v", expected) {
t.Errorf("Malformed string test failed, expected: %v, got: %v", expected, result)
}
result = ConvertStringToLabels("k=y=val;u=e")
expected = map[string]string{"k": "y=val", "u": "e"}
if fmt.Sprintf("%v", result) != fmt.Sprintf("%v", expected) {
t.Errorf("Special characters test failed, expected: %v, got: %v", expected, result)
}
}
func TestParseIntEnvVar(t *testing.T) {
testCases := []struct {
expectedErr string