Paul Duffin | 0550055 | 2021-03-12 11:29:56 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Paul Duffin | 0550055 | 2021-03-12 11:29:56 +0000 | [diff] [blame] | 18 | "testing" |
| 19 | ) |
| 20 | |
| 21 | // Provides general test support. |
| 22 | type 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. |
| 28 | func (h *TestHelper) AssertBoolEquals(message string, expected bool, actual bool) { |
Paul Duffin | 9dcf253 | 2021-03-12 11:50:43 +0000 | [diff] [blame] | 29 | AssertBoolEquals(h.T, message, expected, actual) |
Paul Duffin | 0550055 | 2021-03-12 11:29:56 +0000 | [diff] [blame] | 30 | } |
| 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. |
| 34 | func (h *TestHelper) AssertStringEquals(message string, expected string, actual string) { |
Paul Duffin | 9dcf253 | 2021-03-12 11:50:43 +0000 | [diff] [blame] | 35 | AssertStringEquals(h.T, message, expected, actual) |
Paul Duffin | 0550055 | 2021-03-12 11:29:56 +0000 | [diff] [blame] | 36 | } |
| 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. |
| 41 | func (h *TestHelper) AssertErrorMessageEquals(message string, expected string, actual error) { |
Paul Duffin | 9dcf253 | 2021-03-12 11:50:43 +0000 | [diff] [blame] | 42 | AssertErrorMessageEquals(h.T, message, expected, actual) |
Paul Duffin | 0550055 | 2021-03-12 11:29:56 +0000 | [diff] [blame] | 43 | } |
| 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. |
| 48 | func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) { |
Paul Duffin | 9dcf253 | 2021-03-12 11:50:43 +0000 | [diff] [blame] | 49 | AssertTrimmedStringEquals(h.T, message, expected, actual) |
Paul Duffin | 0550055 | 2021-03-12 11:29:56 +0000 | [diff] [blame] | 50 | } |
| 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. |
| 55 | func (h *TestHelper) AssertStringDoesContain(message string, s string, expectedSubstring string) { |
Paul Duffin | 9dcf253 | 2021-03-12 11:50:43 +0000 | [diff] [blame] | 56 | AssertStringDoesContain(h.T, message, s, expectedSubstring) |
Paul Duffin | 0550055 | 2021-03-12 11:29:56 +0000 | [diff] [blame] | 57 | } |
| 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. |
| 61 | func (h *TestHelper) AssertStringDoesNotContain(message string, s string, unexpectedSubstring string) { |
Paul Duffin | 9dcf253 | 2021-03-12 11:50:43 +0000 | [diff] [blame] | 62 | AssertStringDoesNotContain(h.T, message, s, unexpectedSubstring) |
Paul Duffin | 0550055 | 2021-03-12 11:29:56 +0000 | [diff] [blame] | 63 | } |
| 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. |
| 68 | func (h *TestHelper) AssertStringListContains(message string, list []string, expected string) { |
Paul Duffin | 9dcf253 | 2021-03-12 11:50:43 +0000 | [diff] [blame] | 69 | AssertStringListContains(h.T, message, list, expected) |
Paul Duffin | 0550055 | 2021-03-12 11:29:56 +0000 | [diff] [blame] | 70 | } |
| 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. |
| 74 | func (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. |
| 92 | func (h *TestHelper) AssertDeepEquals(message string, expected interface{}, actual interface{}) { |
Paul Duffin | 9dcf253 | 2021-03-12 11:50:43 +0000 | [diff] [blame] | 93 | AssertDeepEquals(h.T, message, expected, actual) |
Paul Duffin | 0550055 | 2021-03-12 11:29:56 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // AssertPanic checks that the supplied function panics as expected. |
| 97 | func (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 | } |