Source file src/runtime/panic_test.go

     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(+3.210000e+001+1.000000e+001i)`},
    22  		{"panicCustomComplex64", `panic: main.MyComplex64(+1.100000e-001+3.000000e+000i)`},
    23  		{"panicCustomFloat32", `panic: main.MyFloat32(-9.370000e+001)`},
    24  		{"panicCustomFloat64", `panic: main.MyFloat64(-9.370000e+001)`},
    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  	}
    38  
    39  	for _, tt := range tests {
    40  		t := t
    41  		t.Run(tt.name, func(t *testing.T) {
    42  			output := runTestProg(t, "testprog", tt.name)
    43  			if !strings.HasPrefix(output, tt.wantPanicPrefix) {
    44  				t.Fatalf("%q\nis not present in\n%s", tt.wantPanicPrefix, output)
    45  			}
    46  		})
    47  	}
    48  }
    49  

View as plain text