blob: 91df9135055204d3e5e6f908600820237e67fba8 [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 (
Paul Duffin05500552021-03-12 11:29:56 +000018 "testing"
19)
20
21// Provides general test support.
22type TestHelper struct {
23 *testing.T
24}
25
26// AssertBoolEquals checks if the expected and actual values are equal and if they are not then it
27// reports an error prefixed with the supplied message and including a reason for why it failed.
28func (h *TestHelper) AssertBoolEquals(message string, expected bool, actual bool) {
Paul Duffin9dcf2532021-03-12 11:50:43 +000029 AssertBoolEquals(h.T, message, expected, actual)
Paul Duffin05500552021-03-12 11:29:56 +000030}
31
32// AssertStringEquals checks if the expected and actual values are equal and if they are not then
33// it reports an error prefixed with the supplied message and including a reason for why it failed.
34func (h *TestHelper) AssertStringEquals(message string, expected string, actual string) {
Paul Duffin9dcf2532021-03-12 11:50:43 +000035 AssertStringEquals(h.T, message, expected, actual)
Paul Duffin05500552021-03-12 11:29:56 +000036}
37
38// AssertErrorMessageEquals checks if the error is not nil and has the expected message. If it does
39// not then this reports an error prefixed with the supplied message and including a reason for why
40// it failed.
41func (h *TestHelper) AssertErrorMessageEquals(message string, expected string, actual error) {
Paul Duffin9dcf2532021-03-12 11:50:43 +000042 AssertErrorMessageEquals(h.T, message, expected, actual)
Paul Duffin05500552021-03-12 11:29:56 +000043}
44
45// AssertTrimmedStringEquals checks if the expected and actual values are the same after trimming
46// leading and trailing spaces from them both. If they are not then it reports an error prefixed
47// with the supplied message and including a reason for why it failed.
48func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) {
Paul Duffin9dcf2532021-03-12 11:50:43 +000049 AssertTrimmedStringEquals(h.T, message, expected, actual)
Paul Duffin05500552021-03-12 11:29:56 +000050}
51
52// AssertStringDoesContain checks if the string contains the expected substring. If it does not
53// then it reports an error prefixed with the supplied message and including a reason for why it
54// failed.
55func (h *TestHelper) AssertStringDoesContain(message string, s string, expectedSubstring string) {
Paul Duffin9dcf2532021-03-12 11:50:43 +000056 AssertStringDoesContain(h.T, message, s, expectedSubstring)
Paul Duffin05500552021-03-12 11:29:56 +000057}
58
59// AssertStringDoesNotContain checks if the string contains the expected substring. If it does then
60// it reports an error prefixed with the supplied message and including a reason for why it failed.
61func (h *TestHelper) AssertStringDoesNotContain(message string, s string, unexpectedSubstring string) {
Paul Duffin9dcf2532021-03-12 11:50:43 +000062 AssertStringDoesNotContain(h.T, message, s, unexpectedSubstring)
Paul Duffin05500552021-03-12 11:29:56 +000063}
64
65// AssertStringListContains checks if the list of strings contains the expected string. If it does
66// not then it reports an error prefixed with the supplied message and including a reason for why it
67// failed.
68func (h *TestHelper) AssertStringListContains(message string, list []string, expected string) {
Paul Duffin9dcf2532021-03-12 11:50:43 +000069 AssertStringListContains(h.T, message, list, expected)
Paul Duffin05500552021-03-12 11:29:56 +000070}
71
72// AssertArrayString checks if the expected and actual values are equal and if they are not then it
73// reports an error prefixed with the supplied message and including a reason for why it failed.
74func (h *TestHelper) AssertArrayString(message string, expected, actual []string) {
75 h.Helper()
76 if len(actual) != len(expected) {
77 h.Errorf("%s: expected %d (%q), actual (%d) %q", message, len(expected), expected, len(actual), actual)
78 return
79 }
80 for i := range actual {
81 if actual[i] != expected[i] {
82 h.Errorf("%s: expected %d-th, %q (%q), actual %q (%q)",
83 message, i, expected[i], expected, actual[i], actual)
84 return
85 }
86 }
87}
88
89// AssertDeepEquals checks if the expected and actual values are equal using reflect.DeepEqual and
90// if they are not then it reports an error prefixed with the supplied message and including a
91// reason for why it failed.
92func (h *TestHelper) AssertDeepEquals(message string, expected interface{}, actual interface{}) {
Paul Duffin9dcf2532021-03-12 11:50:43 +000093 AssertDeepEquals(h.T, message, expected, actual)
Paul Duffin05500552021-03-12 11:29:56 +000094}
95
96// AssertPanic checks that the supplied function panics as expected.
97func (h *TestHelper) AssertPanic(message string, funcThatShouldPanic func()) {
98 h.Helper()
99 panicked := false
100 func() {
101 defer func() {
102 if x := recover(); x != nil {
103 panicked = true
104 }
105 }()
106 funcThatShouldPanic()
107 }()
108 if !panicked {
109 h.Error(message)
110 }
111}