Skip to content

Instantly share code, notes, and snippets.

@iMega
Created June 9, 2017 17:15
Show Gist options
  • Save iMega/b8984b5dfa612af8e912e1dda197d009 to your computer and use it in GitHub Desktop.
Save iMega/b8984b5dfa612af8e912e1dda197d009 to your computer and use it in GitHub Desktop.
EqualWithDuration
func EqualWithDuration(t *testing.T, expected, actual interface{}, delta time.Duration) bool {
a := reflect.ValueOf(actual)
e := reflect.ValueOf(expected)
for i := 0; i < a.NumField(); i++ {
if a.Field(i).Type() == reflect.TypeOf(time.Time{}) {
assert.WithinDuration(t, e.Field(i).Interface().(time.Time), a.Field(i).Interface().(time.Time), delta)
} else {
assert.Equal(t, e.Field(i).Interface(), a.Field(i).Interface())
}
}
return true
}
@iMega
Copy link
Author

iMega commented Jun 13, 2017

func EqualWithDuration(t *testing.T, expected, actual interface{}, delta time.Duration) bool {
	a := reflect.ValueOf(actual)
	e := reflect.ValueOf(expected)

	for i := 0; i < a.NumField(); i++ {
		if a.Field(i).Type() == reflect.TypeOf(time.Time{}) {
			assert.WithinDuration(t, e.Field(i).Interface().(time.Time), a.Field(i).Interface().(time.Time), delta, fmt.Sprintf("Field: %s", a.Type().Field(i).Name))
		} else {
			assert.Equal(t, e.Field(i).Interface(), a.Field(i).Interface())
		}
	}
	return true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment