blob: d0de523935d19e61e6a3361e93916320ced2caaa [file] [log] [blame]
Paul Duffin9dcf2532021-03-12 11:50:43 +00001// Copyright 2021 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
17import (
18 "reflect"
19 "strings"
20 "testing"
21)
22
23// This file contains general purpose test assert functions.
24
25// AssertBoolEquals checks if the expected and actual values are equal and if they are not then it
26// reports an error prefixed with the supplied message and including a reason for why it failed.
27func AssertBoolEquals(t *testing.T, message string, expected bool, actual bool) {
28 t.Helper()
29 if actual != expected {
30 t.Errorf("%s: expected %t, actual %t", message, expected, actual)
31 }
32}
33
34// AssertStringEquals checks if the expected and actual values are equal and if they are not then
35// it reports an error prefixed with the supplied message and including a reason for why it failed.
36func AssertStringEquals(t *testing.T, message string, expected string, actual string) {
37 t.Helper()
38 if actual != expected {
39 t.Errorf("%s: expected %s, actual %s", message, expected, actual)
40 }
41}
42
43// AssertErrorMessageEquals checks if the error is not nil and has the expected message. If it does
44// not then this reports an error prefixed with the supplied message and including a reason for why
45// it failed.
46func AssertErrorMessageEquals(t *testing.T, message string, expected string, actual error) {
47 t.Helper()
48 if actual == nil {
49 t.Errorf("Expected error but was nil")
50 } else if actual.Error() != expected {
51 t.Errorf("%s: expected %s, actual %s", message, expected, actual.Error())
52 }
53}
54
55// AssertTrimmedStringEquals checks if the expected and actual values are the same after trimming
56// leading and trailing spaces from them both. If they are not then it reports an error prefixed
57// with the supplied message and including a reason for why it failed.
58func AssertTrimmedStringEquals(t *testing.T, message string, expected string, actual string) {
59 t.Helper()
60 AssertStringEquals(t, message, strings.TrimSpace(expected), strings.TrimSpace(actual))
61}
62
63// AssertStringDoesContain checks if the string contains the expected substring. If it does not
64// then it reports an error prefixed with the supplied message and including a reason for why it
65// failed.
66func AssertStringDoesContain(t *testing.T, message string, s string, expectedSubstring string) {
67 t.Helper()
68 if !strings.Contains(s, expectedSubstring) {
69 t.Errorf("%s: could not find %q within %q", message, expectedSubstring, s)
70 }
71}
72
73// AssertStringDoesNotContain checks if the string contains the expected substring. If it does then
74// it reports an error prefixed with the supplied message and including a reason for why it failed.
75func AssertStringDoesNotContain(t *testing.T, message string, s string, unexpectedSubstring string) {
76 t.Helper()
77 if strings.Contains(s, unexpectedSubstring) {
78 t.Errorf("%s: unexpectedly found %q within %q", message, unexpectedSubstring, s)
79 }
80}
81
82// AssertStringListContains checks if the list of strings contains the expected string. If it does
83// not then it reports an error prefixed with the supplied message and including a reason for why it
84// failed.
85func AssertStringListContains(t *testing.T, message string, list []string, expected string) {
86 t.Helper()
87 if !InList(expected, list) {
88 t.Errorf("%s: could not find %q within %q", message, expected, list)
89 }
90}
91
92// AssertArrayString checks if the expected and actual values are equal and if they are not then it
93// reports an error prefixed with the supplied message and including a reason for why it failed.
94func AssertArrayString(t *testing.T, message string, expected, actual []string) {
95 t.Helper()
96 if len(actual) != len(expected) {
97 t.Errorf("%s: expected %d (%q), actual (%d) %q", message, len(expected), expected, len(actual), actual)
98 return
99 }
100 for i := range actual {
101 if actual[i] != expected[i] {
102 t.Errorf("%s: expected %d-th, %q (%q), actual %q (%q)",
103 message, i, expected[i], expected, actual[i], actual)
104 return
105 }
106 }
107}
108
109// AssertDeepEquals checks if the expected and actual values are equal using reflect.DeepEqual and
110// if they are not then it reports an error prefixed with the supplied message and including a
111// reason for why it failed.
112func AssertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
113 t.Helper()
114 if !reflect.DeepEqual(actual, expected) {
115 t.Errorf("%s: expected:\n %#v\n got:\n %#v", message, expected, actual)
116 }
117}
118
119// AssertPanic checks that the supplied function panics as expected.
120func AssertPanic(t *testing.T, message string, funcThatShouldPanic func()) {
121 t.Helper()
122 panicked := false
123 func() {
124 defer func() {
125 if x := recover(); x != nil {
126 panicked = true
127 }
128 }()
129 funcThatShouldPanic()
130 }()
131 if !panicked {
132 t.Error(message)
133 }
134}