I’m running into an issue with Golang’s coverage report where case statements within a switch block are marked as not covered, even though I have written tests that execute these cases. Is this a known issue with Go’s coverage tool, or am I missing something in my tests? How can I ensure that each case statement is accurately tracked in the coverage report?
Code:
func SanitizeMobile(mobile string) (string, serror.SError) {
// Remove all non-digit characters
var cleanedMobile strings.Builder
for _, char := range mobile {
if unicode.IsDigit(char) {
cleanedMobile.WriteRune(char)
}
}
mobile = cleanedMobile.String()
switch {
case len(mobile) < 10 || len(mobile) > 12:
// After cleaning up all non-digits, mobile number cannot be greater than 12 digits in length
return "", serror.New(serror.BadRequestError, InvalidMobile, nil)
case len(mobile) == 10 && mobile[0] == '0':
// In case mobile number is 10 digits long, we don't allow it to start with 0
// As of now, first digit of the mobile number can only be 5,6,7,8,9 but we relax
// this criteria a bit, to allow 1,2,3,4 as well.
return "", serror.New(serror.BadRequestError, InvalidMobile, nil)
case len(mobile) == 11 && mobile[0] != '0':
return "", serror.New(serror.BadRequestError, InvalidMobile, nil)
case len(mobile) == 12 && mobile[0:2] != "91":
return "", serror.New(serror.BadRequestError, InvalidMobile, nil)
default:
break
}
return mobile[len(mobile)-constant.IndianMobileLen:], nil
}
Coverage report: