github.com/cloudwego/hertz@v0.9.3/pkg/common/test/assert/assert.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package assert
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  // Assert .
    25  func Assert(t testing.TB, cond bool, val ...interface{}) {
    26  	t.Helper()
    27  	if !cond {
    28  		if len(val) > 0 {
    29  			val = append([]interface{}{"assertion failed:"}, val...)
    30  			t.Fatal(val...)
    31  		} else {
    32  			t.Fatal("assertion failed")
    33  		}
    34  	}
    35  }
    36  
    37  // Assertf .
    38  func Assertf(t testing.TB, cond bool, format string, val ...interface{}) {
    39  	t.Helper()
    40  	if !cond {
    41  		t.Fatalf(format, val...)
    42  	}
    43  }
    44  
    45  // DeepEqual .
    46  func DeepEqual(t testing.TB, expected, actual interface{}) {
    47  	t.Helper()
    48  	if !reflect.DeepEqual(actual, expected) {
    49  		t.Fatalf("assertion failed, unexpected: %v, expected: %v", actual, expected)
    50  	}
    51  }
    52  
    53  func Nil(t testing.TB, data interface{}) {
    54  	t.Helper()
    55  	if data == nil {
    56  		return
    57  	}
    58  	if !reflect.ValueOf(data).IsNil() {
    59  		t.Fatalf("assertion failed, unexpected: %v, expected: nil", data)
    60  	}
    61  }
    62  
    63  func NotNil(t testing.TB, data interface{}) {
    64  	t.Helper()
    65  	if data != nil {
    66  		return
    67  	}
    68  
    69  	if reflect.ValueOf(data).IsNil() {
    70  		t.Fatalf("assertion failed, unexpected: %v, expected: not nil", data)
    71  	}
    72  }
    73  
    74  // NotEqual .
    75  func NotEqual(t testing.TB, expected, actual interface{}) {
    76  	t.Helper()
    77  	if expected == nil || actual == nil {
    78  		if expected == actual {
    79  			t.Fatalf("assertion failed, unexpected: %v, expected: %v", actual, expected)
    80  		}
    81  	}
    82  
    83  	if reflect.DeepEqual(actual, expected) {
    84  		t.Fatalf("assertion failed, unexpected: %v, expected: %v", actual, expected)
    85  	}
    86  }
    87  
    88  func True(t testing.TB, obj interface{}) {
    89  	t.Helper()
    90  	DeepEqual(t, true, obj)
    91  }
    92  
    93  func False(t testing.TB, obj interface{}) {
    94  	t.Helper()
    95  	DeepEqual(t, false, obj)
    96  }
    97  
    98  // Panic .
    99  func Panic(t testing.TB, fn func()) {
   100  	t.Helper()
   101  	defer func() {
   102  		if err := recover(); err == nil {
   103  			t.Fatal("assertion failed: did not panic")
   104  		}
   105  	}()
   106  	fn()
   107  }
   108  
   109  // NotPanic .
   110  func NotPanic(t testing.TB, fn func()) {
   111  	t.Helper()
   112  	defer func() {
   113  		if err := recover(); err != nil {
   114  			t.Fatal("assertion failed: did panic")
   115  		}
   116  	}()
   117  	fn()
   118  }