...

Source file src/sigs.k8s.io/cli-utils/pkg/testutil/asserter.go

Documentation: sigs.k8s.io/cli-utils/pkg/testutil

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package testutil
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	"github.com/google/go-cmp/cmp/cmpopts"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  // Asserter provides a set of assertion methods that use a shared set of
    15  // comparison options.
    16  type Asserter struct {
    17  	Options cmp.Options
    18  }
    19  
    20  // Returns a new Asserter with the specified options.
    21  func NewAsserter(opts ...cmp.Option) *Asserter {
    22  	return &Asserter{
    23  		Options: opts,
    24  	}
    25  }
    26  
    27  // DefaultAsserter is a global Asserter with default comparison options:
    28  // - EquateErrors (compare with "Is(T) bool" method)
    29  var DefaultAsserter = NewAsserter(cmpopts.EquateErrors())
    30  
    31  // EqualMatcher returns a new EqualMatcher with the Asserter's options and the
    32  // specified expected value.
    33  func (a *Asserter) EqualMatcher(expected interface{}) *EqualMatcher {
    34  	return &EqualMatcher{
    35  		Expected: expected,
    36  		Options:  a.Options,
    37  	}
    38  }
    39  
    40  // Equal fails the test if the actual value does not deeply equal the
    41  // expected value. Prints a diff on failure.
    42  func (a *Asserter) Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
    43  	t.Helper() // print the caller's file:line, instead of this func, on failure
    44  	matcher := a.EqualMatcher(expected)
    45  	match, err := matcher.Match(actual)
    46  	if err != nil {
    47  		t.Errorf("errored testing equality: %v", err)
    48  		return
    49  	}
    50  	if !match {
    51  		assert.Fail(t, matcher.FailureMessage(actual), msgAndArgs...)
    52  	}
    53  }
    54  
    55  // AssertEqual fails the test if the actual value does not deeply equal the
    56  // expected value. Prints a diff on failure.
    57  func AssertEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
    58  	t.Helper() // print the caller's file:line, instead of this func, on failure
    59  	DefaultAsserter.Equal(t, expected, actual, msgAndArgs...)
    60  }
    61  
    62  // NotEqual fails the test if the actual value deeply equals the
    63  // expected value. Prints a diff on failure.
    64  func (a *Asserter) NotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
    65  	t.Helper() // print the caller's file:line, instead of this func, on failure
    66  	matcher := a.EqualMatcher(expected)
    67  	match, err := matcher.Match(actual)
    68  	if err != nil {
    69  		t.Errorf("errored testing equality: %v", err)
    70  		return
    71  	}
    72  	if match {
    73  		assert.Fail(t, matcher.NegatedFailureMessage(actual), msgAndArgs...)
    74  	}
    75  }
    76  
    77  // AssertNotEqual fails the test if the actual value deeply equals the
    78  // expected value. Prints a diff on failure.
    79  func AssertNotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
    80  	t.Helper() // print the caller's file:line, instead of this func, on failure
    81  	DefaultAsserter.NotEqual(t, expected, actual, msgAndArgs...)
    82  }
    83  

View as plain text