Add new function Assert methods

Bug: 181070625
Test: m nothing
Change-Id: If7977493d4f7380e746df1b9b8b4cb13c39abeac
This commit is contained in:
Paul Duffin
2021-03-12 11:50:43 +00:00
parent 0550055007
commit 9dcf253197
3 changed files with 143 additions and 34 deletions

View File

@@ -15,8 +15,6 @@
package android
import (
"reflect"
"strings"
"testing"
)
@@ -28,68 +26,47 @@ type TestHelper struct {
// AssertBoolEquals checks if the expected and actual values are equal and if they are not then it
// reports an error prefixed with the supplied message and including a reason for why it failed.
func (h *TestHelper) AssertBoolEquals(message string, expected bool, actual bool) {
h.Helper()
if actual != expected {
h.Errorf("%s: expected %t, actual %t", message, expected, actual)
}
AssertBoolEquals(h.T, message, expected, actual)
}
// AssertStringEquals checks if the expected and actual values are equal and if they are not then
// it reports an error prefixed with the supplied message and including a reason for why it failed.
func (h *TestHelper) AssertStringEquals(message string, expected string, actual string) {
h.Helper()
if actual != expected {
h.Errorf("%s: expected %s, actual %s", message, expected, actual)
}
AssertStringEquals(h.T, message, expected, actual)
}
// AssertErrorMessageEquals checks if the error is not nil and has the expected message. If it does
// not then this reports an error prefixed with the supplied message and including a reason for why
// it failed.
func (h *TestHelper) AssertErrorMessageEquals(message string, expected string, actual error) {
h.Helper()
if actual == nil {
h.Errorf("Expected error but was nil")
} else if actual.Error() != expected {
h.Errorf("%s: expected %s, actual %s", message, expected, actual.Error())
}
AssertErrorMessageEquals(h.T, message, expected, actual)
}
// AssertTrimmedStringEquals checks if the expected and actual values are the same after trimming
// leading and trailing spaces from them both. If they are not then it reports an error prefixed
// with the supplied message and including a reason for why it failed.
func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) {
h.Helper()
h.AssertStringEquals(message, strings.TrimSpace(expected), strings.TrimSpace(actual))
AssertTrimmedStringEquals(h.T, message, expected, actual)
}
// AssertStringDoesContain checks if the string contains the expected substring. If it does not
// then it reports an error prefixed with the supplied message and including a reason for why it
// failed.
func (h *TestHelper) AssertStringDoesContain(message string, s string, expectedSubstring string) {
h.Helper()
if !strings.Contains(s, expectedSubstring) {
h.Errorf("%s: could not find %q within %q", message, expectedSubstring, s)
}
AssertStringDoesContain(h.T, message, s, expectedSubstring)
}
// AssertStringDoesNotContain checks if the string contains the expected substring. If it does then
// it reports an error prefixed with the supplied message and including a reason for why it failed.
func (h *TestHelper) AssertStringDoesNotContain(message string, s string, unexpectedSubstring string) {
h.Helper()
if strings.Contains(s, unexpectedSubstring) {
h.Errorf("%s: unexpectedly found %q within %q", message, unexpectedSubstring, s)
}
AssertStringDoesNotContain(h.T, message, s, unexpectedSubstring)
}
// AssertStringListContains checks if the list of strings contains the expected string. If it does
// not then it reports an error prefixed with the supplied message and including a reason for why it
// failed.
func (h *TestHelper) AssertStringListContains(message string, list []string, expected string) {
h.Helper()
if !InList(expected, list) {
h.Errorf("%s: could not find %q within %q", message, expected, list)
}
AssertStringListContains(h.T, message, list, expected)
}
// AssertArrayString checks if the expected and actual values are equal and if they are not then it
@@ -113,10 +90,7 @@ func (h *TestHelper) AssertArrayString(message string, expected, actual []string
// if they are not then it reports an error prefixed with the supplied message and including a
// reason for why it failed.
func (h *TestHelper) AssertDeepEquals(message string, expected interface{}, actual interface{}) {
h.Helper()
if !reflect.DeepEqual(actual, expected) {
h.Errorf("%s: expected:\n %#v\n got:\n %#v", message, expected, actual)
}
AssertDeepEquals(h.T, message, expected, actual)
}
// AssertPanic checks that the supplied function panics as expected.