diff --git a/check/test.go b/check/test.go index 10c629b..e0fcd77 100644 --- a/check/test.go +++ b/check/test.go @@ -162,25 +162,29 @@ func compareOp(tCompareOp string, flagVal string, tCompareValue string) (string, testResult = !(flagVal == tCompareValue) } - case "gt": - expectedResultPattern = "%s is greater than %s" - a, b := toNumeric(flagVal, tCompareValue) - testResult = a > b - - case "gte": - expectedResultPattern = "%s is greater or equal to %s" - a, b := toNumeric(flagVal, tCompareValue) - testResult = a >= b - - case "lt": - expectedResultPattern = "%s is lower than %s" - a, b := toNumeric(flagVal, tCompareValue) - testResult = a < b - - case "lte": - expectedResultPattern = "%s is lower or equal to %s" - a, b := toNumeric(flagVal, tCompareValue) - testResult = a <= b + case "gt", "gte", "lt", "lte": + a, b, err := toNumeric(flagVal, tCompareValue) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + } + switch tCompareOp { + case "gt": + expectedResultPattern = "%s is greater than %s" + testResult = a > b + + case "gte": + expectedResultPattern = "%s is greater or equal to %s" + testResult = a >= b + + case "lt": + expectedResultPattern = "%s is lower than %s" + testResult = a < b + + case "lte": + expectedResultPattern = "%s is lower or equal to %s" + testResult = a <= b + } case "has": expectedResultPattern = "'%s' has '%s'" @@ -342,18 +346,15 @@ func (ts *tests) execute(s string) *testOutput { return finalOutput } -func toNumeric(a, b string) (c, d int) { - var err error - c, err = strconv.Atoi(a) +func toNumeric(a, b string) (c, d int, err error) { + c, err = strconv.Atoi(strings.TrimSpace(a)) if err != nil { - fmt.Fprintf(os.Stderr, "error converting %s: %s\n", a, err) - os.Exit(1) + return -1, -1, fmt.Errorf("toNumeric - error converting %s: %s", a, err) } - d, err = strconv.Atoi(b) + d, err = strconv.Atoi(strings.TrimSpace(b)) if err != nil { - fmt.Fprintf(os.Stderr, "error converting %s: %s\n", b, err) - os.Exit(1) + return -1, -1, fmt.Errorf("toNumeric - error converting %s: %s", b, err) } - return c, d + return c, d, nil } diff --git a/check/test_test.go b/check/test_test.go index bd168c2..ff826e9 100644 --- a/check/test_test.go +++ b/check/test_test.go @@ -656,3 +656,38 @@ func TestCompareOp(t *testing.T) { } } } + +func TestToNumeric(t *testing.T) { + cases := []struct { + firstValue string + secondValue string + expectedToFail bool + }{ + { + firstValue: "a", + secondValue: "b", + expectedToFail: true, + }, + { + firstValue: "5", + secondValue: "b", + expectedToFail: true, + }, + { + firstValue: "5", + secondValue: "6", + expectedToFail: false, + }, + } + + for _, c := range cases { + f, s, err := toNumeric(c.firstValue, c.secondValue) + if c.expectedToFail && err == nil { + t.Errorf("TestToNumeric - Expected error while converting %s and %s", c.firstValue, c.secondValue) + } + + if !c.expectedToFail && (f != 5 || s != 6) { + t.Errorf("TestToNumeric - Expected to return %d,%d , but instead got %d,%d", 5, 6, f, s) + } + } +}