| 1 |
// Copyright 2020 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package runtime_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
// Test that panics print out the underlying value |
| 13 |
// when the underlying kind is directly printable. |
| 14 |
// Issue: https://golang.org/issues/37531 |
| 15 |
func TestPanicWithDirectlyPrintableCustomTypes(t *testing.T) { |
| 16 |
tests := []struct { |
| 17 |
name string |
| 18 |
wantPanicPrefix string |
| 19 |
}{ |
| 20 |
{"panicCustomBool", `panic: main.MyBool(true)`}, |
| 21 |
{"panicCustomComplex128", `panic: main.MyComplex128(32.1+10i)`}, |
| 22 |
{"panicCustomComplex64", `panic: main.MyComplex64(0.11+3i)`}, |
| 23 |
{"panicCustomFloat32", `panic: main.MyFloat32(-93.7)`}, |
| 24 |
{"panicCustomFloat64", `panic: main.MyFloat64(-93.7)`}, |
| 25 |
{"panicCustomInt", `panic: main.MyInt(93)`}, |
| 26 |
{"panicCustomInt8", `panic: main.MyInt8(93)`}, |
| 27 |
{"panicCustomInt16", `panic: main.MyInt16(93)`}, |
| 28 |
{"panicCustomInt32", `panic: main.MyInt32(93)`}, |
| 29 |
{"panicCustomInt64", `panic: main.MyInt64(93)`}, |
| 30 |
{"panicCustomString", `panic: main.MyString("Panic` + "\n\t" + `line two")`}, |
| 31 |
{"panicCustomUint", `panic: main.MyUint(93)`}, |
| 32 |
{"panicCustomUint8", `panic: main.MyUint8(93)`}, |
| 33 |
{"panicCustomUint16", `panic: main.MyUint16(93)`}, |
| 34 |
{"panicCustomUint32", `panic: main.MyUint32(93)`}, |
| 35 |
{"panicCustomUint64", `panic: main.MyUint64(93)`}, |
| 36 |
{"panicCustomUintptr", `panic: main.MyUintptr(93)`}, |
| 37 |
{"panicDeferFatal", "panic: runtime.errorString(\"invalid memory address or nil pointer dereference\")\n\tfatal error: sync: unlock of unlocked mutex"}, |
| 38 |
{"panicDoublieDeferFatal", "panic: runtime.errorString(\"invalid memory address or nil pointer dereference\") [recovered, repanicked]\n\tfatal error: sync: unlock of unlocked mutex"}, |
| 39 |
} |
| 40 |
|
| 41 |
for _, tt := range tests { |
| 42 |
t := t |
| 43 |
t.Run(tt.name, func(t *testing.T) { |
| 44 |
output := runTestProg(t, "testprog", tt.name) |
| 45 |
if !strings.HasPrefix(output, tt.wantPanicPrefix) { |
| 46 |
t.Fatalf("%q\nis not present in\n%s", tt.wantPanicPrefix, output) |
| 47 |
} |
| 48 |
}) |
| 49 |
} |
| 50 |
} |