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