diff --git a/utils/dateseries.go b/utils/dateseries.go index cb3bf2696..ad1aa1657 100644 --- a/utils/dateseries.go +++ b/utils/dateseries.go @@ -231,7 +231,7 @@ func (md *MonthDays) Parse(input, sep string) { // Dumps the month days in a serialized string, similar to the one parsed func (md MonthDays) Serialize(sep string) string { if len(md) == 0 { - return "*any" + return META_ANY } var mdsStr string for idx, mDay := range md { @@ -290,7 +290,7 @@ func (wd WeekDays) Contains(weekDay time.Weekday) (result bool) { func (wd *WeekDays) Parse(input, sep string) { switch input { - case "*any", "": + case META_ANY, EmptyString: *wd = []time.Weekday{} default: elements := strings.Split(input, sep) @@ -305,7 +305,7 @@ func (wd *WeekDays) Parse(input, sep string) { // Dumps the week days in a serialized string, similar to the one parsed func (wd WeekDays) Serialize(sep string) string { if len(wd) == 0 { - return "*any" + return META_ANY } var wdStr string for idx, d := range wd { diff --git a/utils/dateseries_test.go b/utils/dateseries_test.go index 48e1e24d4..63eb49462 100644 --- a/utils/dateseries_test.go +++ b/utils/dateseries_test.go @@ -207,11 +207,15 @@ func TestMonthsSerialize(t *testing.T) { } } -func TestMonthsIsCompleteYes(t *testing.T) { +func TestMonthsIsComplete(t *testing.T) { months := Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December} if !months.IsComplete() { t.Error("Error months IsComplete: ", months) } + months = Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November} + if months.IsComplete() { + t.Error("Error months IsComplete: ", months) + } } func TestMonthsEquals(t *testing.T) { @@ -310,6 +314,199 @@ func TestMonthDaysSerialize(t *testing.T) { } } +func TestMonthDaysEquals(t *testing.T) { + md1 := MonthDays{24, 25, 26} + md2 := MonthDays{24, 25, 26} + md3 := MonthDays{24, 25, 27} + md4 := MonthDays{} + if !md1.Equals(md2) { + t.Errorf("Expected: true, received: false") + } else if md1.Equals(md4) { + t.Errorf("Expected: false, received: true") + } else if md1.Equals(md3) { + t.Errorf("Expected: false, received: true") + } +} + +func TestWeekdaysSort(t *testing.T) { + wd := &WeekDays{} + wd.Sort() + if !reflect.DeepEqual(wd, &WeekDays{}) { + t.Errorf("Expecting %+v received: %+v", &WeekDays{}, wd) + } + wd = &WeekDays{time.Thursday, time.Sunday, time.Monday, time.Friday} + wdSorted := &WeekDays{time.Sunday, time.Monday, time.Thursday, time.Friday} + wd.Sort() + if !reflect.DeepEqual(wd, wdSorted) { + t.Errorf("Expecting %+v received: %+v", wdSorted, wd) + } +} + +func TestWeekDaysLen(t *testing.T) { + wd := &WeekDays{} + if rcv := wd.Len(); rcv != 0 { + t.Errorf("Expecting %+v received: %+v", 0, rcv) + } + wd = &WeekDays{time.Thursday, time.Sunday, time.Monday, time.Friday} + if rcv := wd.Len(); rcv != 4 { + t.Errorf("Expecting %+v received: %+v", 4, rcv) + } +} + +func TestWeekDaysSwap(t *testing.T) { + wd := &WeekDays{time.Thursday, time.Sunday, time.Monday, time.Friday} + wd.Swap(0, 1) + ysSwapped := &WeekDays{time.Sunday, time.Thursday, time.Monday, time.Friday} + if !reflect.DeepEqual(wd, ysSwapped) { + t.Errorf("Expecting %+v received: %+v", ysSwapped, wd) + } +} + +func TestWeekDaysLess(t *testing.T) { + ys := &WeekDays{time.Thursday, time.Sunday, time.Monday, time.Friday} + if ys.Less(0, 1) { + t.Errorf("Expecting false received: true") + } + if !ys.Less(1, 2) { + t.Errorf("Expecting true received: false") + } +} + +func TestWeekDaysContains(t *testing.T) { + wds := WeekDays{time.Monday, time.Tuesday} + if !wds.Contains(time.Monday) { + t.Errorf("Expected: true, received: false") + } else if wds.Contains(time.Wednesday) { + t.Errorf("Expected: false, received: true") + } +} + +func TestWeekDaysParse(t *testing.T) { + wd := WeekDays{} + wd.Parse(META_ANY, EmptyString) + eOut := WeekDays{time.Monday, time.Tuesday, time.Wednesday} + wd.Parse("1,2,3", FIELDS_SEP) + if !reflect.DeepEqual(eOut, wd) { + t.Errorf("Expected: %+v, received: %+v", eOut, wd) + } +} + +func TestWeekDaysSerialize(t *testing.T) { + wd := &WeekDays{} + if rcv := wd.Serialize(INFIELD_SEP); !reflect.DeepEqual(META_ANY, rcv) { + t.Errorf("Expected: %s, received: %s", META_ANY, rcv) + } + + wd = &WeekDays{time.Monday} + if rcv := wd.Serialize(INFIELD_SEP); !reflect.DeepEqual("1", rcv) { + t.Errorf("Expected: '1', received: %s", rcv) + } + + wd = &WeekDays{time.Monday, time.Saturday, time.Sunday} + if rcv := wd.Serialize(INFIELD_SEP); !reflect.DeepEqual("1;6;0", rcv) { + t.Errorf("Expected: '1;6;0', received: %s", rcv) + } +} + +func TestWeekDaysEquals(t *testing.T) { + wd1 := WeekDays{time.Monday, time.Saturday, time.Sunday} + wd2 := WeekDays{time.Monday, time.Saturday, time.Sunday} + wd3 := WeekDays{time.Monday, time.Saturday, time.Tuesday} + wd4 := WeekDays{time.Monday} + if !wd1.Equals(wd2) { + t.Errorf("Expected: true, received: false") + } else if wd1.Equals(wd3) { + t.Errorf("Expected: false, received: true") + } else if wd1.Equals(wd4) { + t.Errorf("Expected: false, received: true") + } +} + +func TestDaysInMonth(t *testing.T) { + if rcv := DaysInMonth(2016, 4); rcv != 30 { + t.Errorf("Expected: %v, received: %v ", 30, rcv) + } + if rcv := DaysInMonth(2016, 2); rcv != 29 { + t.Errorf("Expected: %v, received: %v ", 29, rcv) + } + if rcv := DaysInMonth(2016, 1); rcv != 31 { + t.Errorf("Expected: %v, received: %v ", 31, rcv) + } + if rcv := DaysInMonth(2016, 12); rcv != 31 { + t.Errorf("Expected: %v, received: %v ", 31, rcv) + } + if rcv := DaysInMonth(2015, 2); rcv != 28 { + t.Errorf("Expected: %v, received: %v ", 28, rcv) + } +} + +func TestDaysInYear(t *testing.T) { + if rcv := DaysInYear(2016); rcv != 366 { + t.Errorf("Expected: %v, received: %v ", 366, rcv) + } + if rcv := DaysInYear(2015); rcv != 365 { + t.Errorf("Expected: %v, received: %v ", 265, rcv) + } +} + +func TestLocalAddr(t *testing.T) { + eOut := &NetAddr{network: Local, ip: Local} + if rcv := LocalAddr(); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected: %+v, received: %+v ", ToJSON(eOut), ToJSON(rcv)) + } +} + +func TestNewNetAddr(t *testing.T) { + eOut := &NetAddr{} + if rcv := NewNetAddr(EmptyString, EmptyString); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected: %+v, received: %+v ", ToJSON(eOut), ToJSON(rcv)) + } + eOut = &NetAddr{network: "network"} + if rcv := NewNetAddr("network", EmptyString); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected: %+v, received: %+v ", ToJSON(eOut), ToJSON(rcv)) + } + eOut = &NetAddr{ip: "127.0.0.1", port: 2012} + if rcv := NewNetAddr(EmptyString, "127.0.0.1:2012"); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected: %+v, received: %+v ", ToJSON(eOut), ToJSON(rcv)) + } + eOut = &NetAddr{network: "network", ip: "127.0.0.1", port: 2012} + if rcv := NewNetAddr("network", "127.0.0.1:2012"); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected: %+v, received: %+v ", ToJSON(eOut), ToJSON(rcv)) + } +} + +func TestNetAddrNetwork(t *testing.T) { + lc := NetAddr{network: "network", ip: "127.0.0.1", port: 2012} + if rcv := lc.Network(); !reflect.DeepEqual(rcv, lc.network) { + t.Errorf("Expected: %+v, received: %+v ", lc.network, rcv) + } +} + +func TestNetAddrString(t *testing.T) { + lc := NetAddr{network: "network", ip: "127.0.0.1", port: 2012} + if rcv := lc.String(); !reflect.DeepEqual(rcv, lc.ip) { + t.Errorf("Expected: %+v, received: %+v ", lc.ip, rcv) + } +} + +func TestNetAddrPort(t *testing.T) { + lc := NetAddr{network: "network", ip: "127.0.0.1", port: 2012} + if rcv := lc.Port(); !reflect.DeepEqual(rcv, lc.port) { + t.Errorf("Expected: %+v, received: %+v ", lc.port, rcv) + } +} + +func TestNetAddrHost(t *testing.T) { + lc := NetAddr{network: "network", ip: "127.0.0.1", port: 2012} + if rcv := lc.Host(); !reflect.DeepEqual(rcv, "127.0.0.1:2012") { + t.Errorf("Expected: '127.0.0.1:2012', received: %+v ", rcv) + } + lc = NetAddr{network: "network", ip: Local, port: 2012} + if rcv := lc.Host(); !reflect.DeepEqual(rcv, Local) { + t.Errorf("Expected: %+v, received: %+v ", Local, rcv) + } +} + func TestMonthStoreRestoreJson(t *testing.T) { m := Months{time.May, time.June, time.July, time.August} r, _ := json.Marshal(m) @@ -348,102 +545,3 @@ func TestWeekDayStoreRestoreJson(t *testing.T) { t.Errorf("Expected %v was %v", wd, o) } } - -func TestWeekDaysSerialize(t *testing.T) { - wds := &WeekDays{} - wdsString := wds.Serialize(";") - expectString := "*any" - if expectString != wdsString { - t.Errorf("Expected: %s, got: %s", expectString, wdsString) - } - wds2 := &WeekDays{time.Monday} - wdsString2 := wds2.Serialize(";") - expectString2 := "1" - if expectString2 != wdsString2 { - t.Errorf("Expected: %s, got: %s", expectString2, wdsString2) - } - wds3 := &WeekDays{time.Monday, time.Saturday, time.Sunday} - wdsString3 := wds3.Serialize(";") - expectString3 := "1;6;0" - if expectString3 != wdsString3 { - t.Errorf("Expected: %s, got: %s", expectString3, wdsString3) - } -} - -func TestMonthsIsCompleteNot(t *testing.T) { - months := Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November} - if months.IsComplete() { - t.Error("Error months IsComplete: ", months) - } -} - -func TestDaysInMonth(t *testing.T) { - if n := DaysInMonth(2016, 4); n != 30 { - t.Error("error calculating days: ", n) - } - if n := DaysInMonth(2016, 2); n != 29 { - t.Error("error calculating days: ", n) - } - if n := DaysInMonth(2016, 1); n != 31 { - t.Error("error calculating days: ", n) - } - if n := DaysInMonth(2016, 12); n != 31 { - t.Error("error calculating days: ", n) - } - if n := DaysInMonth(2015, 2); n != 28 { - t.Error("error calculating days: ", n) - } -} - -func TestDaysInYear(t *testing.T) { - if n := DaysInYear(2016); n != 366 { - t.Error("error calculating days: ", n) - } - if n := DaysInYear(2015); n != 365 { - t.Error("error calculating days: ", n) - } -} - -func TestWeekDaysEquals(t *testing.T) { - wds1 := WeekDays{time.Monday, time.Saturday, time.Sunday} - wds2 := WeekDays{time.Monday, time.Saturday, time.Sunday} - wds3 := WeekDays{time.Monday} - if wds1.Equals(wds2) != true { - t.Errorf("Expected: true, got: %v", !true) - } else if wds1.Equals(wds3) != false { - t.Errorf("Expected: false, got: %v", !false) - } -} - -func TestMonthDaysEquals(t *testing.T) { - md1 := MonthDays{24, 25, 26} - md2 := MonthDays{24, 25, 26} - md3 := MonthDays{} - if md1.Equals(md2) != true { - t.Errorf("Expected: true, got: %v", !true) - } else if md1.Equals(md3) != false { - t.Errorf("Expected: false, got: %v", !false) - } -} - -func TestWeekDaysParse(t *testing.T) { - wds1 := WeekDays{} - in := "1,2,3" - wds2 := WeekDays{time.Monday, time.Tuesday, time.Wednesday} - if reflect.DeepEqual(wds2, wds1) != false { - t.Errorf("Expected: %+v, received: %+v", WeekDays{}, wds1) - } - wds1.Parse(in, ",") - if !reflect.DeepEqual(wds2, wds1) { - t.Errorf("Expected: %+v, received: %+v", wds2, wds1) - } -} - -func TestWeekDaysContains(t *testing.T) { - wds := WeekDays{time.Monday, time.Tuesday} - if wds.Contains(time.Monday) != true { - t.Errorf("Expected: true, got: %v", !true) - } else if wds.Contains(time.Wednesday) != false { - t.Errorf("Expected: false, got: %v", !false) - } -} diff --git a/utils/errors_test.go b/utils/errors_test.go index 7de628e6c..282aa43ff 100644 --- a/utils/errors_test.go +++ b/utils/errors_test.go @@ -18,28 +18,116 @@ along with this program. If not, see package utils import ( + "reflect" "testing" ) +func TestNewCGRError(t *testing.T) { + eOut := &CGRError{} + if rcv := NewCGRError(EmptyString, EmptyString, EmptyString, EmptyString); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected: %+v, received: %+v ", ToJSON(eOut), ToJSON(rcv)) + } + eOut = &CGRError{ + context: "context", + apiError: "apiError", + shortError: "shortError", + longError: "longError", + errorMessage: "shortError", + } + if rcv := NewCGRError("context", "apiError", "shortError", "longError"); !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected: %+v, received: %+v ", ToJSON(eOut), ToJSON(rcv)) + } +} + func TestCGRErrorActivate(t *testing.T) { ctx := "TEST_CONTEXT" apiErr := "TEST_API_ERR" shortErr := "short error" longErr := "long error which is good for debug" - err := NewCGRError(ctx, apiErr, shortErr, longErr) - if ctxRcv := err.Context(); ctxRcv != ctx { - t.Errorf("Context: <%s>", ctxRcv) + cgrError := NewCGRError(ctx, apiErr, shortErr, longErr) + + if rcv := cgrError.Context(); !reflect.DeepEqual(rcv, ctx) { + t.Errorf("Expected: %+q, received: %+q ", ctx, rcv) } - if err.Error() != shortErr { - t.Error(err) + if rcv := cgrError.Error(); !reflect.DeepEqual(rcv, shortErr) { + t.Errorf("Expected: %+q, received: %+q ", shortErr, rcv) } - err.ActivateAPIError() - if err.Error() != apiErr { - t.Error(err) + cgrError.ActivateAPIError() + if !reflect.DeepEqual(apiErr, cgrError.errorMessage) { + t.Errorf("Expected: %+q, received: %+q ", apiErr, cgrError.errorMessage) } - err.ActivateLongError() - if err.Error() != longErr { - t.Error(err) + cgrError.ActivateShortError() + if !reflect.DeepEqual(shortErr, cgrError.errorMessage) { + t.Errorf("Expected: %+q, received: %+q ", shortErr, cgrError.errorMessage) + } + cgrError.ActivateLongError() + if !reflect.DeepEqual(longErr, cgrError.errorMessage) { + t.Errorf("Expected: %+q, received: %+q ", longErr, cgrError.errorMessage) + } +} + +func TestNewErrMandatoryIeMissing(t *testing.T) { + if rcv := NewErrMandatoryIeMissing(EmptyString); rcv.Error() != "MANDATORY_IE_MISSING: []" { + t.Errorf("Expecting: MANDATORY_IE_MISSING: [], received: %+v", rcv) + } + if rcv := NewErrMandatoryIeMissing("string1", "string2"); rcv.Error() != "MANDATORY_IE_MISSING: [string1 string2]" { + t.Errorf("Expecting: MANDATORY_IE_MISSING: [string1 string2], received: %+b", rcv) + } + if rcv := NewErrMandatoryIeMissing("test"); rcv.Error() != "MANDATORY_IE_MISSING: [test]" { + t.Errorf("Expecting: MANDATORY_IE_MISSING: [test], received: %+v", rcv) + } +} + +func TestNewErrServerError(t *testing.T) { + cgrError := NewCGRError("context", "apiError", "shortError", "longError") + if rcv := NewErrServerError(cgrError); rcv.Error() != "SERVER_ERROR: shortError" { + t.Errorf("Expecting: SERVER_ERROR: shortError, received: %+v", rcv) + } +} + +func TestNewErrServiceNotOperational(t *testing.T) { + if rcv := NewErrServiceNotOperational("Error"); rcv.Error() != "SERVICE_NOT_OPERATIONAL: Error" { + t.Errorf("Expecting: SERVICE_NOT_OPERATIONAL: Error, received: %+v", rcv) + } +} + +func TestNewErrNotConnected(t *testing.T) { + if rcv := NewErrNotConnected("Error"); rcv.Error() != "NOT_CONNECTED: Error" { + t.Errorf("Expecting: NOT_CONNECTED: Error, received: %+v", rcv) + } +} + +func TestNewErrRALs(t *testing.T) { + cgrError := NewCGRError("context", "apiError", "shortError", "longError") + if rcv := NewErrRALs(cgrError); rcv.Error() != "RALS_ERROR:shortError" { + t.Errorf("Expecting: RALS_ERROR:shortError, received: %+v", rcv) + } +} + +func TestNewErrResourceS(t *testing.T) { + cgrError := NewCGRError("context", "apiError", "shortError", "longError") + if rcv := NewErrResourceS(cgrError); rcv.Error() != "RESOURCES_ERROR:shortError" { + t.Errorf("Expecting: RESOURCES_ERROR:shortError, received: %+v", rcv) + } +} + +func TestNewErrSupplierS(t *testing.T) { + cgrError := NewCGRError("context", "apiError", "shortError", "longError") + if rcv := NewErrSupplierS(cgrError); rcv.Error() != "SUPPLIERS_ERROR:shortError" { + t.Errorf("Expecting: SUPPLIERS_ERROR:shortError, received: %+v", rcv) + } +} +func TestNewErrAttributeS(t *testing.T) { + cgrError := NewCGRError("context", "apiError", "shortError", "longError") + if rcv := NewErrAttributeS(cgrError); rcv.Error() != "ATTRIBUTES_ERROR:shortError" { + t.Errorf("Expecting: ATTRIBUTES_ERROR:shortError, received: %+v", rcv) + } +} + +func TestNewErrDispatcherS(t *testing.T) { + cgrError := NewCGRError("context", "apiError", "shortError", "longError") + if rcv := NewErrDispatcherS(cgrError); rcv.Error() != "DISPATCHER_ERROR:shortError" { + t.Errorf("Expecting: DISPATCHER_ERROR:shortError, received: %+v", rcv) } } @@ -55,3 +143,49 @@ func TestAPIErrorHandler(t *testing.T) { t.Error(err) } } + +func TestNewErrStringCast(t *testing.T) { + if rcv := NewErrStringCast("test"); rcv.Error() != "cannot cast value: test to string" { + t.Errorf("Expecting: cannot cast value: test to string, received: %+v", rcv) + } +} + +func TestNewErrFldStringCast(t *testing.T) { + if rcv := NewErrFldStringCast("test1", "test2"); rcv.Error() != "cannot cast field: test1 with value: test2 to string" { + t.Errorf("Expecting: cannot cast field: test1 with value: test2 to string, received: %+v", rcv) + } +} + +func TestErrHasPrefix(t *testing.T) { + if ErrHasPrefix(nil, EmptyString) { + t.Error("Expecting false, received: true") + } + if !ErrHasPrefix(&CGRError{errorMessage: "test_errorMessage"}, "test") { + t.Error("Expecting true, received: false") + } +} + +func TestErrPrefix(t *testing.T) { + cgrError := NewCGRError("context", "apiError", "shortError", "longError") + if rcv := ErrPrefix(cgrError, "notaprefix"); rcv.Error() != "shortError:notaprefix" { + t.Errorf("Expecting: shortError:notaprefix, received: %+v", rcv) + } +} + +func TestErrPrefixNotFound(t *testing.T) { + if rcv := ErrPrefixNotFound("test_string"); rcv.Error() != "NOT_FOUND:test_string" { + t.Errorf("Expecting: NOT_FOUND:test_string, received: %+v", rcv) + } +} + +func TestErrPrefixNotErrNotImplemented(t *testing.T) { + if rcv := ErrPrefixNotErrNotImplemented("test_string"); rcv.Error() != "NOT_IMPLEMENTED:test_string" { + t.Errorf("Expecting: NOT_IMPLEMENTED:test_string, received: %+v", rcv) + } +} + +func TestErrEnvNotFound(t *testing.T) { + if rcv := ErrEnvNotFound("test_string"); rcv.Error() != "NOT_FOUND:ENV_VAR:test_string" { + t.Errorf("Expecting: NOT_FOUND:ENV_VAR:test_string, received: %+v", rcv) + } +} diff --git a/utils/map.go b/utils/map.go index 826e0d697..9103cfc19 100644 --- a/utils/map.go +++ b/utils/map.go @@ -46,7 +46,7 @@ func MirrorMap(mapIn map[string]string) map[string]string { func MissingMapKeys(inMap map[string]string, requiredKeys []string) []string { missingKeys := []string{} for _, reqKey := range requiredKeys { - if val, hasKey := inMap[reqKey]; !hasKey || val == "" { + if val, hasKey := inMap[reqKey]; !hasKey || val == EmptyString { missingKeys = append(missingKeys, reqKey) } } @@ -70,8 +70,8 @@ func NewStringMap(s ...string) StringMap { result := make(StringMap) for _, v := range s { v = strings.TrimSpace(v) - if v != "" { - if strings.HasPrefix(v, "!") { + if v != EmptyString { + if strings.HasPrefix(v, NegativePrefix) { result[v[1:]] = false } else { result[v] = true @@ -135,8 +135,8 @@ func StringMapFromSlice(s []string) StringMap { result := make(StringMap, len(s)) for _, v := range s { v = strings.TrimSpace(v) - if v != "" { - if strings.HasPrefix(v, "!") { + if v != EmptyString { + if strings.HasPrefix(v, NegativePrefix) { result[v[1:]] = false } else { result[v] = true @@ -166,7 +166,7 @@ func (sm StringMap) GetOne() string { for key := range sm { return key } - return "" + return EmptyString } func (sm StringMap) Join(mps ...StringMap) { diff --git a/utils/map_test.go b/utils/map_test.go index b23f8c4ff..dcdf57fd1 100644 --- a/utils/map_test.go +++ b/utils/map_test.go @@ -23,32 +23,108 @@ import ( "testing" ) -func TestStringMapParse(t *testing.T) { - sm := ParseStringMap("1;2;3;4") - if len(sm) != 4 { - t.Error("Error pasring map: ", sm) +func TestConvertMapValStrIf(t *testing.T) { + var mapIn map[string]string + var mapOut map[string]interface{} + if rcv := ConvertMapValStrIf(mapIn); reflect.TypeOf(rcv) != reflect.TypeOf(mapOut) { + t.Errorf("Expecting: %+v, received: %+v", reflect.TypeOf(mapOut), reflect.TypeOf(rcv)) + } + mapIn = map[string]string{"test1": "_test1_", "test2": "_test2_"} + if rcv := ConvertMapValStrIf(mapIn); reflect.TypeOf(rcv) != reflect.TypeOf(mapOut) { + t.Errorf("Expecting: %+v, received: %+v", reflect.TypeOf(mapOut), reflect.TypeOf(rcv)) + } else if !reflect.DeepEqual(mapIn["test1"], rcv["test1"]) { + t.Errorf("Expecting: %+v, received: %+v", mapIn["test1"], rcv["test1"]) + } else if len(rcv) != len(mapIn) { + t.Errorf("Expecting: %+v, received: %+v", len(mapIn), len(rcv)) } } -func TestStringMapParseNegative(t *testing.T) { - sm := ParseStringMap("1;2;!3;4") - if len(sm) != 4 { +func TestMirrorMap(t *testing.T) { + var mapIn map[string]string + if rcv := MirrorMap(mapIn); reflect.DeepEqual(rcv, mapIn) { + t.Errorf("Expecting: %+v, received: %+v", reflect.TypeOf(mapIn), reflect.TypeOf(rcv)) + } else if len(rcv) != 0 { + t.Errorf("Expecting: %+v, received: %+v", 0, len(rcv)) + } + mapIn = map[string]string{"test1": "_test1_", "test2": "_test2_"} + eOut := map[string]string{"_test1_": "test1", "_test2_": "test2"} + if rcv := MirrorMap(mapIn); reflect.DeepEqual(rcv, mapIn) { + t.Errorf("Expecting: %+v, received: %+v", reflect.TypeOf(mapIn), reflect.TypeOf(rcv)) + } else if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %+v, received: %+v", eOut, rcv) + } +} + +func TestMissingMapKeys(t *testing.T) { + mapIn := map[string]string{} + requiredKeys := []string{} + if rcv := MissingMapKeys(mapIn, requiredKeys); len(rcv) != 0 { + t.Errorf("Expecting: %+v, received: %+v", 0, len(rcv)) + } + + mapIn = map[string]string{"test1": "_test1_", "test2": "_test2_"} + requiredKeys = []string{"test1", "test2"} + if rcv := MissingMapKeys(mapIn, requiredKeys); len(rcv) != 0 { + t.Errorf("Expecting: %+v, received: %+v", 0, len(rcv)) + } + + mapIn = map[string]string{"test1": "_test1_", "test2": "_test2_"} + requiredKeys = []string{"test2", "test3"} + if rcv := MissingMapKeys(mapIn, requiredKeys); len(rcv) != 1 { + t.Errorf("Expecting: %+v, received: %+v", 1, len(rcv)) + } else if !reflect.DeepEqual([]string{"test3"}, rcv) { + t.Errorf("Expecting: %+v, received: %+v", []string{"test3"}, rcv) + } + + requiredKeys = []string{"test3", "test4"} + eOut := []string{"test3", "test4"} + if rcv := MissingMapKeys(mapIn, requiredKeys); len(rcv) != 2 { + t.Errorf("Expecting: %+v, received: %+v", 2, len(rcv)) + } else if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %+v, received: %+v", eOut, rcv) + } +} + +func TestMapKeys(t *testing.T) { + mapIn := map[string]string{"test1": "_test1_", "test2": "_test2_"} + eOut := []string{"test1", "test2"} + rcv := MapKeys(mapIn) + sort.Slice(rcv, func(i, j int) bool { return rcv[i] < rcv[j] }) + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %+v, received: %+v", eOut, rcv) + } + mapIn = map[string]string{"test1": "_test1_", "test2": "_test2_", "test3": "_test3_", "test4": "_test4_"} + eOut = []string{"test1", "test2", "test3", "test4"} + rcv = MapKeys(mapIn) + sort.Slice(rcv, func(i, j int) bool { return rcv[i] < rcv[j] }) + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %+v, received: %+v", eOut, rcv) + } +} + +func MapKeysStringMapParse(t *testing.T) { + if sm := ParseStringMap(EmptyString); len(sm) != 0 { + t.Errorf("Expecting %+v, received %+v", 0, len(sm)) + } + if sm := ParseStringMap(ZERO); len(sm) != 0 { + t.Errorf("Expecting %+v, received %+v", 0, len(sm)) + } + if sm := ParseStringMap("1;2;3;4"); len(sm) != 4 { t.Error("Error pasring map: ", sm) } - if sm["3"] != false { + if sm := ParseStringMap("1;2;!3;4"); len(sm) != 4 { + t.Error("Error pasring map: ", sm) + } else if sm["3"] != false { t.Error("Error parsing negative: ", sm) } -} - -func TestStringMapCompare(t *testing.T) { sm := ParseStringMap("1;2;!3;4") - if include, found := sm["2"]; include != true && found != true { + if include, has := sm["2"]; include != true && has != true { t.Error("Error detecting positive: ", sm) } - if include, found := sm["3"]; include != false && found != true { + if include, has := sm["3"]; include != false && has != true { t.Error("Error detecting negative: ", sm) } - if include, found := sm["5"]; include != false && found != false { + if include, has := sm["5"]; include != false && has != false { t.Error("Error detecting missing: ", sm) } }