github.com/tooploox/oya@v0.0.21-0.20230524103240-1cda1861aad6/testutil/assertions.go (about)

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"reflect"
     8  	"regexp"
     9  	"sort"
    10  	"testing"
    11  
    12  	"github.com/go-test/deep"
    13  	"github.com/pkg/errors"
    14  	"k8s.io/apimachinery/pkg/util/diff"
    15  )
    16  
    17  func AssertNoErr(t *testing.T, err error, msg string, args ...interface{}) {
    18  	t.Helper()
    19  	if err != nil {
    20  		t.Fatalf(errors.Wrapf(err, msg, args...).Error())
    21  	}
    22  }
    23  
    24  func AssertErr(t *testing.T, err error, msg string, args ...interface{}) {
    25  	t.Helper()
    26  	if err == nil {
    27  		t.Fatalf(msg, args...)
    28  	}
    29  }
    30  
    31  func AssertTrue(t *testing.T, b bool, msg string, args ...interface{}) {
    32  	t.Helper()
    33  	if !b {
    34  		t.Fatalf(msg, args...)
    35  	}
    36  }
    37  
    38  func AssertFalse(t *testing.T, b bool, msg string, args ...interface{}) {
    39  	t.Helper()
    40  	if b {
    41  		t.Fatalf(msg, args...)
    42  	}
    43  }
    44  
    45  // AssertStringsMatch compares string slices after sorting them.
    46  func AssertStringsMatch(t *testing.T, expected []string, actual []string, msg string, args ...interface{}) {
    47  	t.Helper()
    48  	expSorted := make([]string, len(expected))
    49  	copy(expSorted, expected)
    50  	sort.Strings(expSorted)
    51  	actSorted := make([]string, len(actual))
    52  	copy(actSorted, actual)
    53  	sort.Strings(actSorted)
    54  
    55  	if !reflect.DeepEqual(expSorted, actSorted) {
    56  		t.Errorf(msg, args...)
    57  	}
    58  }
    59  
    60  // AssertRegexpMatch checks if string matches the regexp.
    61  func AssertRegexpMatch(t *testing.T, expectedRegexp, actual string) {
    62  	rx := regexp.MustCompile(expectedRegexp)
    63  	if !rx.MatchString(actual) {
    64  		t.Errorf("Expected regexp %q to match %q", expectedRegexp, actual)
    65  	}
    66  }
    67  
    68  func AssertEqual(t *testing.T, expected, actual interface{}) {
    69  	t.Helper()
    70  	if !reflect.DeepEqual(expected, actual) {
    71  		t.Errorf("Expected: %v actual: %v", expected, actual)
    72  	}
    73  }
    74  
    75  func AssertEqualMsg(t *testing.T, expected, actual interface{}, msg string, args ...interface{}) {
    76  	t.Helper()
    77  	if !reflect.DeepEqual(expected, actual) {
    78  		t.Errorf("%v: expected: %v actual: %v", fmt.Sprintf(msg, args...), expected, actual)
    79  	}
    80  }
    81  
    82  func AssertObjectsEqual(t *testing.T, expected, actual interface{}) {
    83  	t.Helper()
    84  	if df := deep.Equal(expected, actual); df != nil {
    85  		t.Errorf("Objects are not equal.\n\nDiff: expected\tactual\n %v\n\nSide-by-side: %v", df, diff.ObjectGoPrintSideBySide(expected, actual))
    86  	}
    87  }
    88  
    89  func AssertObjectsEqualMsg(t *testing.T, expected, actual interface{}, msg string, args ...interface{}) {
    90  	t.Helper()
    91  	if df := deep.Equal(expected, actual); df != nil {
    92  		t.Errorf("%v: %v",
    93  			fmt.Sprintf(msg, args...),
    94  			fmt.Sprintf("objects are not equal.\n\nDiff:\n %v\n\nSide-by-side:\n%v", df, diff.ObjectGoPrintSideBySide(expected, actual)))
    95  	}
    96  }
    97  
    98  func AssertPathExists(t *testing.T, path string) {
    99  	t.Helper()
   100  	_, err := os.Stat(path)
   101  	if err != nil {
   102  		t.Errorf("path %v does not exist", path)
   103  	}
   104  }
   105  
   106  func AssertPathNotExists(t *testing.T, path string) {
   107  	t.Helper()
   108  	_, err := os.Stat(path)
   109  	if err == nil {
   110  		t.Errorf("path %v does exist", path)
   111  	}
   112  }
   113  
   114  func AssertFileContains(t *testing.T, path string, expectedContent string) {
   115  	t.Helper()
   116  	AssertPathExists(t, path)
   117  	actual, err := ioutil.ReadFile(path)
   118  	AssertNoErr(t, err, "Expected no error reading %v", path)
   119  	AssertEqual(t, expectedContent, string(actual))
   120  }