blob: 977e1af9f48ea4ea8e1724a9f44d9ee7743bf81b [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
Paul Duffin3d119612021-03-16 19:30:32 +000025// AssertSame checks if the expected and actual values are equal and if they are not then
26// it reports an error prefixed with the supplied message and including a reason for why it failed.
27func AssertSame(t *testing.T, message string, expected interface{}, actual interface{}) {
28 t.Helper()
29 if actual != expected {
30 t.Errorf("%s: expected:\n%#v\nactual:\n%#v", message, expected, actual)
31 }
32}
33
Paul Duffin9dcf2532021-03-12 11:50:43 +000034// AssertBoolEquals checks if the expected and actual values are equal and if they are not then it
35// reports an error prefixed with the supplied message and including a reason for why it failed.
36func AssertBoolEquals(t *testing.T, message string, expected bool, actual bool) {
37 t.Helper()
38 if actual != expected {
39 t.Errorf("%s: expected %t, actual %t", message, expected, actual)
40 }
41}
42
43// AssertStringEquals checks if the expected and actual values are equal and if they are not then
44// it reports an error prefixed with the supplied message and including a reason for why it failed.
45func AssertStringEquals(t *testing.T, message string, expected string, actual string) {
46 t.Helper()
47 if actual != expected {
48 t.Errorf("%s: expected %s, actual %s", message, expected, actual)
49 }
50}
51
Paul Duffin567465d2021-03-16 01:21:34 +000052// AssertPathRelativeToTopEquals checks if the expected value is equal to the result of calling
53// PathRelativeToTop on the actual Path.
54func AssertPathRelativeToTopEquals(t *testing.T, message string, expected string, actual Path) {
55 t.Helper()
56 AssertStringEquals(t, message, expected, PathRelativeToTop(actual))
57}
58
59// AssertPathsRelativeToTopEquals checks if the expected value is equal to the result of calling
60// PathsRelativeToTop on the actual Paths.
61func AssertPathsRelativeToTopEquals(t *testing.T, message string, expected []string, actual Paths) {
62 t.Helper()
63 AssertDeepEquals(t, message, expected, PathsRelativeToTop(actual))
64}
65
66// AssertStringPathRelativeToTopEquals checks if the expected value is equal to the result of calling
67// StringPathRelativeToTop on the actual string path.
68func AssertStringPathRelativeToTopEquals(t *testing.T, message string, config Config, expected string, actual string) {
69 t.Helper()
70 AssertStringEquals(t, message, expected, StringPathRelativeToTop(config.buildDir, actual))
71}
72
73// AssertStringPathsRelativeToTopEquals checks if the expected value is equal to the result of
74// calling StringPathsRelativeToTop on the actual string paths.
75func AssertStringPathsRelativeToTopEquals(t *testing.T, message string, config Config, expected []string, actual []string) {
76 t.Helper()
77 AssertDeepEquals(t, message, expected, StringPathsRelativeToTop(config.buildDir, actual))
78}
79
Paul Duffin9dcf2532021-03-12 11:50:43 +000080// AssertErrorMessageEquals checks if the error is not nil and has the expected message. If it does
81// not then this reports an error prefixed with the supplied message and including a reason for why
82// it failed.
83func AssertErrorMessageEquals(t *testing.T, message string, expected string, actual error) {
84 t.Helper()
85 if actual == nil {
86 t.Errorf("Expected error but was nil")
87 } else if actual.Error() != expected {
88 t.Errorf("%s: expected %s, actual %s", message, expected, actual.Error())
89 }
90}
91
92// AssertTrimmedStringEquals checks if the expected and actual values are the same after trimming
93// leading and trailing spaces from them both. If they are not then it reports an error prefixed
94// with the supplied message and including a reason for why it failed.
95func AssertTrimmedStringEquals(t *testing.T, message string, expected string, actual string) {
96 t.Helper()
97 AssertStringEquals(t, message, strings.TrimSpace(expected), strings.TrimSpace(actual))
98}
99
100// AssertStringDoesContain checks if the string contains the expected substring. If it does not
101// then it reports an error prefixed with the supplied message and including a reason for why it
102// failed.
103func AssertStringDoesContain(t *testing.T, message string, s string, expectedSubstring string) {
104 t.Helper()
105 if !strings.Contains(s, expectedSubstring) {
106 t.Errorf("%s: could not find %q within %q", message, expectedSubstring, s)
107 }
108}
109
110// AssertStringDoesNotContain checks if the string contains the expected substring. If it does then
111// it reports an error prefixed with the supplied message and including a reason for why it failed.
112func AssertStringDoesNotContain(t *testing.T, message string, s string, unexpectedSubstring string) {
113 t.Helper()
114 if strings.Contains(s, unexpectedSubstring) {
115 t.Errorf("%s: unexpectedly found %q within %q", message, unexpectedSubstring, s)
116 }
117}
118
119// AssertStringListContains checks if the list of strings contains the expected string. If it does
120// not then it reports an error prefixed with the supplied message and including a reason for why it
121// failed.
122func AssertStringListContains(t *testing.T, message string, list []string, expected string) {
123 t.Helper()
124 if !InList(expected, list) {
125 t.Errorf("%s: could not find %q within %q", message, expected, list)
126 }
127}
128
129// AssertArrayString checks if the expected and actual values are equal and if they are not then it
130// reports an error prefixed with the supplied message and including a reason for why it failed.
131func AssertArrayString(t *testing.T, message string, expected, actual []string) {
132 t.Helper()
133 if len(actual) != len(expected) {
134 t.Errorf("%s: expected %d (%q), actual (%d) %q", message, len(expected), expected, len(actual), actual)
135 return
136 }
137 for i := range actual {
138 if actual[i] != expected[i] {
139 t.Errorf("%s: expected %d-th, %q (%q), actual %q (%q)",
140 message, i, expected[i], expected, actual[i], actual)
141 return
142 }
143 }
144}
145
146// AssertDeepEquals checks if the expected and actual values are equal using reflect.DeepEqual and
147// if they are not then it reports an error prefixed with the supplied message and including a
148// reason for why it failed.
149func AssertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
150 t.Helper()
151 if !reflect.DeepEqual(actual, expected) {
152 t.Errorf("%s: expected:\n %#v\n got:\n %#v", message, expected, actual)
153 }
154}
155
156// AssertPanic checks that the supplied function panics as expected.
157func AssertPanic(t *testing.T, message string, funcThatShouldPanic func()) {
158 t.Helper()
159 panicked := false
160 func() {
161 defer func() {
162 if x := recover(); x != nil {
163 panicked = true
164 }
165 }()
166 funcThatShouldPanic()
167 }()
168 if !panicked {
169 t.Error(message)
170 }
171}