Jack Neus | b49819f | 2021-03-01 23:37:34 +0000 | [diff] [blame^] | 1 | // Copyright 2021 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | package assert |
| 5 | |
| 6 | import ( |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // Assert checks that the given bool is true. |
| 12 | func Assert(t *testing.T, b bool) { |
| 13 | t.Helper() |
| 14 | if !b { |
| 15 | t.Fatal("assert failed: bool is false") |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | // NilError checks that the given error is nil. |
| 20 | func NilError(t *testing.T, err error) { |
| 21 | t.Helper() |
| 22 | if err != nil { |
| 23 | t.Fatalf("assert failed: non-nil error %v", err) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // ErrorContains checks that the given string exists in an error. |
| 28 | func ErrorContains(t *testing.T, err error, s string) { |
| 29 | if !strings.Contains(err.Error(), s) { |
| 30 | t.Fatalf("assert failed: %v does not contain \"%s\"", err, s) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // IntsEqual checks that the two ints are equal. |
| 35 | func IntsEqual(t *testing.T, a, b int) { |
| 36 | t.Helper() |
| 37 | if a != b { |
| 38 | t.Fatalf("assert failed: %d != %d", a, b) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // StringsEqual checks that the two strings are equal. |
| 43 | func StringsEqual(t *testing.T, a, b string) { |
| 44 | t.Helper() |
| 45 | if a != b { |
| 46 | t.Fatalf("assert failed: \"%s\" != \"%s\"", a, b) |
| 47 | } |
| 48 | } |