Adds Unit Tests for check/toNumeric (#401)

* fixes issue #364

* fixed unit test error text
pull/428/head^2
Roberto Rojas 5 years ago committed by GitHub
parent 050145f6b3
commit 4416e46967
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -162,25 +162,29 @@ func compareOp(tCompareOp string, flagVal string, tCompareValue string) (string,
testResult = !(flagVal == tCompareValue) testResult = !(flagVal == tCompareValue)
} }
case "gt": case "gt", "gte", "lt", "lte":
expectedResultPattern = "%s is greater than %s" a, b, err := toNumeric(flagVal, tCompareValue)
a, b := toNumeric(flagVal, tCompareValue) if err != nil {
testResult = a > b fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
case "gte": }
expectedResultPattern = "%s is greater or equal to %s" switch tCompareOp {
a, b := toNumeric(flagVal, tCompareValue) case "gt":
testResult = a >= b expectedResultPattern = "%s is greater than %s"
testResult = a > b
case "lt":
expectedResultPattern = "%s is lower than %s" case "gte":
a, b := toNumeric(flagVal, tCompareValue) expectedResultPattern = "%s is greater or equal to %s"
testResult = a < b testResult = a >= b
case "lte": case "lt":
expectedResultPattern = "%s is lower or equal to %s" expectedResultPattern = "%s is lower than %s"
a, b := toNumeric(flagVal, tCompareValue) testResult = a < b
testResult = a <= b
case "lte":
expectedResultPattern = "%s is lower or equal to %s"
testResult = a <= b
}
case "has": case "has":
expectedResultPattern = "'%s' has '%s'" expectedResultPattern = "'%s' has '%s'"
@ -342,18 +346,15 @@ func (ts *tests) execute(s string) *testOutput {
return finalOutput return finalOutput
} }
func toNumeric(a, b string) (c, d int) { func toNumeric(a, b string) (c, d int, err error) {
var err error c, err = strconv.Atoi(strings.TrimSpace(a))
c, err = strconv.Atoi(a)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error converting %s: %s\n", a, err) return -1, -1, fmt.Errorf("toNumeric - error converting %s: %s", a, err)
os.Exit(1)
} }
d, err = strconv.Atoi(b) d, err = strconv.Atoi(strings.TrimSpace(b))
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error converting %s: %s\n", b, err) return -1, -1, fmt.Errorf("toNumeric - error converting %s: %s", b, err)
os.Exit(1)
} }
return c, d return c, d, nil
} }

@ -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)
}
}
}

Loading…
Cancel
Save