]> Cypherpunks repositories - gostls13.git/commitdiff
strconv: fix function name in errors for Atoi
authorJoe Tsai <joetsai@digital-static.net>
Sat, 3 Sep 2016 19:01:27 +0000 (12:01 -0700)
committerJoe Tsai <thebrokentoaster@gmail.com>
Sun, 4 Sep 2016 00:17:58 +0000 (00:17 +0000)
Fixes #16980

Change-Id: I902a02b157c2c7d1772f5122b850dc48b1d7a224
Reviewed-on: https://go-review.googlesource.com/28474
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/strconv/atoi.go
src/strconv/strconv_test.go

index a236de421c6023f843d4e02fb5f958afc5142f1d..66df149172d8741ebd30d39f6fd79d9097380391 100644 (file)
@@ -199,6 +199,10 @@ func ParseInt(s string, base int, bitSize int) (i int64, err error) {
 
 // Atoi returns the result of ParseInt(s, 10, 0) converted to type int.
 func Atoi(s string) (int, error) {
+       const fnAtoi = "Atoi"
        i64, err := ParseInt(s, 10, 0)
+       if nerr, ok := err.(*NumError); ok {
+               nerr.Func = fnAtoi
+       }
        return int(i64), err
 }
index 9a007dde4aaae643dea00f1a5157c0d219f032c4..0c14236097d6af7dfbb9a87583c5deaa2a6e053a 100644 (file)
@@ -55,3 +55,34 @@ func TestCountMallocs(t *testing.T) {
                }
        }
 }
+
+func TestErrorPrefixes(t *testing.T) {
+       _, errInt := Atoi("INVALID")
+       _, errBool := ParseBool("INVALID")
+       _, errFloat := ParseFloat("INVALID", 64)
+       _, errInt64 := ParseInt("INVALID", 10, 64)
+       _, errUint64 := ParseUint("INVALID", 10, 64)
+
+       vectors := []struct {
+               err  error  // Input error
+               want string // Function name wanted
+       }{
+               {errInt, "Atoi"},
+               {errBool, "ParseBool"},
+               {errFloat, "ParseFloat"},
+               {errInt64, "ParseInt"},
+               {errUint64, "ParseUint"},
+       }
+
+       for _, v := range vectors {
+               nerr, ok := v.err.(*NumError)
+               if !ok {
+                       t.Errorf("test %s, error was not a *NumError", v.want)
+                       continue
+               }
+               if got := nerr.Func; got != v.want {
+                       t.Errorf("mismatching Func: got %s, want %s", got, v.want)
+               }
+       }
+
+}