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

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