A lightweight test helper package
0

Configure Feed

Select the types of activity you want to include in your feed.

Remove context arg from Ok and Err (#29)

authored by

Tom Fleet and committed by
GitHub
(Jul 27, 2024, 7:19 PM +0100) c2a6944e 3fdac1e1

+6 -23
+6 -19
test.go
··· 4 4 5 5 import ( 6 6 "bytes" 7 - "fmt" 8 7 "io" 9 8 "math" 10 9 "os" ··· 76 75 } 77 76 } 78 77 79 - // Ok fails if err != nil, optionally adding context to the output. 78 + // Ok fails if err != nil. 80 79 // 81 80 // err := doSomething() 82 81 // test.Ok(t, err, "Doing something") 83 - func Ok(t testing.TB, err error, context ...string) { 82 + func Ok(t testing.TB, err error) { 84 83 t.Helper() 85 - var msg string 86 - if len(context) == 0 { 87 - msg = fmt.Sprintf("\nGot error:\t%v\nWanted:\tnil\n", err) 88 - } else { 89 - msg = fmt.Sprintf("\nGot error:\t%v\nWanted:\tnil\nContext:\t%s\n", err, context[0]) 90 - } 91 84 if err != nil { 92 - t.Fatalf(msg, err) 85 + t.Fatalf("\nGot error:\t%v\nWanted:\tnil\n", err) 93 86 } 94 87 } 95 88 96 89 // Err fails if err == nil. 97 90 // 98 91 // err := shouldReturnErr() 99 - // test.Err(t, err, "shouldReturnErr") 100 - func Err(t testing.TB, err error, context ...string) { 92 + // test.Err(t, err) 93 + func Err(t testing.TB, err error) { 101 94 t.Helper() 102 - var msg string 103 - if len(context) == 0 { 104 - msg = fmt.Sprintf("Error was not nil:\t%v\n", err) 105 - } else { 106 - msg = fmt.Sprintf("Error was not nil:\t%v\nContext:\t%s", err, context[0]) 107 - } 108 95 if err == nil { 109 - t.Fatalf(msg, err) 96 + t.Fatalf("Error was not nil:\t%v\n", err) 110 97 } 111 98 } 112 99
-4
test_test.go
··· 63 63 "NotEqual bool": func(tb testing.TB) { test.NotEqual(tb, true, false) }, 64 64 "NotEqual float": func(tb testing.TB) { test.NotEqual(tb, 3.14, 8.67) }, 65 65 "Ok nil": func(tb testing.TB) { test.Ok(tb, nil) }, 66 - "Ok with context": func(tb testing.TB) { test.Ok(tb, nil, "Something") }, 67 66 "Err": func(tb testing.TB) { test.Err(tb, errors.New("uh oh")) }, 68 - "Err with context": func(tb testing.TB) { test.Err(tb, errors.New("uh oh"), "Something") }, 69 67 "True": func(tb testing.TB) { test.True(tb, true) }, 70 68 "False": func(tb testing.TB) { test.False(tb, false) }, 71 69 "Diff int": func(tb testing.TB) { test.Diff(tb, 42, 42) }, ··· 148 146 "NotEqual bool": func(tb testing.TB) { test.NotEqual(tb, true, true) }, 149 147 "NotEqual float": func(tb testing.TB) { test.NotEqual(tb, 3.14, 3.14) }, 150 148 "Ok": func(tb testing.TB) { test.Ok(tb, errors.New("uh oh")) }, 151 - "Ok with context": func(tb testing.TB) { test.Ok(tb, errors.New("uh oh"), "Something") }, 152 149 "Err": func(tb testing.TB) { test.Err(tb, nilErr()) }, 153 - "Err with context": func(tb testing.TB) { test.Err(tb, nilErr(), "Something") }, 154 150 "True": func(tb testing.TB) { test.True(tb, false) }, 155 151 "False": func(tb testing.TB) { test.False(tb, true) }, 156 152 "Diff string": func(tb testing.TB) { test.Diff(tb, "hello", "there") },