|
| 1 | +// Package assert provides functions to compare actual and expected test values. |
| 2 | +package assert |
| 3 | + |
| 4 | +import ( |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "runtime/debug" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// IntsAreEqual compares an expected int value and an actual int value for equality. |
| 13 | +func IntsAreEqual(t *testing.T, expected int, actual int) { |
| 14 | + if expected != actual { |
| 15 | + debug.PrintStack() |
| 16 | + t.Fatalf("Expected: %d; Actual: %d", expected, actual) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +// Int32sAreEqual compares an expected int32 value and an actual int32 value for equality. |
| 21 | +func Int32sAreEqual(t *testing.T, expected int32, actual int32) { |
| 22 | + if expected != actual { |
| 23 | + debug.PrintStack() |
| 24 | + t.Fatalf("Expected: %d; Actual: %d", expected, actual) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// Int64sAreEqual compares an expected int64 value and an actual int64 value for equality. |
| 29 | +func Int64sAreEqual(t *testing.T, expected int64, actual int64) { |
| 30 | + if expected != actual { |
| 31 | + debug.PrintStack() |
| 32 | + t.Fatalf("Expected: %d; Actual: %d", expected, actual) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// StringPrefix compares an expected string value and an actual string value for a prefix match. |
| 37 | +func StringPrefix(t *testing.T, expectedPrefix string, actual string) { |
| 38 | + if expectedPrefix == "" { |
| 39 | + t.FailNow() // invalid usage -> strings.HasPrefix will return true for empty string |
| 40 | + } |
| 41 | + |
| 42 | + if !strings.HasPrefix(actual, expectedPrefix) { |
| 43 | + debug.PrintStack() |
| 44 | + t.Fatalf("Actual does not start with expected: Expected: %s; Actual: %s", expectedPrefix, actual) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// StringContains compares an expected string and an actual string value for a substring. |
| 49 | +func StringContains(t *testing.T, expectedContains string, actual string) { |
| 50 | + if !strings.Contains(actual, expectedContains) { |
| 51 | + debug.PrintStack() |
| 52 | + t.Fatalf("Actual does not contain expected:\nExpected: %s\nActual: %s", expectedContains, actual) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// StringNotContains compares an expected string and an actual string value for a missing substring. |
| 57 | +func StringNotContains(t *testing.T, expectedNotContains string, actual string) { |
| 58 | + if strings.Contains(actual, expectedNotContains) { |
| 59 | + debug.PrintStack() |
| 60 | + t.Fatalf("Actual contains expected: Expected: %s; Actual: %s", expectedNotContains, actual) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// StringsAreEqual compares an expected string value and an actual string value for equality. |
| 65 | +func StringsAreEqual(t *testing.T, expected string, actual string) { |
| 66 | + if expected != actual { |
| 67 | + debug.PrintStack() |
| 68 | + |
| 69 | + if len(expected) <= 250 { |
| 70 | + t.Fatalf("\nExpected: %s;\nActual: %s\n", |
| 71 | + expected, |
| 72 | + actual) |
| 73 | + } else { |
| 74 | + t.Fatalf("\nExpected: %s;\nActual: %s\nExpected File: %s\nActual File: %s\nDiff Cmd: gvimdiff '%[3]s' '%[4]s'", |
| 75 | + expected, |
| 76 | + actual, |
| 77 | + saveString(expected, "expected-"), |
| 78 | + saveString(actual, "actual-")) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// EmptyString compares an actual string value and the empty string for equality. |
| 84 | +func EmptyString(t *testing.T, actual string) { |
| 85 | + if actual != "" { |
| 86 | + debug.PrintStack() |
| 87 | + t.Fatalf("Expected empty string: Actual: %s", actual) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// NotNil compares an interface and nil for equality. |
| 92 | +func NotNil(t *testing.T, actual interface{}) { |
| 93 | + if actual == nil { |
| 94 | + debug.PrintStack() |
| 95 | + t.Fatal("Expected non-nil") |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// Nil compares an interface and nil for inequality. |
| 100 | +func Nil(t *testing.T, actual interface{}) { |
| 101 | + if actual != nil { |
| 102 | + debug.PrintStack() |
| 103 | + t.Fatal("Expected nil") |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// NilError compares an error and nil. |
| 108 | +func NilError(t *testing.T, err error) { |
| 109 | + if err != nil { |
| 110 | + debug.PrintStack() |
| 111 | + t.Fatalf("Expected nil error, found: %s", err.Error()) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// False compares a bool value to false. |
| 116 | +func False(t *testing.T, actual bool) { |
| 117 | + if actual { |
| 118 | + debug.PrintStack() |
| 119 | + t.Fatalf("Expected false: Actual: %t", actual) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// True compares a bool value to false. |
| 124 | +func True(t *testing.T, actual bool) { |
| 125 | + if !actual { |
| 126 | + debug.PrintStack() |
| 127 | + t.Fatalf("Expected true: Actual: %t", actual) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func saveString(str string, prefix string) string { |
| 132 | + f, _ := ioutil.TempFile(os.TempDir(), prefix) |
| 133 | + _, _ = f.WriteString(str) |
| 134 | + _ = f.Close() |
| 135 | + return f.Name() |
| 136 | +} |
0 commit comments