github.com/timandy/routine@v1.1.4-0.20240507073150-e4a3e1fe2ba5/api_error_test.go (about)

     1  package routine
     2  
     3  import (
     4  	"errors"
     5  	"runtime"
     6  	"runtime/debug"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestNewRuntimeError_Nil(t *testing.T) {
    14  	err := NewRuntimeError(nil)
    15  	assertGoidGopc(t, err)
    16  	assert.Equal(t, "", err.Message())
    17  	assert.Greater(t, len(err.StackTrace()), 1)
    18  	assert.Nil(t, err.Cause())
    19  	lines := strings.Split(err.Error(), newLine)
    20  	assert.Equal(t, "RuntimeError", lines[0])
    21  	assert.Equal(t, "   at ", lines[1][:6])
    22  }
    23  
    24  func TestNewRuntimeError_EmptyString(t *testing.T) {
    25  	err := NewRuntimeError("")
    26  	assertGoidGopc(t, err)
    27  	assert.Equal(t, "", err.Message())
    28  	assert.Greater(t, len(err.StackTrace()), 1)
    29  	assert.Nil(t, err.Cause())
    30  	lines := strings.Split(err.Error(), newLine)
    31  	assert.Equal(t, "RuntimeError", lines[0])
    32  	assert.Equal(t, "   at ", lines[1][:6])
    33  }
    34  
    35  func TestNewRuntimeError_WhiteSpaceString(t *testing.T) {
    36  	err := NewRuntimeError("\t")
    37  	assertGoidGopc(t, err)
    38  	assert.Equal(t, "\t", err.Message())
    39  	assert.Greater(t, len(err.StackTrace()), 1)
    40  	assert.Nil(t, err.Cause())
    41  	lines := strings.Split(err.Error(), newLine)
    42  	assert.Equal(t, "RuntimeError: \t", lines[0])
    43  	assert.Equal(t, "   at ", lines[1][:6])
    44  }
    45  
    46  func TestNewRuntimeError_NormalString(t *testing.T) {
    47  	err := NewRuntimeError("this is error message")
    48  	assertGoidGopc(t, err)
    49  	assert.Equal(t, "this is error message", err.Message())
    50  	assert.Greater(t, len(err.StackTrace()), 1)
    51  	assert.Nil(t, err.Cause())
    52  	lines := strings.Split(err.Error(), newLine)
    53  	assert.Equal(t, "RuntimeError: this is error message", lines[0])
    54  	assert.Equal(t, "   at ", lines[1][:6])
    55  }
    56  
    57  func TestNewRuntimeError_NilError(t *testing.T) {
    58  	var cause error
    59  	err := NewRuntimeError(cause)
    60  	assertGoidGopc(t, err)
    61  	assert.Equal(t, "", err.Message())
    62  	assert.Greater(t, len(err.StackTrace()), 1)
    63  	assert.Nil(t, err.Cause())
    64  	lines := strings.Split(err.Error(), newLine)
    65  	assert.Equal(t, "RuntimeError", lines[0])
    66  	assert.Equal(t, "   at ", lines[1][:6])
    67  }
    68  
    69  func TestNewRuntimeError_NormalError(t *testing.T) {
    70  	err := NewRuntimeError(errors.New("this is error message"))
    71  	assertGoidGopc(t, err)
    72  	assert.Equal(t, "this is error message", err.Message())
    73  	assert.Greater(t, len(err.StackTrace()), 1)
    74  	assert.Nil(t, err.Cause())
    75  	lines := strings.Split(err.Error(), newLine)
    76  	assert.Equal(t, "RuntimeError: this is error message", lines[0])
    77  	assert.Equal(t, "   at ", lines[1][:6])
    78  }
    79  
    80  func TestNewRuntimeError_NilRuntimeError(t *testing.T) {
    81  	var cause RuntimeError
    82  	err := NewRuntimeError(cause)
    83  	assertGoidGopc(t, err)
    84  	assert.Equal(t, "", err.Message())
    85  	assert.Greater(t, len(err.StackTrace()), 1)
    86  	assert.Nil(t, err.Cause())
    87  	lines := strings.Split(err.Error(), newLine)
    88  	assert.Equal(t, "RuntimeError", lines[0])
    89  	assert.Equal(t, "   at ", lines[1][:6])
    90  }
    91  
    92  func TestNewRuntimeError_NormalRuntimeError(t *testing.T) {
    93  	cause := NewRuntimeError("this is inner message")
    94  	err := NewRuntimeError(cause)
    95  	assertGoidGopc(t, err)
    96  	assert.Equal(t, "", err.Message())
    97  	assert.Greater(t, len(err.StackTrace()), 1)
    98  	assert.Same(t, cause, err.Cause())
    99  	lines := strings.Split(err.Error(), newLine)
   100  	assert.Equal(t, "RuntimeError", lines[0])
   101  	assert.Equal(t, " ---> RuntimeError: this is inner message", lines[1])
   102  }
   103  
   104  func TestNewRuntimeError_NilValue(t *testing.T) {
   105  	var cause *person
   106  	err := NewRuntimeError(cause)
   107  	assertGoidGopc(t, err)
   108  	assert.Equal(t, "<nil>", err.Message())
   109  	assert.Greater(t, len(err.StackTrace()), 1)
   110  	assert.Nil(t, err.Cause())
   111  	lines := strings.Split(err.Error(), newLine)
   112  	assert.Equal(t, "RuntimeError: <nil>", lines[0])
   113  	assert.Equal(t, "   at ", lines[1][:6])
   114  }
   115  
   116  func TestNewRuntimeError_NormalValue(t *testing.T) {
   117  	cause := person{Id: 1, Name: "Tim"}
   118  	err := NewRuntimeError(cause)
   119  	assertGoidGopc(t, err)
   120  	assert.Equal(t, "{1 Tim}", err.Message())
   121  	assert.Greater(t, len(err.StackTrace()), 1)
   122  	assert.Nil(t, err.Cause())
   123  	lines := strings.Split(err.Error(), newLine)
   124  	assert.Equal(t, "RuntimeError: {1 Tim}", lines[0])
   125  	assert.Equal(t, "   at ", lines[1][:6])
   126  }
   127  
   128  func TestNewRuntimeErrorWithMessage_EmptyString(t *testing.T) {
   129  	err := NewRuntimeErrorWithMessage("")
   130  	assertGoidGopc(t, err)
   131  	assert.Equal(t, "", err.Message())
   132  	assert.Greater(t, len(err.StackTrace()), 1)
   133  	assert.Nil(t, err.Cause())
   134  	lines := strings.Split(err.Error(), newLine)
   135  	assert.Equal(t, "RuntimeError", lines[0])
   136  	assert.Equal(t, "   at ", lines[1][:6])
   137  }
   138  
   139  func TestNewRuntimeErrorWithMessage_WhiteSpaceString(t *testing.T) {
   140  	err := NewRuntimeErrorWithMessage("\t")
   141  	assertGoidGopc(t, err)
   142  	assert.Equal(t, "\t", err.Message())
   143  	assert.Greater(t, len(err.StackTrace()), 1)
   144  	assert.Nil(t, err.Cause())
   145  	lines := strings.Split(err.Error(), newLine)
   146  	assert.Equal(t, "RuntimeError: \t", lines[0])
   147  	assert.Equal(t, "   at ", lines[1][:6])
   148  }
   149  
   150  func TestNewRuntimeErrorWithMessage_NormalString(t *testing.T) {
   151  	err := NewRuntimeErrorWithMessage("this is error message")
   152  	assertGoidGopc(t, err)
   153  	assert.Equal(t, "this is error message", err.Message())
   154  	assert.Greater(t, len(err.StackTrace()), 1)
   155  	assert.Nil(t, err.Cause())
   156  	lines := strings.Split(err.Error(), newLine)
   157  	assert.Equal(t, "RuntimeError: this is error message", lines[0])
   158  	assert.Equal(t, "   at ", lines[1][:6])
   159  }
   160  
   161  func TestNewRuntimeErrorWithMessageCause_EmptyString_Nil(t *testing.T) {
   162  	err := NewRuntimeErrorWithMessageCause("", nil)
   163  	assertGoidGopc(t, err)
   164  	assert.Equal(t, "", err.Message())
   165  	assert.Greater(t, len(err.StackTrace()), 1)
   166  	assert.Nil(t, err.Cause())
   167  	lines := strings.Split(err.Error(), newLine)
   168  	assert.Equal(t, "RuntimeError", lines[0])
   169  	assert.Equal(t, "   at ", lines[1][:6])
   170  }
   171  
   172  func TestNewRuntimeErrorWithMessageCause_EmptyString_EmptyString(t *testing.T) {
   173  	err := NewRuntimeErrorWithMessageCause("", "")
   174  	assertGoidGopc(t, err)
   175  	assert.Equal(t, "", err.Message())
   176  	assert.Greater(t, len(err.StackTrace()), 1)
   177  	assert.Nil(t, err.Cause())
   178  	lines := strings.Split(err.Error(), newLine)
   179  	assert.Equal(t, "RuntimeError", lines[0])
   180  	assert.Equal(t, "   at ", lines[1][:6])
   181  }
   182  
   183  func TestNewRuntimeErrorWithMessageCause_EmptyString_WhiteSpaceString(t *testing.T) {
   184  	err := NewRuntimeErrorWithMessageCause("", "\t")
   185  	assertGoidGopc(t, err)
   186  	assert.Equal(t, "\t", err.Message())
   187  	assert.Greater(t, len(err.StackTrace()), 1)
   188  	assert.Nil(t, err.Cause())
   189  	lines := strings.Split(err.Error(), newLine)
   190  	assert.Equal(t, "RuntimeError: \t", lines[0])
   191  	assert.Equal(t, "   at ", lines[1][:6])
   192  }
   193  
   194  func TestNewRuntimeErrorWithMessageCause_EmptyString_NormalString(t *testing.T) {
   195  	err := NewRuntimeErrorWithMessageCause("", "this is error message")
   196  	assertGoidGopc(t, err)
   197  	assert.Equal(t, "this is error message", err.Message())
   198  	assert.Greater(t, len(err.StackTrace()), 1)
   199  	assert.Nil(t, err.Cause())
   200  	lines := strings.Split(err.Error(), newLine)
   201  	assert.Equal(t, "RuntimeError: this is error message", lines[0])
   202  	assert.Equal(t, "   at ", lines[1][:6])
   203  }
   204  
   205  func TestNewRuntimeErrorWithMessageCause_EmptyString_NilError(t *testing.T) {
   206  	var cause error
   207  	err := NewRuntimeErrorWithMessageCause("", cause)
   208  	assertGoidGopc(t, err)
   209  	assert.Equal(t, "", err.Message())
   210  	assert.Greater(t, len(err.StackTrace()), 1)
   211  	assert.Nil(t, err.Cause())
   212  	lines := strings.Split(err.Error(), newLine)
   213  	assert.Equal(t, "RuntimeError", lines[0])
   214  	assert.Equal(t, "   at ", lines[1][:6])
   215  }
   216  
   217  func TestNewRuntimeErrorWithMessageCause_EmptyString_NormalError(t *testing.T) {
   218  	err := NewRuntimeErrorWithMessageCause("", errors.New("this is error message"))
   219  	assertGoidGopc(t, err)
   220  	assert.Equal(t, "this is error message", err.Message())
   221  	assert.Greater(t, len(err.StackTrace()), 1)
   222  	assert.Nil(t, err.Cause())
   223  	lines := strings.Split(err.Error(), newLine)
   224  	assert.Equal(t, "RuntimeError: this is error message", lines[0])
   225  	assert.Equal(t, "   at ", lines[1][:6])
   226  }
   227  
   228  func TestNewRuntimeErrorWithMessageCause_EmptyString_NilRuntimeError(t *testing.T) {
   229  	var cause RuntimeError
   230  	err := NewRuntimeErrorWithMessageCause("", cause)
   231  	assertGoidGopc(t, err)
   232  	assert.Equal(t, "", err.Message())
   233  	assert.Greater(t, len(err.StackTrace()), 1)
   234  	assert.Nil(t, err.Cause())
   235  	lines := strings.Split(err.Error(), newLine)
   236  	assert.Equal(t, "RuntimeError", lines[0])
   237  	assert.Equal(t, "   at ", lines[1][:6])
   238  }
   239  
   240  func TestNewRuntimeErrorWithMessageCause_EmptyString_NormalRuntimeError(t *testing.T) {
   241  	cause := NewRuntimeError("this is inner message")
   242  	err := NewRuntimeErrorWithMessageCause("", cause)
   243  	assertGoidGopc(t, err)
   244  	assert.Equal(t, "", err.Message())
   245  	assert.Greater(t, len(err.StackTrace()), 1)
   246  	assert.Same(t, cause, err.Cause())
   247  	lines := strings.Split(err.Error(), newLine)
   248  	assert.Equal(t, "RuntimeError", lines[0])
   249  	assert.Equal(t, " ---> RuntimeError: this is inner message", lines[1])
   250  }
   251  
   252  func TestNewRuntimeErrorWithMessageCause_EmptyString_NilValue(t *testing.T) {
   253  	var cause *person
   254  	err := NewRuntimeErrorWithMessageCause("", cause)
   255  	assertGoidGopc(t, err)
   256  	assert.Equal(t, "<nil>", err.Message())
   257  	assert.Greater(t, len(err.StackTrace()), 1)
   258  	assert.Nil(t, err.Cause())
   259  	lines := strings.Split(err.Error(), newLine)
   260  	assert.Equal(t, "RuntimeError: <nil>", lines[0])
   261  	assert.Equal(t, "   at ", lines[1][:6])
   262  }
   263  
   264  func TestNewRuntimeErrorWithMessageCause_EmptyString_NormalValue(t *testing.T) {
   265  	cause := person{Id: 1, Name: "Tim"}
   266  	err := NewRuntimeErrorWithMessageCause("", cause)
   267  	assertGoidGopc(t, err)
   268  	assert.Equal(t, "{1 Tim}", err.Message())
   269  	assert.Greater(t, len(err.StackTrace()), 1)
   270  	assert.Nil(t, err.Cause())
   271  	lines := strings.Split(err.Error(), newLine)
   272  	assert.Equal(t, "RuntimeError: {1 Tim}", lines[0])
   273  	assert.Equal(t, "   at ", lines[1][:6])
   274  }
   275  
   276  func TestNewRuntimeErrorWithMessageCause_WhiteSpaceString_Nil(t *testing.T) {
   277  	err := NewRuntimeErrorWithMessageCause("\t", nil)
   278  	assertGoidGopc(t, err)
   279  	assert.Equal(t, "\t", err.Message())
   280  	assert.Greater(t, len(err.StackTrace()), 1)
   281  	assert.Nil(t, err.Cause())
   282  	lines := strings.Split(err.Error(), newLine)
   283  	assert.Equal(t, "RuntimeError: \t", lines[0])
   284  	assert.Equal(t, "   at ", lines[1][:6])
   285  }
   286  
   287  func TestNewRuntimeErrorWithMessageCause_WhiteSpaceString_EmptyString(t *testing.T) {
   288  	err := NewRuntimeErrorWithMessageCause("\t", "")
   289  	assertGoidGopc(t, err)
   290  	assert.Equal(t, "\t", err.Message())
   291  	assert.Greater(t, len(err.StackTrace()), 1)
   292  	assert.Nil(t, err.Cause())
   293  	lines := strings.Split(err.Error(), newLine)
   294  	assert.Equal(t, "RuntimeError: \t", lines[0])
   295  	assert.Equal(t, "   at ", lines[1][:6])
   296  }
   297  
   298  func TestNewRuntimeErrorWithMessageCause_WhiteSpaceString_WhiteSpaceString(t *testing.T) {
   299  	err := NewRuntimeErrorWithMessageCause("\t", "\t")
   300  	assertGoidGopc(t, err)
   301  	assert.Equal(t, "\t - \t", err.Message())
   302  	assert.Greater(t, len(err.StackTrace()), 1)
   303  	assert.Nil(t, err.Cause())
   304  	lines := strings.Split(err.Error(), newLine)
   305  	assert.Equal(t, "RuntimeError: \t - \t", lines[0])
   306  	assert.Equal(t, "   at ", lines[1][:6])
   307  }
   308  
   309  func TestNewRuntimeErrorWithMessageCause_WhiteSpaceString_NormalString(t *testing.T) {
   310  	err := NewRuntimeErrorWithMessageCause("\t", "this is error message")
   311  	assertGoidGopc(t, err)
   312  	assert.Equal(t, "\t - this is error message", err.Message())
   313  	assert.Greater(t, len(err.StackTrace()), 1)
   314  	assert.Nil(t, err.Cause())
   315  	lines := strings.Split(err.Error(), newLine)
   316  	assert.Equal(t, "RuntimeError: \t - this is error message", lines[0])
   317  	assert.Equal(t, "   at ", lines[1][:6])
   318  }
   319  
   320  func TestNewRuntimeErrorWithMessageCause_WhiteSpaceString_NilError(t *testing.T) {
   321  	var cause error
   322  	err := NewRuntimeErrorWithMessageCause("\t", cause)
   323  	assertGoidGopc(t, err)
   324  	assert.Equal(t, "\t", err.Message())
   325  	assert.Greater(t, len(err.StackTrace()), 1)
   326  	assert.Nil(t, err.Cause())
   327  	lines := strings.Split(err.Error(), newLine)
   328  	assert.Equal(t, "RuntimeError: \t", lines[0])
   329  	assert.Equal(t, "   at ", lines[1][:6])
   330  }
   331  
   332  func TestNewRuntimeErrorWithMessageCause_WhiteSpaceString_NormalError(t *testing.T) {
   333  	err := NewRuntimeErrorWithMessageCause("\t", errors.New("this is error message"))
   334  	assertGoidGopc(t, err)
   335  	assert.Equal(t, "\t - this is error message", err.Message())
   336  	assert.Greater(t, len(err.StackTrace()), 1)
   337  	assert.Nil(t, err.Cause())
   338  	lines := strings.Split(err.Error(), newLine)
   339  	assert.Equal(t, "RuntimeError: \t - this is error message", lines[0])
   340  	assert.Equal(t, "   at ", lines[1][:6])
   341  }
   342  
   343  func TestNewRuntimeErrorWithMessageCause_WhiteSpaceString_NilRuntimeError(t *testing.T) {
   344  	var cause RuntimeError
   345  	err := NewRuntimeErrorWithMessageCause("\t", cause)
   346  	assertGoidGopc(t, err)
   347  	assert.Equal(t, "\t", err.Message())
   348  	assert.Greater(t, len(err.StackTrace()), 1)
   349  	assert.Nil(t, err.Cause())
   350  	lines := strings.Split(err.Error(), newLine)
   351  	assert.Equal(t, "RuntimeError: \t", lines[0])
   352  	assert.Equal(t, "   at ", lines[1][:6])
   353  }
   354  
   355  func TestNewRuntimeErrorWithMessageCause_WhiteSpaceString_NormalRuntimeError(t *testing.T) {
   356  	cause := NewRuntimeError("this is inner message")
   357  	err := NewRuntimeErrorWithMessageCause("\t", cause)
   358  	assertGoidGopc(t, err)
   359  	assert.Equal(t, "\t", err.Message())
   360  	assert.Greater(t, len(err.StackTrace()), 1)
   361  	assert.Same(t, cause, err.Cause())
   362  	lines := strings.Split(err.Error(), newLine)
   363  	assert.Equal(t, "RuntimeError: \t", lines[0])
   364  	assert.Equal(t, " ---> RuntimeError: this is inner message", lines[1])
   365  }
   366  
   367  func TestNewRuntimeErrorWithMessageCause_WhiteSpaceString_NilValue(t *testing.T) {
   368  	var cause *person
   369  	err := NewRuntimeErrorWithMessageCause("\t", cause)
   370  	assertGoidGopc(t, err)
   371  	assert.Equal(t, "\t - <nil>", err.Message())
   372  	assert.Greater(t, len(err.StackTrace()), 1)
   373  	assert.Nil(t, err.Cause())
   374  	lines := strings.Split(err.Error(), newLine)
   375  	assert.Equal(t, "RuntimeError: \t - <nil>", lines[0])
   376  	assert.Equal(t, "   at ", lines[1][:6])
   377  }
   378  
   379  func TestNewRuntimeErrorWithMessageCause_WhiteSpaceString_NormalValue(t *testing.T) {
   380  	cause := person{Id: 1, Name: "Tim"}
   381  	err := NewRuntimeErrorWithMessageCause("\t", cause)
   382  	assertGoidGopc(t, err)
   383  	assert.Equal(t, "\t - {1 Tim}", err.Message())
   384  	assert.Greater(t, len(err.StackTrace()), 1)
   385  	assert.Nil(t, err.Cause())
   386  	lines := strings.Split(err.Error(), newLine)
   387  	assert.Equal(t, "RuntimeError: \t - {1 Tim}", lines[0])
   388  	assert.Equal(t, "   at ", lines[1][:6])
   389  }
   390  
   391  func TestNewRuntimeErrorWithMessageCause_NormalString_Nil(t *testing.T) {
   392  	err := NewRuntimeErrorWithMessageCause("this is error message", nil)
   393  	assertGoidGopc(t, err)
   394  	assert.Equal(t, "this is error message", err.Message())
   395  	assert.Greater(t, len(err.StackTrace()), 1)
   396  	assert.Nil(t, err.Cause())
   397  	lines := strings.Split(err.Error(), newLine)
   398  	assert.Equal(t, "RuntimeError: this is error message", lines[0])
   399  	assert.Equal(t, "   at ", lines[1][:6])
   400  }
   401  
   402  func TestNewRuntimeErrorWithMessageCause_NormalString_EmptyString(t *testing.T) {
   403  	err := NewRuntimeErrorWithMessageCause("this is error message", "")
   404  	assertGoidGopc(t, err)
   405  	assert.Equal(t, "this is error message", err.Message())
   406  	assert.Greater(t, len(err.StackTrace()), 1)
   407  	assert.Nil(t, err.Cause())
   408  	lines := strings.Split(err.Error(), newLine)
   409  	assert.Equal(t, "RuntimeError: this is error message", lines[0])
   410  	assert.Equal(t, "   at ", lines[1][:6])
   411  }
   412  
   413  func TestNewRuntimeErrorWithMessageCause_NormalString_WhiteSpaceString(t *testing.T) {
   414  	err := NewRuntimeErrorWithMessageCause("this is error message", "\t")
   415  	assertGoidGopc(t, err)
   416  	assert.Equal(t, "this is error message - \t", err.Message())
   417  	assert.Greater(t, len(err.StackTrace()), 1)
   418  	assert.Nil(t, err.Cause())
   419  	lines := strings.Split(err.Error(), newLine)
   420  	assert.Equal(t, "RuntimeError: this is error message - \t", lines[0])
   421  	assert.Equal(t, "   at ", lines[1][:6])
   422  }
   423  
   424  func TestNewRuntimeErrorWithMessageCause_NormalString_NormalString(t *testing.T) {
   425  	err := NewRuntimeErrorWithMessageCause("this is error message", "this is error message")
   426  	assertGoidGopc(t, err)
   427  	assert.Equal(t, "this is error message - this is error message", err.Message())
   428  	assert.Greater(t, len(err.StackTrace()), 1)
   429  	assert.Nil(t, err.Cause())
   430  	lines := strings.Split(err.Error(), newLine)
   431  	assert.Equal(t, "RuntimeError: this is error message - this is error message", lines[0])
   432  	assert.Equal(t, "   at ", lines[1][:6])
   433  }
   434  
   435  func TestNewRuntimeErrorWithMessageCause_NormalString_NilError(t *testing.T) {
   436  	var cause error
   437  	err := NewRuntimeErrorWithMessageCause("this is error message", cause)
   438  	assertGoidGopc(t, err)
   439  	assert.Equal(t, "this is error message", err.Message())
   440  	assert.Greater(t, len(err.StackTrace()), 1)
   441  	assert.Nil(t, err.Cause())
   442  	lines := strings.Split(err.Error(), newLine)
   443  	assert.Equal(t, "RuntimeError: this is error message", lines[0])
   444  	assert.Equal(t, "   at ", lines[1][:6])
   445  }
   446  
   447  func TestNewRuntimeErrorWithMessageCause_NormalString_NormalError(t *testing.T) {
   448  	err := NewRuntimeErrorWithMessageCause("this is error message", errors.New("this is error message2"))
   449  	assertGoidGopc(t, err)
   450  	assert.Equal(t, "this is error message - this is error message2", err.Message())
   451  	assert.Greater(t, len(err.StackTrace()), 1)
   452  	assert.Nil(t, err.Cause())
   453  	lines := strings.Split(err.Error(), newLine)
   454  	assert.Equal(t, "RuntimeError: this is error message - this is error message2", lines[0])
   455  	assert.Equal(t, "   at ", lines[1][:6])
   456  }
   457  
   458  func TestNewRuntimeErrorWithMessageCause_NormalString_NilRuntimeError(t *testing.T) {
   459  	var cause RuntimeError
   460  	err := NewRuntimeErrorWithMessageCause("this is error message", cause)
   461  	assertGoidGopc(t, err)
   462  	assert.Equal(t, "this is error message", err.Message())
   463  	assert.Greater(t, len(err.StackTrace()), 1)
   464  	assert.Nil(t, err.Cause())
   465  	lines := strings.Split(err.Error(), newLine)
   466  	assert.Equal(t, "RuntimeError: this is error message", lines[0])
   467  	assert.Equal(t, "   at ", lines[1][:6])
   468  }
   469  
   470  func TestNewRuntimeErrorWithMessageCause_NormalString_NormalRuntimeError(t *testing.T) {
   471  	cause := NewRuntimeError("this is inner message")
   472  	err := NewRuntimeErrorWithMessageCause("this is error message", cause)
   473  	assertGoidGopc(t, err)
   474  	assert.Equal(t, "this is error message", err.Message())
   475  	assert.Greater(t, len(err.StackTrace()), 1)
   476  	assert.Same(t, cause, err.Cause())
   477  	lines := strings.Split(err.Error(), newLine)
   478  	assert.Equal(t, "RuntimeError: this is error message", lines[0])
   479  	assert.Equal(t, " ---> RuntimeError: this is inner message", lines[1])
   480  }
   481  
   482  func TestNewRuntimeErrorWithMessageCause_NormalString_NilValue(t *testing.T) {
   483  	var cause *person
   484  	err := NewRuntimeErrorWithMessageCause("this is error message", cause)
   485  	assertGoidGopc(t, err)
   486  	assert.Equal(t, "this is error message - <nil>", err.Message())
   487  	assert.Greater(t, len(err.StackTrace()), 1)
   488  	assert.Nil(t, err.Cause())
   489  	lines := strings.Split(err.Error(), newLine)
   490  	assert.Equal(t, "RuntimeError: this is error message - <nil>", lines[0])
   491  	assert.Equal(t, "   at ", lines[1][:6])
   492  }
   493  
   494  func TestNewRuntimeErrorWithMessageCause_NormalString_NormalValue(t *testing.T) {
   495  	cause := person{Id: 1, Name: "Tim"}
   496  	err := NewRuntimeErrorWithMessageCause("this is error message", cause)
   497  	assertGoidGopc(t, err)
   498  	assert.Equal(t, "this is error message - {1 Tim}", err.Message())
   499  	assert.Greater(t, len(err.StackTrace()), 1)
   500  	assert.Nil(t, err.Cause())
   501  	lines := strings.Split(err.Error(), newLine)
   502  	assert.Equal(t, "RuntimeError: this is error message - {1 Tim}", lines[0])
   503  	assert.Equal(t, "   at ", lines[1][:6])
   504  }
   505  
   506  func assertGoidGopc(t *testing.T, err RuntimeError) {
   507  	assert.Equal(t, Goid(), err.Goid())
   508  	assert.NotNil(t, runtime.FuncForPC(err.Gopc()-1))
   509  }
   510  
   511  //===
   512  
   513  // BenchmarkDebugStack-8                             301474              3400 ns/op            1024 B/op          1 allocs/op
   514  func BenchmarkDebugStack(b *testing.B) {
   515  	b.ReportAllocs()
   516  	b.ResetTimer()
   517  	for i := 0; i < b.N; i++ {
   518  		_ = debug.Stack()
   519  	}
   520  }
   521  
   522  // BenchmarkRuntimeError-8                           342477              3344 ns/op            2840 B/op         15 allocs/op
   523  func BenchmarkRuntimeError(b *testing.B) {
   524  	b.ReportAllocs()
   525  	b.ResetTimer()
   526  	for i := 0; i < b.N; i++ {
   527  		_ = NewRuntimeError(nil).Error()
   528  	}
   529  }
   530  
   531  // BenchmarkRuntimeErrorWithMessage-8                337117              3300 ns/op            2872 B/op         15 allocs/op
   532  func BenchmarkRuntimeErrorWithMessage(b *testing.B) {
   533  	b.ReportAllocs()
   534  	b.ResetTimer()
   535  	for i := 0; i < b.N; i++ {
   536  		_ = NewRuntimeErrorWithMessage("").Error()
   537  	}
   538  }
   539  
   540  // BenchmarkRuntimeErrorWithMessageCause-8           350422              3362 ns/op            2872 B/op         15 allocs/op
   541  func BenchmarkRuntimeErrorWithMessageCause(b *testing.B) {
   542  	b.ReportAllocs()
   543  	b.ResetTimer()
   544  	for i := 0; i < b.N; i++ {
   545  		_ = NewRuntimeErrorWithMessageCause("", nil).Error()
   546  	}
   547  }