github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/timecmp/assert.go (about)

     1  package timecmp
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  type stringableTimeValue interface {
    11  	commonTime
    12  	String() string
    13  }
    14  
    15  // AssertTimeEqual compares two time values using timecmp.Equal and fails the test if not equal.
    16  //
    17  // This simplifies comparing Go stdlib time.Time values with apiserver metav1.Time / metav1.MicroTime values
    18  // based on the minimum granularity between the two values.
    19  func AssertTimeEqual(t testing.TB, expected stringableTimeValue, actual stringableTimeValue) (equal bool) {
    20  	t.Helper()
    21  
    22  	if !Equal(expected, actual) {
    23  		return assert.Fail(t, fmt.Sprintf("Not equal: \n"+
    24  			"expected: %v\n"+
    25  			"actual  : %v", expected, actual))
    26  	}
    27  	return true
    28  }
    29  
    30  // RequireTimeEqual compares two time values using timecmp.Equal and fails the test immediately if not equal.
    31  //
    32  // This simplifies comparing Go stdlib time.Time values with apiserver metav1.Time / metav1.MicroTime values
    33  // based on the minimum granularity between the two values.
    34  func RequireTimeEqual(t testing.TB, expected stringableTimeValue, actual stringableTimeValue) {
    35  	t.Helper()
    36  	if !AssertTimeEqual(t, expected, actual) {
    37  		t.FailNow()
    38  	}
    39  }