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