blob: 7070bda112448df87fefb11ab73bc3030fc4c06c [file] [log] [blame]
Paul Duffin05500552021-03-12 11:29:56 +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// Provides general test support.
24type TestHelper struct {
25 *testing.T
26}
27
28// AssertBoolEquals checks if the expected and actual values are equal and if they are not then it
29// reports an error prefixed with the supplied message and including a reason for why it failed.
30func (h *TestHelper) AssertBoolEquals(message string, expected bool, actual bool) {
31 h.Helper()
32 if actual != expected {
33 h.Errorf("%s: expected %t, actual %t", message, expected, actual)
34 }
35}
36
37// AssertStringEquals checks if the expected and actual values are equal and if they are not then
38// it reports an error prefixed with the supplied message and including a reason for why it failed.
39func (h *TestHelper) AssertStringEquals(message string, expected string, actual string) {
40 h.Helper()
41 if actual != expected {
42 h.Errorf("%s: expected %s, actual %s", message, expected, actual)
43 }
44}
45
46// AssertErrorMessageEquals checks if the error is not nil and has the expected message. If it does
47// not then this reports an error prefixed with the supplied message and including a reason for why
48// it failed.
49func (h *TestHelper) AssertErrorMessageEquals(message string, expected string, actual error) {
50 h.Helper()
51 if actual == nil {
52 h.Errorf("Expected error but was nil")
53 } else if actual.Error() != expected {
54 h.Errorf("%s: expected %s, actual %s", message, expected, actual.Error())
55 }
56}
57
58// AssertTrimmedStringEquals checks if the expected and actual values are the same after trimming
59// leading and trailing spaces from them both. If they are not then it reports an error prefixed
60// with the supplied message and including a reason for why it failed.
61func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) {
62 h.Helper()
63 h.AssertStringEquals(message, strings.TrimSpace(expected), strings.TrimSpace(actual))
64}
65
66// AssertStringDoesContain checks if the string contains the expected substring. If it does not
67// then it reports an error prefixed with the supplied message and including a reason for why it
68// failed.
69func (h *TestHelper) AssertStringDoesContain(message string, s string, expectedSubstring string) {
70 h.Helper()
71 if !strings.Contains(s, expectedSubstring) {
72 h.Errorf("%s: could not find %q within %q", message, expectedSubstring, s)
73 }
74}
75
76// AssertStringDoesNotContain checks if the string contains the expected substring. If it does then
77// it reports an error prefixed with the supplied message and including a reason for why it failed.
78func (h *TestHelper) AssertStringDoesNotContain(message string, s string, unexpectedSubstring string) {
79 h.Helper()
80 if strings.Contains(s, unexpectedSubstring) {
81 h.Errorf("%s: unexpectedly found %q within %q", message, unexpectedSubstring, s)
82 }
83}
84
85// AssertStringListContains checks if the list of strings contains the expected string. If it does
86// not then it reports an error prefixed with the supplied message and including a reason for why it
87// failed.
88func (h *TestHelper) AssertStringListContains(message string, list []string, expected string) {
89 h.Helper()
90 if !InList(expected, list) {
91 h.Errorf("%s: could not find %q within %q", message, expected, list)
92 }
93}
94
95// AssertArrayString checks if the expected and actual values are equal and if they are not then it
96// reports an error prefixed with the supplied message and including a reason for why it failed.
97func (h *TestHelper) AssertArrayString(message string, expected, actual []string) {
98 h.Helper()
99 if len(actual) != len(expected) {
100 h.Errorf("%s: expected %d (%q), actual (%d) %q", message, len(expected), expected, len(actual), actual)
101 return
102 }
103 for i := range actual {
104 if actual[i] != expected[i] {
105 h.Errorf("%s: expected %d-th, %q (%q), actual %q (%q)",
106 message, i, expected[i], expected, actual[i], actual)
107 return
108 }
109 }
110}
111
112// AssertDeepEquals checks if the expected and actual values are equal using reflect.DeepEqual and
113// if they are not then it reports an error prefixed with the supplied message and including a
114// reason for why it failed.
115func (h *TestHelper) AssertDeepEquals(message string, expected interface{}, actual interface{}) {
116 h.Helper()
117 if !reflect.DeepEqual(actual, expected) {
118 h.Errorf("%s: expected:\n %#v\n got:\n %#v", message, expected, actual)
119 }
120}
121
122// AssertPanic checks that the supplied function panics as expected.
123func (h *TestHelper) AssertPanic(message string, funcThatShouldPanic func()) {
124 h.Helper()
125 panicked := false
126 func() {
127 defer func() {
128 if x := recover(); x != nil {
129 panicked = true
130 }
131 }()
132 funcThatShouldPanic()
133 }()
134 if !panicked {
135 h.Error(message)
136 }
137}