github.com/sandwich-go/boost@v1.3.29/xpanic/try_test.go (about)

     1  package xpanic
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func Test_NormalFlow(T *testing.T) {
     8  	called := false
     9  
    10  	Try(func() {
    11  		called = true
    12  
    13  	}).Catch(func(_ E) {
    14  		T.Error("Catch must not be called")
    15  	})
    16  
    17  	// if try was not called
    18  	if !called {
    19  		T.Error("Try do not called")
    20  	}
    21  }
    22  
    23  func Test_NormalFlowFinally(T *testing.T) {
    24  	calledTry := false
    25  	calledFinally := false
    26  
    27  	Try(func() {
    28  		calledTry = true
    29  
    30  	}).Finally(func() {
    31  		calledFinally = true
    32  
    33  	}).Catch(func(_ E) {
    34  		T.Error("Catch must not be called")
    35  
    36  	})
    37  
    38  	// if try was not called
    39  	if !calledTry {
    40  		T.Error("Try do not called")
    41  	}
    42  
    43  	// if finally was not called
    44  	if !calledFinally {
    45  		T.Error("Finally do not called")
    46  	}
    47  }
    48  
    49  func Test_CrashInTry(T *testing.T) {
    50  	calledFinally := false
    51  	calledCatch := false
    52  
    53  	Try(func() {
    54  		panic("testing panic")
    55  
    56  	}).Finally(func() {
    57  		calledFinally = true
    58  
    59  	}).Catch(func(e E) {
    60  		calledCatch = true
    61  		if e != "testing panic" {
    62  			T.Error("error is not 'testing panic'")
    63  		}
    64  	})
    65  
    66  	// if catch was not called
    67  	if !calledCatch {
    68  		T.Error("Catch do not called")
    69  	}
    70  
    71  	// if finally was not called
    72  	if !calledFinally {
    73  		T.Error("Finally do not called")
    74  	}
    75  }
    76  
    77  func Test_CrashInTry2(T *testing.T) {
    78  	calledCatch := false
    79  
    80  	Try(func() {
    81  		panic("testing panic")
    82  
    83  	}).Catch(func(e E) {
    84  		calledCatch = true
    85  		if e != "testing panic" {
    86  			T.Error("error is not 'testing panic'")
    87  		}
    88  	})
    89  
    90  	// if catch was not called
    91  	if !calledCatch {
    92  		T.Error("Catch do not called")
    93  	}
    94  }
    95  
    96  func Test_CrashInCatch(T *testing.T) {
    97  	calledFinally := false
    98  
    99  	defer func() {
   100  		err := recover()
   101  		if err != "another panic" {
   102  			T.Error("error is not 'another panic'")
   103  		}
   104  		// if finally was not called
   105  		if !calledFinally {
   106  			T.Error("Finally do not called")
   107  		}
   108  	}()
   109  	Try(func() {
   110  		panic("testing panic")
   111  
   112  	}).Finally(func() {
   113  		calledFinally = true
   114  
   115  	}).Catch(func(e E) {
   116  		if e != "testing panic" {
   117  			T.Error("error is not 'testing panic'")
   118  		}
   119  		panic("another panic")
   120  
   121  	})
   122  }
   123  
   124  func Test_CrashInCatch2(T *testing.T) {
   125  	defer func() {
   126  		err := recover()
   127  		if err != "another panic" {
   128  			T.Error("error is not 'another panic'")
   129  		}
   130  	}()
   131  	Try(func() {
   132  		panic("testing panic")
   133  
   134  	}).Catch(func(e E) {
   135  		if e != "testing panic" {
   136  			T.Error("error is not 'testing panic'")
   137  		}
   138  		panic("another panic")
   139  	})
   140  }
   141  
   142  func Test_CrashInThrow(T *testing.T) {
   143  	calledFinally := false
   144  
   145  	defer func() {
   146  		err := recover()
   147  		if err != "testing panic" {
   148  			T.Error("error is not 'testing panic'")
   149  		}
   150  		// if finally was not called
   151  		if !calledFinally {
   152  			T.Error("Finally do not called")
   153  		}
   154  	}()
   155  
   156  	Try(func() {
   157  		panic("testing panic")
   158  
   159  	}).Finally(func() {
   160  		calledFinally = true
   161  
   162  	}).Catch(func(e E) {
   163  		if e != "testing panic" {
   164  			T.Error("error is not 'testing panic'")
   165  		}
   166  		Throw()
   167  	})
   168  }
   169  
   170  func Test_CrashInThrow2(T *testing.T) {
   171  	defer func() {
   172  		err := recover()
   173  		if err != "testing panic" {
   174  			T.Error("error is not 'testing panic'")
   175  		}
   176  	}()
   177  
   178  	Try(func() {
   179  		panic("testing panic")
   180  
   181  	}).Catch(func(e E) {
   182  		if e != "testing panic" {
   183  			T.Error("error is not 'testing panic'")
   184  		}
   185  		Throw()
   186  	})
   187  }
   188  
   189  func Test_CrashInFinally1(T *testing.T) {
   190  	calledTry := false
   191  
   192  	defer func() {
   193  		err := recover()
   194  		if err != "finally panic" {
   195  			T.Error("error is not 'finally panic'")
   196  		}
   197  
   198  		// if try was not called
   199  		if !calledTry {
   200  			T.Error("Try do not called")
   201  		}
   202  	}()
   203  
   204  	Try(func() {
   205  		calledTry = true
   206  
   207  	}).Finally(func() {
   208  		panic("finally panic")
   209  
   210  	}).Catch(func(e E) {
   211  		T.Error("Catch must not be called")
   212  	})
   213  }
   214  
   215  func Test_CrashInFinally2(T *testing.T) {
   216  
   217  	defer func() {
   218  		err := recover()
   219  		if err != "finally panic" {
   220  			T.Error("error is not 'finally panic'")
   221  		}
   222  	}()
   223  
   224  	Try(func() {
   225  		panic("testing panic")
   226  
   227  	}).Finally(func() {
   228  		panic("finally panic")
   229  
   230  	}).Catch(func(e E) {
   231  		if e != "testing panic" {
   232  			T.Error("error is not 'testing panic'")
   233  		}
   234  		panic("another panic")
   235  
   236  	})
   237  }