github.com/nikandfor/assert@v0.0.0-20231112165957-bf2ce0a3555a/assert_test.go (about) 1 package assert_test 2 3 import ( 4 "fmt" 5 "io" 6 "testing" 7 8 "github.com/nikandfor/assert" 9 "tlog.app/go/errors" 10 ) 11 12 type ( 13 wbuf []byte 14 15 TestT struct { 16 testing.T 17 18 failed int 19 b wbuf 20 } 21 ) 22 23 func TestNoError(t *testing.T) { 24 tt := &TestT{} 25 26 assert.NoError(tt, nil) 27 checkOK(t, tt) 28 29 tt.reset() 30 31 assert.NoError(tt, errors.New("test_error")) 32 checkFailed(t, tt, 1) 33 } 34 35 func TestErrorIs(t *testing.T) { 36 tt := &TestT{} 37 38 assert.ErrorIs(tt, io.EOF, io.EOF) 39 checkOK(t, tt) 40 41 tt.reset() 42 43 assert.ErrorIs(tt, errors.Wrap(io.EOF, "wrapped"), io.EOF) 44 checkOK(t, tt) 45 46 tt.reset() 47 48 assert.NoError(tt, errors.New("test_error"), io.EOF) 49 checkFailed(t, tt, 1) 50 } 51 52 func TestEqual(t *testing.T) { 53 tt := &TestT{} 54 55 assert.Equal(tt, "asd", "asd") 56 checkOK(t, tt) 57 58 assert.Equal(tt, "asd", "qwe") 59 checkFailed(t, tt, 1) 60 } 61 62 func checkOK(t *testing.T, tt *TestT) { 63 if tt.failed == 0 && len(tt.b) == 0 { 64 return 65 } 66 67 failTT(t, tt) 68 } 69 70 func checkFailed(t *testing.T, tt *TestT, failed int) { 71 if tt.failed == failed && len(tt.b) != 0 { 72 t.Logf("TEST OUTPUT:\n%s", tt.b) 73 74 return 75 } 76 77 failTT(t, tt) 78 } 79 80 func failTT(t *testing.T, tt *TestT) { 81 t.Errorf("failed: %v\n%s", tt.failed, tt.b) 82 } 83 84 func (tt *TestT) Fail() { 85 tt.failed = 1 86 } 87 88 func (tt *TestT) FailNow() { 89 tt.failed = 2 90 } 91 92 func (tt *TestT) Logf(format string, args ...interface{}) { 93 _, _ = fmt.Fprintf(&tt.b, format, args...) 94 } 95 96 func (tt *TestT) reset() { 97 tt.failed = 0 98 tt.b = tt.b[:0] 99 } 100 101 func (w *wbuf) Write(p []byte) (int, error) { 102 *w = append(*w, p...) 103 104 return len(p), nil 105 }