...

Source file src/go.etcd.io/etcd/client/pkg/v3/testutil/assert.go

Documentation: go.etcd.io/etcd/client/pkg/v3/testutil

     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  	t.Helper()
    25  	if (e == nil || a == nil) && (isNil(e) && isNil(a)) {
    26  		return
    27  	}
    28  	if reflect.DeepEqual(e, a) {
    29  		return
    30  	}
    31  	s := ""
    32  	if len(msg) > 1 {
    33  		s = msg[0] + ": "
    34  	}
    35  	s = fmt.Sprintf("%sexpected %+v, got %+v", s, e, a)
    36  	FatalStack(t, s)
    37  }
    38  
    39  func AssertNil(t *testing.T, v interface{}) {
    40  	t.Helper()
    41  	AssertEqual(t, nil, v)
    42  }
    43  
    44  func AssertNotNil(t *testing.T, v interface{}) {
    45  	t.Helper()
    46  	if v == nil {
    47  		t.Fatalf("expected non-nil, got %+v", v)
    48  	}
    49  }
    50  
    51  func AssertTrue(t *testing.T, v bool, msg ...string) {
    52  	t.Helper()
    53  	AssertEqual(t, true, v, msg...)
    54  }
    55  
    56  func AssertFalse(t *testing.T, v bool, msg ...string) {
    57  	t.Helper()
    58  	AssertEqual(t, false, v, msg...)
    59  }
    60  
    61  func isNil(v interface{}) bool {
    62  	if v == nil {
    63  		return true
    64  	}
    65  	rv := reflect.ValueOf(v)
    66  	return rv.Kind() != reflect.Struct && rv.IsNil()
    67  }
    68  

View as plain text