add unit test and improve robustness
This commit is contained in:
@@ -11,6 +11,17 @@ var (
|
||||
numberPattern = regexp.MustCompile("(-?\\d+)(\\.\\d+)?")
|
||||
)
|
||||
|
||||
// IsStringOnlyContainsDigits returns whether the specified string only contains digit characters
|
||||
func IsStringOnlyContainsDigits(str string) bool {
|
||||
for i := 0; i < len(str); i++ {
|
||||
if str[i] < '0' || str[i] > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// GetRandomInteger returns a random number, the max parameter represents upper limit
|
||||
func GetRandomInteger(max int) (int, error) {
|
||||
result, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
|
||||
|
||||
@@ -6,6 +6,17 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsStringOnlyContainsDigits(t *testing.T) {
|
||||
actualValue := IsStringOnlyContainsDigits("0123456789")
|
||||
assert.True(t, actualValue)
|
||||
|
||||
actualValue = IsStringOnlyContainsDigits("12345a")
|
||||
assert.False(t, actualValue)
|
||||
|
||||
actualValue = IsStringOnlyContainsDigits("12345 ")
|
||||
assert.False(t, actualValue)
|
||||
}
|
||||
|
||||
func TestParseFirstConsecutiveNumber(t *testing.T) {
|
||||
expectedValue := "¥123.45"
|
||||
actualValue, success := ParseFirstConsecutiveNumber(expectedValue)
|
||||
|
||||
Reference in New Issue
Block a user