go.etcd.io/etcd@v3.3.27+incompatible/pkg/testutil/assert.go (about)

     1  // Copyright 2017 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testutil
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  	"testing"
    21  )
    22  
    23  func AssertEqual(t *testing.T, e, a interface{}, msg ...string) {
    24  	if (e == nil || a == nil) && (isNil(e) && isNil(a)) {
    25  		return
    26  	}
    27  	if reflect.DeepEqual(e, a) {
    28  		return
    29  	}
    30  	s := ""
    31  	if len(msg) > 1 {
    32  		s = msg[0] + ": "
    33  	}
    34  	s = fmt.Sprintf("%sexpected %+v, got %+v", s, e, a)
    35  	FatalStack(t, s)
    36  }
    37  
    38  func AssertNil(t *testing.T, v interface{}) {
    39  	AssertEqual(t, nil, v)
    40  }
    41  
    42  func AssertNotNil(t *testing.T, v interface{}) {
    43  	if v == nil {
    44  		t.Fatalf("expected non-nil, got %+v", v)
    45  	}
    46  }
    47  
    48  func AssertTrue(t *testing.T, v bool, msg ...string) {
    49  	AssertEqual(t, true, v, msg...)
    50  }
    51  
    52  func AssertFalse(t *testing.T, v bool, msg ...string) {
    53  	AssertEqual(t, false, v, msg...)
    54  }
    55  
    56  func isNil(v interface{}) bool {
    57  	if v == nil {
    58  		return true
    59  	}
    60  	rv := reflect.ValueOf(v)
    61  	return rv.Kind() != reflect.Struct && rv.IsNil()
    62  }