github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/ledger/testutil/test_util.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     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 testutil
    18  
    19  import (
    20  	"crypto/rand"
    21  	"fmt"
    22  	"reflect"
    23  	"runtime"
    24  	"testing"
    25  )
    26  
    27  // AssertNil varifies that the value is nil
    28  func AssertNil(t testing.TB, value interface{}) {
    29  	if !isNil(value) {
    30  		t.Fatalf("Value not nil. value=[%#v]\n %s", value, getCallerInfo())
    31  	}
    32  }
    33  
    34  // AssertNotNil varifies that the value is not nil
    35  func AssertNotNil(t testing.TB, value interface{}) {
    36  	if isNil(value) {
    37  		t.Fatalf("Values is nil. %s", getCallerInfo())
    38  	}
    39  }
    40  
    41  // AssertSame varifies that the two values are same
    42  func AssertSame(t testing.TB, actual interface{}, expected interface{}) {
    43  	t.Logf("%s: AssertSame [%#v] and [%#v]", getCallerInfo(), actual, expected)
    44  	if actual != expected {
    45  		t.Fatalf("Values actual=[%#v] and expected=[%#v] do not point to same object. %s", actual, expected, getCallerInfo())
    46  	}
    47  }
    48  
    49  // AssertEquals varifies that the two values are equal
    50  func AssertEquals(t testing.TB, actual interface{}, expected interface{}) {
    51  	t.Logf("%s: AssertEquals [%#v] and [%#v]", getCallerInfo(), actual, expected)
    52  	if expected == nil && isNil(actual) {
    53  		return
    54  	}
    55  	if !reflect.DeepEqual(actual, expected) {
    56  		t.Fatalf("Values are not equal.\n Actual=[%#v], \n Expected=[%#v]\n %s", actual, expected, getCallerInfo())
    57  	}
    58  }
    59  
    60  // AssertNotEquals varifies that the two values are not equal
    61  func AssertNotEquals(t testing.TB, actual interface{}, expected interface{}) {
    62  	if reflect.DeepEqual(actual, expected) {
    63  		t.Fatalf("Values are not supposed to be equal. Actual=[%#v], Expected=[%#v]\n %s", actual, expected, getCallerInfo())
    64  	}
    65  }
    66  
    67  // AssertError varifies that the err is not nil
    68  func AssertError(t testing.TB, err error, message string) {
    69  	if err == nil {
    70  		t.Fatalf("%s\n %s", message, getCallerInfo())
    71  	}
    72  }
    73  
    74  // AssertNoError varifies that the err is nil
    75  func AssertNoError(t testing.TB, err error, message string) {
    76  	if err != nil {
    77  		t.Fatalf("%s - Error: %s\n %s", message, err, getCallerInfo())
    78  	}
    79  }
    80  
    81  // AssertContains varifies that the slice contains the value
    82  func AssertContains(t testing.TB, slice interface{}, value interface{}) {
    83  	if reflect.TypeOf(slice).Kind() != reflect.Slice && reflect.TypeOf(slice).Kind() != reflect.Array {
    84  		t.Fatalf("Type of argument 'slice' is expected to be a slice/array, found =[%s]\n %s", reflect.TypeOf(slice), getCallerInfo())
    85  	}
    86  
    87  	if !Contains(slice, value) {
    88  		t.Fatalf("Expected value [%s] not found in slice %s\n %s", value, slice, getCallerInfo())
    89  	}
    90  }
    91  
    92  // AssertContainsAll varifies that sliceActual is a superset of sliceExpected
    93  func AssertContainsAll(t testing.TB, sliceActual interface{}, sliceExpected interface{}) {
    94  	if reflect.TypeOf(sliceActual).Kind() != reflect.Slice && reflect.TypeOf(sliceActual).Kind() != reflect.Array {
    95  		t.Fatalf("Type of argument 'sliceActual' is expected to be a slice/array, found =[%s]\n %s", reflect.TypeOf(sliceActual), getCallerInfo())
    96  	}
    97  
    98  	if reflect.TypeOf(sliceExpected).Kind() != reflect.Slice && reflect.TypeOf(sliceExpected).Kind() != reflect.Array {
    99  		t.Fatalf("Type of argument 'sliceExpected' is expected to be a slice/array, found =[%s]\n %s", reflect.TypeOf(sliceExpected), getCallerInfo())
   100  	}
   101  
   102  	array := reflect.ValueOf(sliceExpected)
   103  	for i := 0; i < array.Len(); i++ {
   104  		element := array.Index(i).Interface()
   105  		if !Contains(sliceActual, element) {
   106  			t.Fatalf("Expected value [%s] not found in slice %s\n %s", element, sliceActual, getCallerInfo())
   107  		}
   108  	}
   109  }
   110  
   111  // AssertPanic varifies that a panic is raised during a test
   112  func AssertPanic(t testing.TB, msg string) {
   113  	x := recover()
   114  	if x == nil {
   115  		t.Fatal(msg)
   116  	} else {
   117  		t.Logf("A panic was caught successfully. Actual msg = %s", x)
   118  	}
   119  }
   120  
   121  // ConstructRandomBytes constructs random bytes of given size
   122  func ConstructRandomBytes(t testing.TB, size int) []byte {
   123  	value := make([]byte, size)
   124  	_, err := rand.Read(value)
   125  	if err != nil {
   126  		t.Fatalf("Error while generating random bytes: %s", err)
   127  	}
   128  	return value
   129  }
   130  
   131  // Contains returns true iff the `value` is present in the `slice`
   132  func Contains(slice interface{}, value interface{}) bool {
   133  	array := reflect.ValueOf(slice)
   134  	for i := 0; i < array.Len(); i++ {
   135  		element := array.Index(i).Interface()
   136  		if value == element || reflect.DeepEqual(element, value) {
   137  			return true
   138  		}
   139  	}
   140  	return false
   141  }
   142  
   143  func isNil(in interface{}) bool {
   144  	return in == nil || reflect.ValueOf(in).IsNil() || (reflect.TypeOf(in).Kind() == reflect.Slice && reflect.ValueOf(in).Len() == 0)
   145  }
   146  
   147  func getCallerInfo() string {
   148  	_, file, line, ok := runtime.Caller(2)
   149  	if !ok {
   150  		return "Could not retrieve caller's info"
   151  	}
   152  	return fmt.Sprintf("CallerInfo = [%s:%d]", file, line)
   153  }