diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2016-07-22 18:15:38 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2016-07-22 18:15:38 +0000 |
commit | 22b955cca564a9a3a5b8c9d9dd1e295b7943c128 (patch) | |
tree | abdbd898676e1f853fca2d7e031d105d7ebcf676 /libgo/go/encoding/gob/encoder_test.go | |
parent | 9d04a3af4c6491536badf6bde9707c907e4d196b (diff) | |
download | gcc-22b955cca564a9a3a5b8c9d9dd1e295b7943c128.zip gcc-22b955cca564a9a3a5b8c9d9dd1e295b7943c128.tar.gz gcc-22b955cca564a9a3a5b8c9d9dd1e295b7943c128.tar.bz2 |
libgo: update to go1.7rc3
Reviewed-on: https://go-review.googlesource.com/25150
From-SVN: r238662
Diffstat (limited to 'libgo/go/encoding/gob/encoder_test.go')
-rw-r--r-- | libgo/go/encoding/gob/encoder_test.go | 92 |
1 files changed, 70 insertions, 22 deletions
diff --git a/libgo/go/encoding/gob/encoder_test.go b/libgo/go/encoding/gob/encoder_test.go index 570d796..22090a1 100644 --- a/libgo/go/encoding/gob/encoder_test.go +++ b/libgo/go/encoding/gob/encoder_test.go @@ -8,6 +8,7 @@ import ( "bytes" "encoding/hex" "fmt" + "io/ioutil" "reflect" "strings" "testing" @@ -280,7 +281,7 @@ func TestValueError(t *testing.T) { } t4p := &Type4{3} var t4 Type4 // note: not a pointer. - if err := encAndDec(t4p, t4); err == nil || strings.Index(err.Error(), "pointer") < 0 { + if err := encAndDec(t4p, t4); err == nil || !strings.Contains(err.Error(), "pointer") { t.Error("expected error about pointer; got", err) } } @@ -388,7 +389,7 @@ func TestSingletons(t *testing.T) { t.Errorf("expected error decoding %v: %s", test.in, test.err) continue case err != nil && test.err != "": - if strings.Index(err.Error(), test.err) < 0 { + if !strings.Contains(err.Error(), test.err) { t.Errorf("wrong error decoding %v: wanted %s, got %v", test.in, test.err, err) } continue @@ -414,7 +415,7 @@ func TestStructNonStruct(t *testing.T) { var ns NonStruct if err := encAndDec(s, &ns); err == nil { t.Error("should get error for struct/non-struct") - } else if strings.Index(err.Error(), "type") < 0 { + } else if !strings.Contains(err.Error(), "type") { t.Error("for struct/non-struct expected type error; got", err) } // Now try the other way @@ -424,7 +425,7 @@ func TestStructNonStruct(t *testing.T) { } if err := encAndDec(ns, &s); err == nil { t.Error("should get error for non-struct/struct") - } else if strings.Index(err.Error(), "type") < 0 { + } else if !strings.Contains(err.Error(), "type") { t.Error("for non-struct/struct expected type error; got", err) } } @@ -439,8 +440,8 @@ func (this *interfaceIndirectTestT) F() bool { return true } -// A version of a bug reported on golang-nuts. Also tests top-level -// slice of interfaces. The issue was registering *T caused T to be +// A version of a bug reported on golang-nuts. Also tests top-level +// slice of interfaces. The issue was registering *T caused T to be // stored as the concrete type. func TestInterfaceIndirect(t *testing.T) { Register(&interfaceIndirectTestT{}) @@ -463,7 +464,7 @@ func TestInterfaceIndirect(t *testing.T) { // Also, when the ignored object contains an interface value, it may define // types. Make sure that skipping the value still defines the types by using -// the encoder/decoder pair to send a value afterwards. If an interface +// the encoder/decoder pair to send a value afterwards. If an interface // is sent, its type in the test is always NewType0, so this checks that the // encoder and decoder don't skew with respect to type definitions. @@ -603,10 +604,6 @@ type Bug1Elem struct { type Bug1StructMap map[string]Bug1Elem -func bug1EncDec(in Bug1StructMap, out *Bug1StructMap) error { - return nil -} - func TestMapBug1(t *testing.T) { in := make(Bug1StructMap) in["val1"] = Bug1Elem{"elem1", 1} @@ -835,30 +832,81 @@ func TestPtrToMapOfMap(t *testing.T) { // A top-level nil pointer generates a panic with a helpful string-valued message. func TestTopLevelNilPointer(t *testing.T) { - errMsg := topLevelNilPanic(t) - if errMsg == "" { + var ip *int + encodeErr, panicErr := encodeAndRecover(ip) + if encodeErr != nil { + t.Fatal("error in encode:", encodeErr) + } + if panicErr == nil { t.Fatal("top-level nil pointer did not panic") } + errMsg := panicErr.Error() if !strings.Contains(errMsg, "nil pointer") { t.Fatal("expected nil pointer error, got:", errMsg) } } -func topLevelNilPanic(t *testing.T) (panicErr string) { +func encodeAndRecover(value interface{}) (encodeErr, panicErr error) { defer func() { e := recover() - if err, ok := e.(string); ok { - panicErr = err + if e != nil { + switch err := e.(type) { + case error: + panicErr = err + default: + panicErr = fmt.Errorf("%v", err) + } } }() - var ip *int - buf := new(bytes.Buffer) - if err := NewEncoder(buf).Encode(ip); err != nil { - t.Fatal("error in encode:", err) - } + + encodeErr = NewEncoder(ioutil.Discard).Encode(value) return } +func TestNilPointerPanics(t *testing.T) { + var ( + nilStringPtr *string + intMap = make(map[int]int) + intMapPtr = &intMap + nilIntMapPtr *map[int]int + zero int + nilBoolChannel chan bool + nilBoolChannelPtr *chan bool + nilStringSlice []string + stringSlice = make([]string, 1) + nilStringSlicePtr *[]string + ) + + testCases := []struct { + value interface{} + mustPanic bool + }{ + {nilStringPtr, true}, + {intMap, false}, + {intMapPtr, false}, + {nilIntMapPtr, true}, + {zero, false}, + {nilStringSlice, false}, + {stringSlice, false}, + {nilStringSlicePtr, true}, + {nilBoolChannel, false}, + {nilBoolChannelPtr, true}, + } + + for _, tt := range testCases { + _, panicErr := encodeAndRecover(tt.value) + if tt.mustPanic { + if panicErr == nil { + t.Errorf("expected panic with input %#v, did not panic", tt.value) + } + continue + } + if panicErr != nil { + t.Fatalf("expected no panic with input %#v, got panic=%v", tt.value, panicErr) + } + } +} + func TestNilPointerInsideInterface(t *testing.T) { var ip *int si := struct { @@ -913,7 +961,7 @@ func TestMutipleEncodingsOfBadType(t *testing.T) { // There was an error check comparing the length of the input with the // length of the slice being decoded. It was wrong because the next // thing in the input might be a type definition, which would lead to -// an incorrect length check. This test reproduces the corner case. +// an incorrect length check. This test reproduces the corner case. type Z struct { } |