From 06ba65af4c40e8c3b93d20e443fbe70456884cd2 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Thu, 15 Feb 2024 13:56:07 +0530 Subject: [PATCH 01/15] chore: remove loopclosure captures from tests Signed-off-by: Manik Rana --- go/streamlog/streamlog_test.go | 2 -- go/trace/trace_test.go | 1 - 2 files changed, 3 deletions(-) diff --git a/go/streamlog/streamlog_test.go b/go/streamlog/streamlog_test.go index e653de74ea8..ab01dd4ccb2 100644 --- a/go/streamlog/streamlog_test.go +++ b/go/streamlog/streamlog_test.go @@ -315,7 +315,6 @@ func TestShouldEmitLog(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.sql, func(t *testing.T) { SetQueryLogFilterTag(tt.qLogFilterTag) SetQueryLogRowThreshold(tt.qLogRowThreshold) @@ -360,7 +359,6 @@ func TestGetFormatter(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.name, func(t *testing.T) { var buffer bytes.Buffer logFormatterFunc := GetFormatter[string](tt.logger) diff --git a/go/trace/trace_test.go b/go/trace/trace_test.go index 6b5bdf491bb..7f1f6d8c528 100644 --- a/go/trace/trace_test.go +++ b/go/trace/trace_test.go @@ -96,7 +96,6 @@ func TestNewFromString(t *testing.T) { }, } for _, tt := range tests { - tt := tt t.Run(tt.label, func(t *testing.T) { span, ctx, err := NewFromString(context.Background(), tt.parent, tt.label) if tt.expectedErr == "" { From 3b4b428c9bb471d7db5037a3c070cced2380625e Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Thu, 15 Feb 2024 14:23:24 +0530 Subject: [PATCH 02/15] chore: convert errorf to assert Signed-off-by: Manik Rana --- go/cache/lru_cache_test.go | 35 ++++------- go/cache/theine/singleflight_test.go | 25 ++++---- go/event/syslogger/syslogger_test.go | 93 +++++++++++----------------- go/flagutil/flagutil_test.go | 36 +++++------ go/history/history_test.go | 12 ++-- 5 files changed, 81 insertions(+), 120 deletions(-) diff --git a/go/cache/lru_cache_test.go b/go/cache/lru_cache_test.go index e0dbfcda117..8e18762e681 100644 --- a/go/cache/lru_cache_test.go +++ b/go/cache/lru_cache_test.go @@ -18,6 +18,8 @@ package cache import ( "testing" + + "github.com/stretchr/testify/assert" ) type CacheValue struct { @@ -27,24 +29,13 @@ type CacheValue struct { func TestInitialState(t *testing.T) { cache := NewLRUCache[*CacheValue](5) l, sz, c, e, h, m := cache.Len(), cache.UsedCapacity(), cache.MaxCapacity(), cache.Evictions(), cache.Hits(), cache.Misses() - if l != 0 { - t.Errorf("length = %v, want 0", l) - } - if sz != 0 { - t.Errorf("size = %v, want 0", sz) - } - if c != 5 { - t.Errorf("capacity = %v, want 5", c) - } - if e != 0 { - t.Errorf("evictions = %v, want 0", c) - } - if h != 0 { - t.Errorf("hits = %v, want 0", c) - } - if m != 0 { - t.Errorf("misses = %v, want 0", c) - } + assert.Equal(t, 0, l, "length = %v, want 0", l) + assert.Equal(t, int64(0), sz, "size = %v, want 0", sz) + assert.Equal(t, int64(5), c, "capacity = %v, want 5", c) + assert.Equal(t, int64(0), e, "evictions = %v, want 0", c) + assert.Equal(t, int64(0), h, "hits = %v, want 0", c) + assert.Equal(t, int64(0), m, "misses = %v, want 0", c) + } func TestSetInsertsValue(t *testing.T) { @@ -137,12 +128,8 @@ func TestCapacityIsObeyed(t *testing.T) { // Insert one more; something should be evicted to make room. cache.Set("key4", value) sz, evictions := cache.UsedCapacity(), cache.Evictions() - if sz != size { - t.Errorf("post-evict cache.UsedCapacity() = %v, expected %v", sz, size) - } - if evictions != 1 { - t.Errorf("post-evict cache.Evictions() = %v, expected 1", evictions) - } + assert.Equal(t, size, sz, "post-evict cache.UsedCapacity() = %v, expected %v", sz, size) + assert.Equal(t, int64(1), evictions, "post-evict cache.Evictions() = %v, expected 1", evictions) // Check various other stats if l := cache.Len(); int64(l) != size { diff --git a/go/cache/theine/singleflight_test.go b/go/cache/theine/singleflight_test.go index 60b28e69b4e..13b77bd59ad 100644 --- a/go/cache/theine/singleflight_test.go +++ b/go/cache/theine/singleflight_test.go @@ -32,6 +32,8 @@ import ( "sync/atomic" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestDo(t *testing.T) { @@ -42,9 +44,8 @@ func TestDo(t *testing.T) { if got, want := fmt.Sprintf("%v (%T)", v, v), "bar (string)"; got != want { t.Errorf("Do = %v; want %v", got, want) } - if err != nil { - t.Errorf("Do error = %v", err) - } + assert.NoError(t, err, "Do error = %v", err) + } func TestDoErr(t *testing.T) { @@ -53,12 +54,9 @@ func TestDoErr(t *testing.T) { v, err, _ := g.Do("key", func() (string, error) { return "", someErr }) - if err != someErr { - t.Errorf("Do error = %v; want someErr %v", err, someErr) - } - if v != "" { - t.Errorf("unexpected non-nil value %#v", v) - } + assert.Equal(t, someErr, err, "Do error = %v; want someErr %v", err, someErr) + assert.Equal(t, "", v, "unexpected non-nil value %#v", v) + } func TestDoDupSuppress(t *testing.T) { @@ -72,9 +70,9 @@ func TestDoDupSuppress(t *testing.T) { wg1.Done() } v := <-c - c <- v // pump; make available for any future calls + c <- v // pump; make available for any future calls - time.Sleep(10 * time.Millisecond) // let more goroutines enter Do + time.Sleep(10 * time.Millisecond) // let more goroutines enter Do return v, nil } @@ -159,9 +157,8 @@ func TestGoexitDo(t *testing.T) { go func() { var err error defer func() { - if err != nil { - t.Errorf("Error should be nil, but got: %v", err) - } + assert.NoError(t, err, "Error should be nil, but got: %v", err) + if atomic.AddInt32(&waited, -1) == 0 { close(done) } diff --git a/go/event/syslogger/syslogger_test.go b/go/event/syslogger/syslogger_test.go index 28925d2ac52..30a4fe234fe 100644 --- a/go/event/syslogger/syslogger_test.go +++ b/go/event/syslogger/syslogger_test.go @@ -25,12 +25,14 @@ import ( "testing" "vitess.io/vitess/go/event" + + "github.com/stretchr/testify/assert" ) type TestEvent struct { - triggered bool - priority syslog.Priority - message string + triggered bool + priority syslog.Priority + message string } func (ev *TestEvent) Syslog() (syslog.Priority, string) { @@ -38,12 +40,12 @@ func (ev *TestEvent) Syslog() (syslog.Priority, string) { return ev.priority, ev.message } -var _ Syslogger = (*TestEvent)(nil) // compile-time interface check +var _ Syslogger = (*TestEvent)(nil) // compile-time interface check type fakeWriter struct { - priority syslog.Priority - message string - err error // if non-nil, force an error to be returned + priority syslog.Priority + message string + err error // if non-nil, force an error to be returned } func (fw *fakeWriter) write(pri syslog.Priority, msg string) error { @@ -55,14 +57,14 @@ func (fw *fakeWriter) write(pri syslog.Priority, msg string) error { fw.message = msg return nil } -func (fw *fakeWriter) Alert(msg string) error { return fw.write(syslog.LOG_ALERT, msg) } -func (fw *fakeWriter) Crit(msg string) error { return fw.write(syslog.LOG_CRIT, msg) } -func (fw *fakeWriter) Debug(msg string) error { return fw.write(syslog.LOG_DEBUG, msg) } -func (fw *fakeWriter) Emerg(msg string) error { return fw.write(syslog.LOG_EMERG, msg) } -func (fw *fakeWriter) Err(msg string) error { return fw.write(syslog.LOG_ERR, msg) } -func (fw *fakeWriter) Info(msg string) error { return fw.write(syslog.LOG_INFO, msg) } -func (fw *fakeWriter) Notice(msg string) error { return fw.write(syslog.LOG_NOTICE, msg) } -func (fw *fakeWriter) Warning(msg string) error { return fw.write(syslog.LOG_WARNING, msg) } +func (fw *fakeWriter) Alert(msg string) error { return fw.write(syslog.LOG_ALERT, msg) } +func (fw *fakeWriter) Crit(msg string) error { return fw.write(syslog.LOG_CRIT, msg) } +func (fw *fakeWriter) Debug(msg string) error { return fw.write(syslog.LOG_DEBUG, msg) } +func (fw *fakeWriter) Emerg(msg string) error { return fw.write(syslog.LOG_EMERG, msg) } +func (fw *fakeWriter) Err(msg string) error { return fw.write(syslog.LOG_ERR, msg) } +func (fw *fakeWriter) Info(msg string) error { return fw.write(syslog.LOG_INFO, msg) } +func (fw *fakeWriter) Notice(msg string) error { return fw.write(syslog.LOG_NOTICE, msg) } +func (fw *fakeWriter) Warning(msg string) error { return fw.write(syslog.LOG_WARNING, msg) } // TestSyslog checks that our callback works. func TestSyslog(t *testing.T) { @@ -70,10 +72,8 @@ func TestSyslog(t *testing.T) { ev := new(TestEvent) event.Dispatch(ev) + assert.True(t, ev.triggered, "Syslog() was not called on event that implements Syslogger") - if !ev.triggered { - t.Errorf("Syslog() was not called on event that implements Syslogger") - } } // TestBadWriter verifies we are still triggering (to normal logs) if @@ -87,55 +87,40 @@ func TestBadWriter(t *testing.T) { wantLevel := "ERROR" ev := &TestEvent{priority: syslog.LOG_ALERT, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().msg, wantMsg) { - t.Errorf("error log msg [%s], want msg [%s]", tl.getLog().msg, wantMsg) - } - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().msg, wantMsg), "error log msg [%s], want msg [%s]", tl.getLog().msg, wantMsg) + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + ev = &TestEvent{priority: syslog.LOG_CRIT, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + ev = &TestEvent{priority: syslog.LOG_ERR, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + ev = &TestEvent{priority: syslog.LOG_EMERG, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) wantLevel = "WARNING" ev = &TestEvent{priority: syslog.LOG_WARNING, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) wantLevel = "INFO" ev = &TestEvent{priority: syslog.LOG_INFO, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + ev = &TestEvent{priority: syslog.LOG_NOTICE, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + ev = &TestEvent{priority: syslog.LOG_DEBUG, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + assert.True(t, ev.triggered, "passed nil writer to client") - if !ev.triggered { - t.Errorf("passed nil writer to client") - } } // TestWriteError checks that we don't panic on a write error. @@ -150,10 +135,8 @@ func TestInvalidSeverity(t *testing.T) { writer = fw event.Dispatch(&TestEvent{priority: syslog.Priority(123), message: "log me"}) + assert.NotEqual(t, "log me", fw.message, "message was logged despite invalid severity") - if fw.message == "log me" { - t.Errorf("message was logged despite invalid severity") - } } func testSeverity(sev syslog.Priority, t *testing.T) { @@ -161,13 +144,9 @@ func testSeverity(sev syslog.Priority, t *testing.T) { writer = fw event.Dispatch(&TestEvent{priority: sev, message: "log me"}) + assert.Equal(t, sev, fw.priority, "wrong priority: got %v, want %v", fw.priority, sev) + assert.Equal(t, "log me", fw.message, `wrong message: got "%v", want "%v"`, fw.message, "log me") - if fw.priority != sev { - t.Errorf("wrong priority: got %v, want %v", fw.priority, sev) - } - if fw.message != "log me" { - t.Errorf(`wrong message: got "%v", want "%v"`, fw.message, "log me") - } } func TestEmerg(t *testing.T) { diff --git a/go/flagutil/flagutil_test.go b/go/flagutil/flagutil_test.go index dda17bb13d7..34a3d42f8a5 100644 --- a/go/flagutil/flagutil_test.go +++ b/go/flagutil/flagutil_test.go @@ -22,28 +22,26 @@ import ( "github.com/spf13/pflag" "github.com/stretchr/testify/require" + "github.com/stretchr/testify/assert" ) func TestStringList(t *testing.T) { p := StringListValue([]string{}) var _ pflag.Value = &p wanted := map[string]string{ - "0ala,ma,kota": "0ala.ma.kota", - `1ala\,ma,kota`: "1ala,ma.kota", - `2ala\\,ma,kota`: `2ala\.ma.kota`, - "3ala,": "3ala.", + "0ala,ma,kota": "0ala.ma.kota", + `1ala\,ma,kota`: "1ala,ma.kota", + `2ala\\,ma,kota`: `2ala\.ma.kota`, + "3ala,": "3ala.", } for in, out := range wanted { if err := p.Set(in); err != nil { t.Errorf("v.Set(%v): %v", in, err) continue } - if strings.Join(p, ".") != out { - t.Errorf("want %#v, got %#v", strings.Split(out, "."), p) - } - if p.String() != in { - t.Errorf("v.String(): want %#v, got %#v", in, p.String()) - } + assert.Equal(t, out, strings.Join(p, "."), "want %#v, got %#v", strings.Split(out, "."), p) + assert.Equal(t, in, p.String(), "v.String(): want %#v, got %#v", in, p.String()) + } } @@ -60,9 +58,9 @@ func TestEmptyStringList(t *testing.T) { } type pair struct { - in string - out map[string]string - err error + in string + out map[string]string + err error } func TestStringMap(t *testing.T) { @@ -70,16 +68,16 @@ func TestStringMap(t *testing.T) { var _ pflag.Value = &v wanted := []pair{ { - in: "tag1:value1,tag2:value2", - out: map[string]string{"tag1": "value1", "tag2": "value2"}, + in: "tag1:value1,tag2:value2", + out: map[string]string{"tag1": "value1", "tag2": "value2"}, }, { - in: `tag1:1:value1\,,tag2:value2`, - out: map[string]string{"tag1": "1:value1,", "tag2": "value2"}, + in: `tag1:1:value1\,,tag2:value2`, + out: map[string]string{"tag1": "1:value1,", "tag2": "value2"}, }, { - in: `tag1:1:value1\,,tag2`, - err: errInvalidKeyValuePair, + in: `tag1:1:value1\,,tag2`, + err: errInvalidKeyValuePair, }, } for _, want := range wanted { diff --git a/go/history/history_test.go b/go/history/history_test.go index 3e817b34bb2..d82138569a1 100644 --- a/go/history/history_test.go +++ b/go/history/history_test.go @@ -18,6 +18,8 @@ package history import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestHistory(t *testing.T) { @@ -33,9 +35,8 @@ func TestHistory(t *testing.T) { t.Errorf("len(records): want %v, got %v. records: %+v", want, got, q) } for i, record := range records { - if record != want[i] { - t.Errorf("record doesn't match: want %v, got %v", want[i], record) - } + assert.Equal(t, want[i], record, "record doesn't match: want %v, got %v", want[i], record) + } for ; i < 6; i++ { @@ -48,9 +49,8 @@ func TestHistory(t *testing.T) { t.Errorf("len(records): want %v, got %v. records: %+v", want, got, q) } for i, record := range records { - if record != want[i] { - t.Errorf("record doesn't match: want %v, got %v", want[i], record) - } + assert.Equal(t, want[i], record, "record doesn't match: want %v, got %v", want[i], record) + } } From 56e4628b72fd77be3524efe9e6e527c5b966c48c Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Thu, 15 Feb 2024 13:56:07 +0530 Subject: [PATCH 03/15] chore: remove loopclosure captures from tests Signed-off-by: Manik Rana --- go/streamlog/streamlog_test.go | 2 -- go/trace/trace_test.go | 1 - 2 files changed, 3 deletions(-) diff --git a/go/streamlog/streamlog_test.go b/go/streamlog/streamlog_test.go index e653de74ea8..ab01dd4ccb2 100644 --- a/go/streamlog/streamlog_test.go +++ b/go/streamlog/streamlog_test.go @@ -315,7 +315,6 @@ func TestShouldEmitLog(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.sql, func(t *testing.T) { SetQueryLogFilterTag(tt.qLogFilterTag) SetQueryLogRowThreshold(tt.qLogRowThreshold) @@ -360,7 +359,6 @@ func TestGetFormatter(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.name, func(t *testing.T) { var buffer bytes.Buffer logFormatterFunc := GetFormatter[string](tt.logger) diff --git a/go/trace/trace_test.go b/go/trace/trace_test.go index 6b5bdf491bb..7f1f6d8c528 100644 --- a/go/trace/trace_test.go +++ b/go/trace/trace_test.go @@ -96,7 +96,6 @@ func TestNewFromString(t *testing.T) { }, } for _, tt := range tests { - tt := tt t.Run(tt.label, func(t *testing.T) { span, ctx, err := NewFromString(context.Background(), tt.parent, tt.label) if tt.expectedErr == "" { From 790243faf0839659d1fec956d92065c575a2771a Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Thu, 15 Feb 2024 14:23:24 +0530 Subject: [PATCH 04/15] chore: convert errorf to assert Signed-off-by: Manik Rana --- go/cache/lru_cache_test.go | 35 ++++------- go/cache/theine/singleflight_test.go | 25 ++++---- go/event/syslogger/syslogger_test.go | 93 +++++++++++----------------- go/flagutil/flagutil_test.go | 36 +++++------ go/history/history_test.go | 12 ++-- 5 files changed, 81 insertions(+), 120 deletions(-) diff --git a/go/cache/lru_cache_test.go b/go/cache/lru_cache_test.go index e0dbfcda117..8e18762e681 100644 --- a/go/cache/lru_cache_test.go +++ b/go/cache/lru_cache_test.go @@ -18,6 +18,8 @@ package cache import ( "testing" + + "github.com/stretchr/testify/assert" ) type CacheValue struct { @@ -27,24 +29,13 @@ type CacheValue struct { func TestInitialState(t *testing.T) { cache := NewLRUCache[*CacheValue](5) l, sz, c, e, h, m := cache.Len(), cache.UsedCapacity(), cache.MaxCapacity(), cache.Evictions(), cache.Hits(), cache.Misses() - if l != 0 { - t.Errorf("length = %v, want 0", l) - } - if sz != 0 { - t.Errorf("size = %v, want 0", sz) - } - if c != 5 { - t.Errorf("capacity = %v, want 5", c) - } - if e != 0 { - t.Errorf("evictions = %v, want 0", c) - } - if h != 0 { - t.Errorf("hits = %v, want 0", c) - } - if m != 0 { - t.Errorf("misses = %v, want 0", c) - } + assert.Equal(t, 0, l, "length = %v, want 0", l) + assert.Equal(t, int64(0), sz, "size = %v, want 0", sz) + assert.Equal(t, int64(5), c, "capacity = %v, want 5", c) + assert.Equal(t, int64(0), e, "evictions = %v, want 0", c) + assert.Equal(t, int64(0), h, "hits = %v, want 0", c) + assert.Equal(t, int64(0), m, "misses = %v, want 0", c) + } func TestSetInsertsValue(t *testing.T) { @@ -137,12 +128,8 @@ func TestCapacityIsObeyed(t *testing.T) { // Insert one more; something should be evicted to make room. cache.Set("key4", value) sz, evictions := cache.UsedCapacity(), cache.Evictions() - if sz != size { - t.Errorf("post-evict cache.UsedCapacity() = %v, expected %v", sz, size) - } - if evictions != 1 { - t.Errorf("post-evict cache.Evictions() = %v, expected 1", evictions) - } + assert.Equal(t, size, sz, "post-evict cache.UsedCapacity() = %v, expected %v", sz, size) + assert.Equal(t, int64(1), evictions, "post-evict cache.Evictions() = %v, expected 1", evictions) // Check various other stats if l := cache.Len(); int64(l) != size { diff --git a/go/cache/theine/singleflight_test.go b/go/cache/theine/singleflight_test.go index 60b28e69b4e..13b77bd59ad 100644 --- a/go/cache/theine/singleflight_test.go +++ b/go/cache/theine/singleflight_test.go @@ -32,6 +32,8 @@ import ( "sync/atomic" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestDo(t *testing.T) { @@ -42,9 +44,8 @@ func TestDo(t *testing.T) { if got, want := fmt.Sprintf("%v (%T)", v, v), "bar (string)"; got != want { t.Errorf("Do = %v; want %v", got, want) } - if err != nil { - t.Errorf("Do error = %v", err) - } + assert.NoError(t, err, "Do error = %v", err) + } func TestDoErr(t *testing.T) { @@ -53,12 +54,9 @@ func TestDoErr(t *testing.T) { v, err, _ := g.Do("key", func() (string, error) { return "", someErr }) - if err != someErr { - t.Errorf("Do error = %v; want someErr %v", err, someErr) - } - if v != "" { - t.Errorf("unexpected non-nil value %#v", v) - } + assert.Equal(t, someErr, err, "Do error = %v; want someErr %v", err, someErr) + assert.Equal(t, "", v, "unexpected non-nil value %#v", v) + } func TestDoDupSuppress(t *testing.T) { @@ -72,9 +70,9 @@ func TestDoDupSuppress(t *testing.T) { wg1.Done() } v := <-c - c <- v // pump; make available for any future calls + c <- v // pump; make available for any future calls - time.Sleep(10 * time.Millisecond) // let more goroutines enter Do + time.Sleep(10 * time.Millisecond) // let more goroutines enter Do return v, nil } @@ -159,9 +157,8 @@ func TestGoexitDo(t *testing.T) { go func() { var err error defer func() { - if err != nil { - t.Errorf("Error should be nil, but got: %v", err) - } + assert.NoError(t, err, "Error should be nil, but got: %v", err) + if atomic.AddInt32(&waited, -1) == 0 { close(done) } diff --git a/go/event/syslogger/syslogger_test.go b/go/event/syslogger/syslogger_test.go index 28925d2ac52..30a4fe234fe 100644 --- a/go/event/syslogger/syslogger_test.go +++ b/go/event/syslogger/syslogger_test.go @@ -25,12 +25,14 @@ import ( "testing" "vitess.io/vitess/go/event" + + "github.com/stretchr/testify/assert" ) type TestEvent struct { - triggered bool - priority syslog.Priority - message string + triggered bool + priority syslog.Priority + message string } func (ev *TestEvent) Syslog() (syslog.Priority, string) { @@ -38,12 +40,12 @@ func (ev *TestEvent) Syslog() (syslog.Priority, string) { return ev.priority, ev.message } -var _ Syslogger = (*TestEvent)(nil) // compile-time interface check +var _ Syslogger = (*TestEvent)(nil) // compile-time interface check type fakeWriter struct { - priority syslog.Priority - message string - err error // if non-nil, force an error to be returned + priority syslog.Priority + message string + err error // if non-nil, force an error to be returned } func (fw *fakeWriter) write(pri syslog.Priority, msg string) error { @@ -55,14 +57,14 @@ func (fw *fakeWriter) write(pri syslog.Priority, msg string) error { fw.message = msg return nil } -func (fw *fakeWriter) Alert(msg string) error { return fw.write(syslog.LOG_ALERT, msg) } -func (fw *fakeWriter) Crit(msg string) error { return fw.write(syslog.LOG_CRIT, msg) } -func (fw *fakeWriter) Debug(msg string) error { return fw.write(syslog.LOG_DEBUG, msg) } -func (fw *fakeWriter) Emerg(msg string) error { return fw.write(syslog.LOG_EMERG, msg) } -func (fw *fakeWriter) Err(msg string) error { return fw.write(syslog.LOG_ERR, msg) } -func (fw *fakeWriter) Info(msg string) error { return fw.write(syslog.LOG_INFO, msg) } -func (fw *fakeWriter) Notice(msg string) error { return fw.write(syslog.LOG_NOTICE, msg) } -func (fw *fakeWriter) Warning(msg string) error { return fw.write(syslog.LOG_WARNING, msg) } +func (fw *fakeWriter) Alert(msg string) error { return fw.write(syslog.LOG_ALERT, msg) } +func (fw *fakeWriter) Crit(msg string) error { return fw.write(syslog.LOG_CRIT, msg) } +func (fw *fakeWriter) Debug(msg string) error { return fw.write(syslog.LOG_DEBUG, msg) } +func (fw *fakeWriter) Emerg(msg string) error { return fw.write(syslog.LOG_EMERG, msg) } +func (fw *fakeWriter) Err(msg string) error { return fw.write(syslog.LOG_ERR, msg) } +func (fw *fakeWriter) Info(msg string) error { return fw.write(syslog.LOG_INFO, msg) } +func (fw *fakeWriter) Notice(msg string) error { return fw.write(syslog.LOG_NOTICE, msg) } +func (fw *fakeWriter) Warning(msg string) error { return fw.write(syslog.LOG_WARNING, msg) } // TestSyslog checks that our callback works. func TestSyslog(t *testing.T) { @@ -70,10 +72,8 @@ func TestSyslog(t *testing.T) { ev := new(TestEvent) event.Dispatch(ev) + assert.True(t, ev.triggered, "Syslog() was not called on event that implements Syslogger") - if !ev.triggered { - t.Errorf("Syslog() was not called on event that implements Syslogger") - } } // TestBadWriter verifies we are still triggering (to normal logs) if @@ -87,55 +87,40 @@ func TestBadWriter(t *testing.T) { wantLevel := "ERROR" ev := &TestEvent{priority: syslog.LOG_ALERT, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().msg, wantMsg) { - t.Errorf("error log msg [%s], want msg [%s]", tl.getLog().msg, wantMsg) - } - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().msg, wantMsg), "error log msg [%s], want msg [%s]", tl.getLog().msg, wantMsg) + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + ev = &TestEvent{priority: syslog.LOG_CRIT, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + ev = &TestEvent{priority: syslog.LOG_ERR, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + ev = &TestEvent{priority: syslog.LOG_EMERG, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) wantLevel = "WARNING" ev = &TestEvent{priority: syslog.LOG_WARNING, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) wantLevel = "INFO" ev = &TestEvent{priority: syslog.LOG_INFO, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + ev = &TestEvent{priority: syslog.LOG_NOTICE, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + ev = &TestEvent{priority: syslog.LOG_DEBUG, message: wantMsg} event.Dispatch(ev) - if !strings.Contains(tl.getLog().level, wantLevel) { - t.Errorf("error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - } + assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + assert.True(t, ev.triggered, "passed nil writer to client") - if !ev.triggered { - t.Errorf("passed nil writer to client") - } } // TestWriteError checks that we don't panic on a write error. @@ -150,10 +135,8 @@ func TestInvalidSeverity(t *testing.T) { writer = fw event.Dispatch(&TestEvent{priority: syslog.Priority(123), message: "log me"}) + assert.NotEqual(t, "log me", fw.message, "message was logged despite invalid severity") - if fw.message == "log me" { - t.Errorf("message was logged despite invalid severity") - } } func testSeverity(sev syslog.Priority, t *testing.T) { @@ -161,13 +144,9 @@ func testSeverity(sev syslog.Priority, t *testing.T) { writer = fw event.Dispatch(&TestEvent{priority: sev, message: "log me"}) + assert.Equal(t, sev, fw.priority, "wrong priority: got %v, want %v", fw.priority, sev) + assert.Equal(t, "log me", fw.message, `wrong message: got "%v", want "%v"`, fw.message, "log me") - if fw.priority != sev { - t.Errorf("wrong priority: got %v, want %v", fw.priority, sev) - } - if fw.message != "log me" { - t.Errorf(`wrong message: got "%v", want "%v"`, fw.message, "log me") - } } func TestEmerg(t *testing.T) { diff --git a/go/flagutil/flagutil_test.go b/go/flagutil/flagutil_test.go index dda17bb13d7..34a3d42f8a5 100644 --- a/go/flagutil/flagutil_test.go +++ b/go/flagutil/flagutil_test.go @@ -22,28 +22,26 @@ import ( "github.com/spf13/pflag" "github.com/stretchr/testify/require" + "github.com/stretchr/testify/assert" ) func TestStringList(t *testing.T) { p := StringListValue([]string{}) var _ pflag.Value = &p wanted := map[string]string{ - "0ala,ma,kota": "0ala.ma.kota", - `1ala\,ma,kota`: "1ala,ma.kota", - `2ala\\,ma,kota`: `2ala\.ma.kota`, - "3ala,": "3ala.", + "0ala,ma,kota": "0ala.ma.kota", + `1ala\,ma,kota`: "1ala,ma.kota", + `2ala\\,ma,kota`: `2ala\.ma.kota`, + "3ala,": "3ala.", } for in, out := range wanted { if err := p.Set(in); err != nil { t.Errorf("v.Set(%v): %v", in, err) continue } - if strings.Join(p, ".") != out { - t.Errorf("want %#v, got %#v", strings.Split(out, "."), p) - } - if p.String() != in { - t.Errorf("v.String(): want %#v, got %#v", in, p.String()) - } + assert.Equal(t, out, strings.Join(p, "."), "want %#v, got %#v", strings.Split(out, "."), p) + assert.Equal(t, in, p.String(), "v.String(): want %#v, got %#v", in, p.String()) + } } @@ -60,9 +58,9 @@ func TestEmptyStringList(t *testing.T) { } type pair struct { - in string - out map[string]string - err error + in string + out map[string]string + err error } func TestStringMap(t *testing.T) { @@ -70,16 +68,16 @@ func TestStringMap(t *testing.T) { var _ pflag.Value = &v wanted := []pair{ { - in: "tag1:value1,tag2:value2", - out: map[string]string{"tag1": "value1", "tag2": "value2"}, + in: "tag1:value1,tag2:value2", + out: map[string]string{"tag1": "value1", "tag2": "value2"}, }, { - in: `tag1:1:value1\,,tag2:value2`, - out: map[string]string{"tag1": "1:value1,", "tag2": "value2"}, + in: `tag1:1:value1\,,tag2:value2`, + out: map[string]string{"tag1": "1:value1,", "tag2": "value2"}, }, { - in: `tag1:1:value1\,,tag2`, - err: errInvalidKeyValuePair, + in: `tag1:1:value1\,,tag2`, + err: errInvalidKeyValuePair, }, } for _, want := range wanted { diff --git a/go/history/history_test.go b/go/history/history_test.go index 3e817b34bb2..d82138569a1 100644 --- a/go/history/history_test.go +++ b/go/history/history_test.go @@ -18,6 +18,8 @@ package history import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestHistory(t *testing.T) { @@ -33,9 +35,8 @@ func TestHistory(t *testing.T) { t.Errorf("len(records): want %v, got %v. records: %+v", want, got, q) } for i, record := range records { - if record != want[i] { - t.Errorf("record doesn't match: want %v, got %v", want[i], record) - } + assert.Equal(t, want[i], record, "record doesn't match: want %v, got %v", want[i], record) + } for ; i < 6; i++ { @@ -48,9 +49,8 @@ func TestHistory(t *testing.T) { t.Errorf("len(records): want %v, got %v. records: %+v", want, got, q) } for i, record := range records { - if record != want[i] { - t.Errorf("record doesn't match: want %v, got %v", want[i], record) - } + assert.Equal(t, want[i], record, "record doesn't match: want %v, got %v", want[i], record) + } } From aff9e49575ab6b56e22713cce3485791cf2ada37 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Thu, 15 Feb 2024 14:29:10 +0000 Subject: [PATCH 05/15] chore: replace errof with assert using test-rewrite tool Signed-off-by: Manik Rana --- go/event/event_test.go | 35 ++---- go/mysql/decimal/decimal_test.go | 181 +++++++++++-------------------- 2 files changed, 72 insertions(+), 144 deletions(-) diff --git a/go/event/event_test.go b/go/event/event_test.go index f7356a98f3e..2bea16cb515 100644 --- a/go/event/event_test.go +++ b/go/event/event_test.go @@ -20,6 +20,8 @@ import ( "reflect" "testing" "time" + + "github.com/stretchr/testify/assert" ) type testInterface1 interface { @@ -56,10 +58,8 @@ func TestStaticListener(t *testing.T) { AddListener(func(testEvent1) { triggered = true }) AddListener(func(testEvent2) { t.Errorf("wrong listener type triggered") }) Dispatch(testEvent1{}) + assert.True(t, triggered, "static listener failed to trigger") - if !triggered { - t.Errorf("static listener failed to trigger") - } } func TestPointerListener(t *testing.T) { @@ -69,10 +69,8 @@ func TestPointerListener(t *testing.T) { AddListener(func(ev *testEvent2) { ev.triggered = true }) AddListener(func(testEvent2) { t.Errorf("non-pointer listener triggered on pointer type") }) Dispatch(testEvent) + assert.True(t, testEvent.triggered, "pointer listener failed to trigger") - if !testEvent.triggered { - t.Errorf("pointer listener failed to trigger") - } } func TestInterfaceListener(t *testing.T) { @@ -82,10 +80,8 @@ func TestInterfaceListener(t *testing.T) { AddListener(func(testInterface1) { triggered = true }) AddListener(func(testInterface2) { t.Errorf("interface listener triggered on non-matching type") }) Dispatch(testEvent1{}) + assert.True(t, triggered, "interface listener failed to trigger") - if !triggered { - t.Errorf("interface listener failed to trigger") - } } func TestEmptyInterfaceListener(t *testing.T) { @@ -94,10 +90,8 @@ func TestEmptyInterfaceListener(t *testing.T) { triggered := false AddListener(func(any) { triggered = true }) Dispatch("this should match any") + assert.True(t, triggered, "any listener failed to trigger") - if !triggered { - t.Errorf("any listener failed to trigger") - } } func TestMultipleListeners(t *testing.T) { @@ -144,7 +138,6 @@ func TestBadListenerWrongType(t *testing.T) { defer func() { err := recover() - if err == nil { t.Errorf("bad listener type (not a func) failed to trigger panic") } @@ -186,10 +179,8 @@ func TestDispatchPointerToValueInterfaceListener(t *testing.T) { triggered = true }) Dispatch(&testEvent1{}) + assert.True(t, triggered, "Dispatch by pointer failed to trigger interface listener") - if !triggered { - t.Errorf("Dispatch by pointer failed to trigger interface listener") - } } func TestDispatchValueToValueInterfaceListener(t *testing.T) { @@ -200,10 +191,8 @@ func TestDispatchValueToValueInterfaceListener(t *testing.T) { triggered = true }) Dispatch(testEvent1{}) + assert.True(t, triggered, "Dispatch by value failed to trigger interface listener") - if !triggered { - t.Errorf("Dispatch by value failed to trigger interface listener") - } } func TestDispatchPointerToPointerInterfaceListener(t *testing.T) { @@ -212,10 +201,8 @@ func TestDispatchPointerToPointerInterfaceListener(t *testing.T) { triggered := false AddListener(func(testInterface2) { triggered = true }) Dispatch(&testEvent2{}) + assert.True(t, triggered, "interface listener failed to trigger for pointer") - if !triggered { - t.Errorf("interface listener failed to trigger for pointer") - } } func TestDispatchValueToPointerInterfaceListener(t *testing.T) { @@ -245,10 +232,8 @@ func TestDispatchUpdate(t *testing.T) { ev := &testUpdateEvent{} DispatchUpdate(ev, "hello") + assert.True(t, triggered, "listener failed to trigger on DispatchUpdate()") - if !triggered { - t.Errorf("listener failed to trigger on DispatchUpdate()") - } want := "hello" if got := ev.update.(string); got != want { t.Errorf("ev.update = %#v, want %#v", got, want) diff --git a/go/mysql/decimal/decimal_test.go b/go/mysql/decimal/decimal_test.go index 09819ddcebb..233028f9839 100644 --- a/go/mysql/decimal/decimal_test.go +++ b/go/mysql/decimal/decimal_test.go @@ -26,6 +26,8 @@ import ( "strings" "testing" "testing/quick" + + "github.com/stretchr/testify/assert" ) type testEnt struct { @@ -120,11 +122,8 @@ func TestNewFromFloat(t *testing.T) { for _, x := range testTable { s := x.short d := NewFromFloat(x.float) - if d.String() != s { - t.Errorf("expected %s, got %s (float: %v) (%s, %d)", - s, d.String(), x.float, - d.value.String(), d.exp) - } + assert.Equal(t, s, d.String(), "expected %s, got %s (float: %v) (%s, %d)", s, d.String(), x.float, d.value.String(), d.exp) + } shouldPanicOn := []float64{ @@ -156,11 +155,8 @@ func TestNewFromFloatRandom(t *testing.T) { continue } got := NewFromFloat(in) - if !want.Equal(got) { - t.Errorf("in: %v, expected %s (%s, %d), got %s (%s, %d) ", - in, want.String(), want.value.String(), want.exp, - got.String(), got.value.String(), got.exp) - } + assert.True(t, want.Equal(got), "in: %v, expected %s (%s, %d), got %s (%s, %d) ", in, want.String(), want.value.String(), want.exp, got.String(), got.value.String(), got.exp) + } } @@ -193,11 +189,8 @@ func TestNewFromFloat32Random(t *testing.T) { continue } got := NewFromFloat32(in) - if !want.Equal(got) { - t.Errorf("in: %v, expected %s (%s, %d), got %s (%s, %d) ", - in, want.String(), want.value.String(), want.exp, - got.String(), got.value.String(), got.exp) - } + assert.True(t, want.Equal(got), "in: %v, expected %s (%s, %d), got %s (%s, %d) ", in, want.String(), want.value.String(), want.exp, got.String(), got.value.String(), got.exp) + } } @@ -317,14 +310,9 @@ func TestNewFromStringErrs(t *testing.T) { for s, o := range tests { out, err := NewFromString(s) + assert.Error(t, err, "error expected when parsing %s", s) + assert.Equal(t, o, out.String(), "expected %s, got %s", o, out.String()) - if err == nil { - t.Errorf("error expected when parsing %s", s) - } - - if out.String() != o { - t.Errorf("expected %s, got %s", o, out.String()) - } } } @@ -353,11 +341,8 @@ func TestNewFromStringDeepEquals(t *testing.T) { if err1 != nil || err2 != nil { t.Errorf("error parsing strings to decimals") } + assert.Equal(t, cmp.expected, reflect.DeepEqual(d1, d2), "comparison result is different from expected results for %s and %s", cmp.str1, cmp.str2) - if reflect.DeepEqual(d1, d2) != cmp.expected { - t.Errorf("comparison result is different from expected results for %s and %s", - cmp.str1, cmp.str2) - } } } @@ -413,11 +398,9 @@ func TestNewFromInt(t *testing.T) { for input, s := range tests { d := NewFromInt(input) - if d.String() != s { - t.Errorf("expected %s, got %s (%s, %d)", - s, d.String(), - d.value.String(), d.exp) - } + assert.Equal(t, s, d.String(), "expected %s, got %s (%s, %d)", s, d.String(), + d.value.String(), d.exp) + } } @@ -499,20 +482,15 @@ func TestDecimal_RoundAndStringFixed(t *testing.T) { t.Fatal(err) } got := d.Round(test.places) - if !got.Equal(expected) { - t.Errorf("Rounding %s to %d places, got %s, expected %s", - d, test.places, got, expected) - } + assert.True(t, got.Equal(expected), "Rounding %s to %d places, got %s, expected %s", d, test.places, got, expected) // test StringFixed if test.expectedFixed == "" { test.expectedFixed = test.expected } gotStr := d.StringFixed(test.places) - if gotStr != test.expectedFixed { - t.Errorf("(%s).StringFixed(%d): got %s, expected %s", - d, test.places, gotStr, test.expectedFixed) - } + assert.Equal(t, test.expectedFixed, gotStr, "(%s).StringFixed(%d): got %s, expected %s", d, test.places, gotStr, test.expectedFixed) + } } @@ -541,9 +519,8 @@ func TestDecimal_Add(t *testing.T) { t.FailNow() } c := a.Add(b) - if c.String() != res { - t.Errorf("expected %s, got %s", res, c.String()) - } + assert.Equal(t, res, c.String(), "expected %s, got %s", res, c.String()) + } } @@ -576,9 +553,8 @@ func TestDecimal_Sub(t *testing.T) { t.FailNow() } c := a.sub(b) - if c.String() != res { - t.Errorf("expected %s, got %s", res, c.String()) - } + assert.Equal(t, res, c.String(), "expected %s, got %s", res, c.String()) + } } @@ -597,18 +573,16 @@ func TestDecimal_Neg(t *testing.T) { t.FailNow() } b := a.Neg() - if b.String() != res { - t.Errorf("expected %s, got %s", res, b.String()) - } + assert.Equal(t, res, b.String(), "expected %s, got %s", res, b.String()) + } } func TestDecimal_NegFromEmpty(t *testing.T) { a := Decimal{} b := a.Neg() - if b.String() != "0" { - t.Errorf("expected %s, got %s", "0", b) - } + assert.Equal(t, "0", b.String(), "expected %s, got %s", "0", b) + } func TestDecimal_Mul(t *testing.T) { @@ -635,16 +609,14 @@ func TestDecimal_Mul(t *testing.T) { t.FailNow() } c := a.mul(b) - if c.String() != res { - t.Errorf("expected %s, got %s", res, c.String()) - } + assert.Equal(t, res, c.String(), "expected %s, got %s", res, c.String()) + } // positive scale c := New(1234, 5).mul(New(45, -1)) - if c.String() != "555300000" { - t.Errorf("Expected %s, got %s", "555300000", c.String()) - } + assert.Equal(t, "555300000", c.String(), "Expected %s, got %s", "555300000", c.String()) + } func TestDecimal_Div(t *testing.T) { @@ -679,14 +651,11 @@ func TestDecimal_Div(t *testing.T) { } got := num.div(denom) expected, _ := NewFromString(expectedStr) - if !got.Equal(expected) { - t.Errorf("expected %v when dividing %v by %v, got %v", - expected, num, denom, got) - } + assert.True(t, got.Equal(expected), "expected %v when dividing %v by %v, got %v", expected, num, denom, got) + got2 := num.divRound(denom, int32(divisionPrecision)) - if !got2.Equal(expected) { - t.Errorf("expected %v on divRound (%v,%v), got %v", expected, num, denom, got2) - } + assert.True(t, got2.Equal(expected), "expected %v on divRound (%v,%v), got %v", expected, num, denom, got2) + } type Inp2 struct { @@ -717,10 +686,8 @@ func TestDecimal_Div(t *testing.T) { expected = "-" + expectedAbs } got := num.div(denom) - if got.String() != expected { - t.Errorf("expected %s when dividing %v by %v, got %v", - expected, num, denom, got) - } + assert.Equal(t, expected, got.String(), "expected %s when dividing %v by %v, got %v", expected, num, denom, got) + } } } @@ -761,14 +728,9 @@ func TestDecimal_QuoRem(t *testing.T) { t.Errorf("bad QuoRem division %s , %s , %d got %v, %v expected %s , %s", inp4.d, inp4.d2, prec, q, r, inp4.q, inp4.r) } - if !d.Equal(d2.mul(q).Add(r)) { - t.Errorf("not fitting: d=%v, d2= %v, prec=%d, q=%v, r=%v", - d, d2, prec, q, r) - } - if !q.Equal(q.Truncate(prec)) { - t.Errorf("quotient wrong precision: d=%v, d2= %v, prec=%d, q=%v, r=%v", - d, d2, prec, q, r) - } + assert.True(t, d.Equal(d2.mul(q).Add(r)), "not fitting: d=%v, d2= %v, prec=%d, q=%v, r=%v", d, d2, prec, q, r) + assert.True(t, q.Equal(q.Truncate(prec)), "quotient wrong precision: d=%v, d2= %v, prec=%d, q=%v, r=%v", d, d2, prec, q, r) + if r.Abs().Cmp(d2.Abs().mul(New(1, -prec))) >= 0 { t.Errorf("remainder too large: d=%v, d2= %v, prec=%d, q=%v, r=%v", d, d2, prec, q, r) @@ -822,16 +784,12 @@ func TestDecimal_QuoRem2(t *testing.T) { d2 := tc.d2 prec := tc.prec q, r := d.QuoRem(d2, prec) - // rule 1: d = d2*q +r - if !d.Equal(d2.mul(q).Add(r)) { - t.Errorf("not fitting, d=%v, d2=%v, prec=%d, q=%v, r=%v", - d, d2, prec, q, r) - } - // rule 2: q is integral multiple of 10^(-prec) - if !q.Equal(q.Truncate(prec)) { - t.Errorf("quotient wrong precision, d=%v, d2=%v, prec=%d, q=%v, r=%v", - d, d2, prec, q, r) - } + assert. + // rule 1: d = d2*q +r + True(t, d.Equal(d2.mul(q).Add(r)), "not fitting, d=%v, d2=%v, prec=%d, q=%v, r=%v", d, d2, prec, q, r) + assert.True(t, // rule 2: q is integral multiple of 10^(-prec) + q.Equal(q.Truncate(prec)), "quotient wrong precision, d=%v, d2=%v, prec=%d, q=%v, r=%v", d, d2, prec, q, r) + // rule 3: abs(r)= 0 { t.Errorf("remainder too large, d=%v, d2=%v, prec=%d, q=%v, r=%v", @@ -894,9 +852,8 @@ func TestDecimal_DivRound(t *testing.T) { if x.Cmp(d2.Abs().mul(New(-1, -prec))) <= 0 { t.Errorf("wrong rounding, got: %v/%v prec=%d is about %v", d, d2, prec, q) } - if !q.Equal(result) { - t.Errorf("rounded division wrong %s / %s scale %d = %s, got %v", s.d, s.d2, prec, s.result, q) - } + assert.True(t, q.Equal(result), "rounded division wrong %s / %s scale %d = %s, got %v", s.d, s.d2, prec, s.result, q) + } } @@ -953,9 +910,8 @@ func TestDecimal_Mod(t *testing.T) { t.FailNow() } c := a.mod(b) - if c.String() != res { - t.Errorf("expected %s, got %s", res, c.String()) - } + assert.Equal(t, res, c.String(), "expected %s, got %s", res, c.String()) + } } @@ -972,9 +928,8 @@ func TestDecimal_Overflow(t *testing.T) { func TestDecimal_Scale(t *testing.T) { a := New(1234, -3) - if a.Exponent() != -3 { - t.Errorf("error") - } + assert.Equal(t, -3, a.Exponent(), "error") + } func TestDecimal_Abs1(t *testing.T) { @@ -982,9 +937,8 @@ func TestDecimal_Abs1(t *testing.T) { b := New(1234, -4) c := a.Abs() - if c.Cmp(b) != 0 { - t.Errorf("error") - } + assert.Equal(t, 0, c.Cmp(b), "error") + } func TestDecimal_Abs2(t *testing.T) { @@ -992,9 +946,8 @@ func TestDecimal_Abs2(t *testing.T) { b := New(1234, -4) c := b.Abs() - if c.Cmp(a) == 0 { - t.Errorf("error") - } + assert.NotEqual(t, 0, c.Cmp(a), "error") + } func TestDecimal_ScalesNotEqual(t *testing.T) { @@ -1008,19 +961,15 @@ func TestDecimal_ScalesNotEqual(t *testing.T) { func TestDecimal_Cmp1(t *testing.T) { a := New(123, 3) b := New(-1234, 2) + assert.Equal(t, 1, a.Cmp(b), "Error") - if a.Cmp(b) != 1 { - t.Errorf("Error") - } } func TestDecimal_Cmp2(t *testing.T) { a := New(123, 3) b := New(1234, 2) + assert.Equal(t, -1, a.Cmp(b), "Error") - if a.Cmp(b) != -1 { - t.Errorf("Error") - } } func TestDecimal_IsInteger(t *testing.T) { @@ -1045,26 +994,20 @@ func TestDecimal_IsInteger(t *testing.T) { if err != nil { t.Fatal(err) } - if d.isInteger() != testCase.IsInteger { - t.Errorf("expect %t, got %t, for %s", testCase.IsInteger, d.isInteger(), testCase.Dec) - } + assert.Equal(t, testCase.IsInteger, d.isInteger(), "expect %t, got %t, for %s", testCase.IsInteger, d.isInteger(), testCase.Dec) + } } func TestDecimal_Sign(t *testing.T) { - if Zero.Sign() != 0 { - t.Errorf("%q should have sign 0", Zero) - } + assert.Equal(t, 0, Zero.Sign(), "%q should have sign 0", Zero) one := New(1, 0) - if one.Sign() != 1 { - t.Errorf("%q should have sign 1", one) - } + assert.Equal(t, 1, one.Sign(), "%q should have sign 1", one) mone := New(-1, 0) - if mone.Sign() != -1 { - t.Errorf("%q should have sign -1", mone) - } + assert.Equal(t, -1, mone.Sign(), "%q should have sign -1", mone) + } func didPanic(f func()) bool { From fec218e0c74791f0680ef981fe0facacb77a7281 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Thu, 15 Feb 2024 23:27:59 +0530 Subject: [PATCH 06/15] chore: replace with assert using test-rewriter + fix failing test Signed-off-by: Manik Rana --- go/cache/lru_cache_test.go | 14 +++--- go/cache/theine/singleflight_test.go | 10 ++-- go/event/event_test.go | 8 ++-- go/event/syslogger/syslogger_test.go | 58 ++++++++++++------------ go/flagutil/flagutil_test.go | 32 ++++++------- go/history/history_test.go | 4 +- go/mysql/decimal/decimal_test.go | 68 ++++++++++++++-------------- 7 files changed, 97 insertions(+), 97 deletions(-) diff --git a/go/cache/lru_cache_test.go b/go/cache/lru_cache_test.go index 8e18762e681..834333af919 100644 --- a/go/cache/lru_cache_test.go +++ b/go/cache/lru_cache_test.go @@ -30,11 +30,11 @@ func TestInitialState(t *testing.T) { cache := NewLRUCache[*CacheValue](5) l, sz, c, e, h, m := cache.Len(), cache.UsedCapacity(), cache.MaxCapacity(), cache.Evictions(), cache.Hits(), cache.Misses() assert.Equal(t, 0, l, "length = %v, want 0", l) - assert.Equal(t, int64(0), sz, "size = %v, want 0", sz) - assert.Equal(t, int64(5), c, "capacity = %v, want 5", c) - assert.Equal(t, int64(0), e, "evictions = %v, want 0", c) - assert.Equal(t, int64(0), h, "hits = %v, want 0", c) - assert.Equal(t, int64(0), m, "misses = %v, want 0", c) + assert.Equal(t, int64(0), sz) + assert.Equal(t, int64(5), c) + assert.Equal(t, int64(0), e) + assert.Equal(t, int64(0), h) + assert.Equal(t, int64(0), m) } @@ -128,8 +128,8 @@ func TestCapacityIsObeyed(t *testing.T) { // Insert one more; something should be evicted to make room. cache.Set("key4", value) sz, evictions := cache.UsedCapacity(), cache.Evictions() - assert.Equal(t, size, sz, "post-evict cache.UsedCapacity() = %v, expected %v", sz, size) - assert.Equal(t, int64(1), evictions, "post-evict cache.Evictions() = %v, expected 1", evictions) + assert.Equal(t, size, sz) + assert.Equal(t, int64(1), evictions) // Check various other stats if l := cache.Len(); int64(l) != size { diff --git a/go/cache/theine/singleflight_test.go b/go/cache/theine/singleflight_test.go index 1f9be7cb027..787280527ba 100644 --- a/go/cache/theine/singleflight_test.go +++ b/go/cache/theine/singleflight_test.go @@ -34,6 +34,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestDo(t *testing.T) { @@ -54,8 +55,8 @@ func TestDoErr(t *testing.T) { return "", someErr }) - assert.ErrorIs(t, err, someErr, "incorrect Do error") - assert.Empty(t, v, "unexpected non-empty value") + assert.Equal(t, someErr, err) + assert.Equal(t, "", v) } @@ -70,9 +71,9 @@ func TestDoDupSuppress(t *testing.T) { wg1.Done() } v := <-c - c <- v // pump; make available for any future calls + c <- v // pump; make available for any future calls - time.Sleep(10 * time.Millisecond) // let more goroutines enter Do + time.Sleep(10 * time.Millisecond) // let more goroutines enter Do return v, nil } @@ -153,6 +154,7 @@ func TestGoexitDo(t *testing.T) { var err error defer func() { assert.NoError(t, err) + if atomic.AddInt32(&waited, -1) == 0 { close(done) } diff --git a/go/event/event_test.go b/go/event/event_test.go index 2bea16cb515..2f2cab9aee6 100644 --- a/go/event/event_test.go +++ b/go/event/event_test.go @@ -179,7 +179,7 @@ func TestDispatchPointerToValueInterfaceListener(t *testing.T) { triggered = true }) Dispatch(&testEvent1{}) - assert.True(t, triggered, "Dispatch by pointer failed to trigger interface listener") + assert.True(t, triggered) } @@ -191,7 +191,7 @@ func TestDispatchValueToValueInterfaceListener(t *testing.T) { triggered = true }) Dispatch(testEvent1{}) - assert.True(t, triggered, "Dispatch by value failed to trigger interface listener") + assert.True(t, triggered) } @@ -201,7 +201,7 @@ func TestDispatchPointerToPointerInterfaceListener(t *testing.T) { triggered := false AddListener(func(testInterface2) { triggered = true }) Dispatch(&testEvent2{}) - assert.True(t, triggered, "interface listener failed to trigger for pointer") + assert.True(t, triggered) } @@ -232,7 +232,7 @@ func TestDispatchUpdate(t *testing.T) { ev := &testUpdateEvent{} DispatchUpdate(ev, "hello") - assert.True(t, triggered, "listener failed to trigger on DispatchUpdate()") + assert.True(t, triggered) want := "hello" if got := ev.update.(string); got != want { diff --git a/go/event/syslogger/syslogger_test.go b/go/event/syslogger/syslogger_test.go index 30a4fe234fe..6c6a181d2e5 100644 --- a/go/event/syslogger/syslogger_test.go +++ b/go/event/syslogger/syslogger_test.go @@ -30,9 +30,9 @@ import ( ) type TestEvent struct { - triggered bool - priority syslog.Priority - message string + triggered bool + priority syslog.Priority + message string } func (ev *TestEvent) Syslog() (syslog.Priority, string) { @@ -40,12 +40,12 @@ func (ev *TestEvent) Syslog() (syslog.Priority, string) { return ev.priority, ev.message } -var _ Syslogger = (*TestEvent)(nil) // compile-time interface check +var _ Syslogger = (*TestEvent)(nil) // compile-time interface check type fakeWriter struct { - priority syslog.Priority - message string - err error // if non-nil, force an error to be returned + priority syslog.Priority + message string + err error // if non-nil, force an error to be returned } func (fw *fakeWriter) write(pri syslog.Priority, msg string) error { @@ -57,14 +57,14 @@ func (fw *fakeWriter) write(pri syslog.Priority, msg string) error { fw.message = msg return nil } -func (fw *fakeWriter) Alert(msg string) error { return fw.write(syslog.LOG_ALERT, msg) } -func (fw *fakeWriter) Crit(msg string) error { return fw.write(syslog.LOG_CRIT, msg) } -func (fw *fakeWriter) Debug(msg string) error { return fw.write(syslog.LOG_DEBUG, msg) } -func (fw *fakeWriter) Emerg(msg string) error { return fw.write(syslog.LOG_EMERG, msg) } -func (fw *fakeWriter) Err(msg string) error { return fw.write(syslog.LOG_ERR, msg) } -func (fw *fakeWriter) Info(msg string) error { return fw.write(syslog.LOG_INFO, msg) } -func (fw *fakeWriter) Notice(msg string) error { return fw.write(syslog.LOG_NOTICE, msg) } -func (fw *fakeWriter) Warning(msg string) error { return fw.write(syslog.LOG_WARNING, msg) } +func (fw *fakeWriter) Alert(msg string) error { return fw.write(syslog.LOG_ALERT, msg) } +func (fw *fakeWriter) Crit(msg string) error { return fw.write(syslog.LOG_CRIT, msg) } +func (fw *fakeWriter) Debug(msg string) error { return fw.write(syslog.LOG_DEBUG, msg) } +func (fw *fakeWriter) Emerg(msg string) error { return fw.write(syslog.LOG_EMERG, msg) } +func (fw *fakeWriter) Err(msg string) error { return fw.write(syslog.LOG_ERR, msg) } +func (fw *fakeWriter) Info(msg string) error { return fw.write(syslog.LOG_INFO, msg) } +func (fw *fakeWriter) Notice(msg string) error { return fw.write(syslog.LOG_NOTICE, msg) } +func (fw *fakeWriter) Warning(msg string) error { return fw.write(syslog.LOG_WARNING, msg) } // TestSyslog checks that our callback works. func TestSyslog(t *testing.T) { @@ -72,7 +72,7 @@ func TestSyslog(t *testing.T) { ev := new(TestEvent) event.Dispatch(ev) - assert.True(t, ev.triggered, "Syslog() was not called on event that implements Syslogger") + assert.True(t, ev.triggered) } @@ -87,39 +87,39 @@ func TestBadWriter(t *testing.T) { wantLevel := "ERROR" ev := &TestEvent{priority: syslog.LOG_ALERT, message: wantMsg} event.Dispatch(ev) - assert.True(t, strings.Contains(tl.getLog().msg, wantMsg), "error log msg [%s], want msg [%s]", tl.getLog().msg, wantMsg) - assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + assert.True(t, strings.Contains(tl.getLog().msg, wantMsg)) + assert.True(t, strings.Contains(tl.getLog().level, wantLevel)) ev = &TestEvent{priority: syslog.LOG_CRIT, message: wantMsg} event.Dispatch(ev) - assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + assert.True(t, strings.Contains(tl.getLog().level, wantLevel)) ev = &TestEvent{priority: syslog.LOG_ERR, message: wantMsg} event.Dispatch(ev) - assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + assert.True(t, strings.Contains(tl.getLog().level, wantLevel)) ev = &TestEvent{priority: syslog.LOG_EMERG, message: wantMsg} event.Dispatch(ev) - assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + assert.True(t, strings.Contains(tl.getLog().level, wantLevel)) wantLevel = "WARNING" ev = &TestEvent{priority: syslog.LOG_WARNING, message: wantMsg} event.Dispatch(ev) - assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + assert.True(t, strings.Contains(tl.getLog().level, wantLevel)) wantLevel = "INFO" ev = &TestEvent{priority: syslog.LOG_INFO, message: wantMsg} event.Dispatch(ev) - assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + assert.True(t, strings.Contains(tl.getLog().level, wantLevel)) ev = &TestEvent{priority: syslog.LOG_NOTICE, message: wantMsg} event.Dispatch(ev) - assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) + assert.True(t, strings.Contains(tl.getLog().level, wantLevel)) ev = &TestEvent{priority: syslog.LOG_DEBUG, message: wantMsg} event.Dispatch(ev) - assert.True(t, strings.Contains(tl.getLog().level, wantLevel), "error log level [%s], want level [%s]", tl.getLog().level, wantLevel) - assert.True(t, ev.triggered, "passed nil writer to client") + assert.True(t, strings.Contains(tl.getLog().level, wantLevel)) + assert.True(t, ev.triggered) } @@ -135,7 +135,7 @@ func TestInvalidSeverity(t *testing.T) { writer = fw event.Dispatch(&TestEvent{priority: syslog.Priority(123), message: "log me"}) - assert.NotEqual(t, "log me", fw.message, "message was logged despite invalid severity") + assert.NotEqual(t, "log me", fw.message) } @@ -144,8 +144,8 @@ func testSeverity(sev syslog.Priority, t *testing.T) { writer = fw event.Dispatch(&TestEvent{priority: sev, message: "log me"}) - assert.Equal(t, sev, fw.priority, "wrong priority: got %v, want %v", fw.priority, sev) - assert.Equal(t, "log me", fw.message, `wrong message: got "%v", want "%v"`, fw.message, "log me") + assert.Equal(t, sev, fw.priority) + assert.Equal(t, "log me", fw.message) } diff --git a/go/flagutil/flagutil_test.go b/go/flagutil/flagutil_test.go index 34a3d42f8a5..2502213ab73 100644 --- a/go/flagutil/flagutil_test.go +++ b/go/flagutil/flagutil_test.go @@ -21,26 +21,26 @@ import ( "testing" "github.com/spf13/pflag" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestStringList(t *testing.T) { p := StringListValue([]string{}) var _ pflag.Value = &p wanted := map[string]string{ - "0ala,ma,kota": "0ala.ma.kota", - `1ala\,ma,kota`: "1ala,ma.kota", - `2ala\\,ma,kota`: `2ala\.ma.kota`, - "3ala,": "3ala.", + "0ala,ma,kota": "0ala.ma.kota", + `1ala\,ma,kota`: "1ala,ma.kota", + `2ala\\,ma,kota`: `2ala\.ma.kota`, + "3ala,": "3ala.", } for in, out := range wanted { if err := p.Set(in); err != nil { t.Errorf("v.Set(%v): %v", in, err) continue } - assert.Equal(t, out, strings.Join(p, "."), "want %#v, got %#v", strings.Split(out, "."), p) - assert.Equal(t, in, p.String(), "v.String(): want %#v, got %#v", in, p.String()) + assert.Equal(t, out, strings.Join(p, ".")) + assert.Equal(t, in, p.String()) } } @@ -58,9 +58,9 @@ func TestEmptyStringList(t *testing.T) { } type pair struct { - in string - out map[string]string - err error + in string + out map[string]string + err error } func TestStringMap(t *testing.T) { @@ -68,16 +68,16 @@ func TestStringMap(t *testing.T) { var _ pflag.Value = &v wanted := []pair{ { - in: "tag1:value1,tag2:value2", - out: map[string]string{"tag1": "value1", "tag2": "value2"}, + in: "tag1:value1,tag2:value2", + out: map[string]string{"tag1": "value1", "tag2": "value2"}, }, { - in: `tag1:1:value1\,,tag2:value2`, - out: map[string]string{"tag1": "1:value1,", "tag2": "value2"}, + in: `tag1:1:value1\,,tag2:value2`, + out: map[string]string{"tag1": "1:value1,", "tag2": "value2"}, }, { - in: `tag1:1:value1\,,tag2`, - err: errInvalidKeyValuePair, + in: `tag1:1:value1\,,tag2`, + err: errInvalidKeyValuePair, }, } for _, want := range wanted { diff --git a/go/history/history_test.go b/go/history/history_test.go index d82138569a1..34c57756315 100644 --- a/go/history/history_test.go +++ b/go/history/history_test.go @@ -35,7 +35,7 @@ func TestHistory(t *testing.T) { t.Errorf("len(records): want %v, got %v. records: %+v", want, got, q) } for i, record := range records { - assert.Equal(t, want[i], record, "record doesn't match: want %v, got %v", want[i], record) + assert.Equal(t, want[i], record) } @@ -49,7 +49,7 @@ func TestHistory(t *testing.T) { t.Errorf("len(records): want %v, got %v. records: %+v", want, got, q) } for i, record := range records { - assert.Equal(t, want[i], record, "record doesn't match: want %v, got %v", want[i], record) + assert.Equal(t, want[i], record) } } diff --git a/go/mysql/decimal/decimal_test.go b/go/mysql/decimal/decimal_test.go index 233028f9839..894d0a6997a 100644 --- a/go/mysql/decimal/decimal_test.go +++ b/go/mysql/decimal/decimal_test.go @@ -122,7 +122,7 @@ func TestNewFromFloat(t *testing.T) { for _, x := range testTable { s := x.short d := NewFromFloat(x.float) - assert.Equal(t, s, d.String(), "expected %s, got %s (float: %v) (%s, %d)", s, d.String(), x.float, d.value.String(), d.exp) + assert.Equal(t, s, d.String()) } @@ -155,7 +155,7 @@ func TestNewFromFloatRandom(t *testing.T) { continue } got := NewFromFloat(in) - assert.True(t, want.Equal(got), "in: %v, expected %s (%s, %d), got %s (%s, %d) ", in, want.String(), want.value.String(), want.exp, got.String(), got.value.String(), got.exp) + assert.True(t, want.Equal(got)) } } @@ -189,7 +189,7 @@ func TestNewFromFloat32Random(t *testing.T) { continue } got := NewFromFloat32(in) - assert.True(t, want.Equal(got), "in: %v, expected %s (%s, %d), got %s (%s, %d) ", in, want.String(), want.value.String(), want.exp, got.String(), got.value.String(), got.exp) + assert.True(t, want.Equal(got)) } } @@ -310,8 +310,8 @@ func TestNewFromStringErrs(t *testing.T) { for s, o := range tests { out, err := NewFromString(s) - assert.Error(t, err, "error expected when parsing %s", s) - assert.Equal(t, o, out.String(), "expected %s, got %s", o, out.String()) + assert.Error(t, err) + assert.Equal(t, o, out.String()) } } @@ -341,7 +341,7 @@ func TestNewFromStringDeepEquals(t *testing.T) { if err1 != nil || err2 != nil { t.Errorf("error parsing strings to decimals") } - assert.Equal(t, cmp.expected, reflect.DeepEqual(d1, d2), "comparison result is different from expected results for %s and %s", cmp.str1, cmp.str2) + assert.Equal(t, cmp.expected, reflect.DeepEqual(d1, d2)) } } @@ -398,8 +398,7 @@ func TestNewFromInt(t *testing.T) { for input, s := range tests { d := NewFromInt(input) - assert.Equal(t, s, d.String(), "expected %s, got %s (%s, %d)", s, d.String(), - d.value.String(), d.exp) + assert.Equal(t, s, d.String()) } } @@ -482,14 +481,14 @@ func TestDecimal_RoundAndStringFixed(t *testing.T) { t.Fatal(err) } got := d.Round(test.places) - assert.True(t, got.Equal(expected), "Rounding %s to %d places, got %s, expected %s", d, test.places, got, expected) + assert.True(t, got.Equal(expected)) // test StringFixed if test.expectedFixed == "" { test.expectedFixed = test.expected } gotStr := d.StringFixed(test.places) - assert.Equal(t, test.expectedFixed, gotStr, "(%s).StringFixed(%d): got %s, expected %s", d, test.places, gotStr, test.expectedFixed) + assert.Equal(t, test.expectedFixed, gotStr) } } @@ -519,7 +518,7 @@ func TestDecimal_Add(t *testing.T) { t.FailNow() } c := a.Add(b) - assert.Equal(t, res, c.String(), "expected %s, got %s", res, c.String()) + assert.Equal(t, res, c.String()) } } @@ -553,7 +552,7 @@ func TestDecimal_Sub(t *testing.T) { t.FailNow() } c := a.sub(b) - assert.Equal(t, res, c.String(), "expected %s, got %s", res, c.String()) + assert.Equal(t, res, c.String()) } } @@ -573,7 +572,7 @@ func TestDecimal_Neg(t *testing.T) { t.FailNow() } b := a.Neg() - assert.Equal(t, res, b.String(), "expected %s, got %s", res, b.String()) + assert.Equal(t, res, b.String()) } } @@ -581,7 +580,7 @@ func TestDecimal_Neg(t *testing.T) { func TestDecimal_NegFromEmpty(t *testing.T) { a := Decimal{} b := a.Neg() - assert.Equal(t, "0", b.String(), "expected %s, got %s", "0", b) + assert.Equal(t, "0", b.String()) } @@ -609,13 +608,13 @@ func TestDecimal_Mul(t *testing.T) { t.FailNow() } c := a.mul(b) - assert.Equal(t, res, c.String(), "expected %s, got %s", res, c.String()) + assert.Equal(t, res, c.String()) } // positive scale c := New(1234, 5).mul(New(45, -1)) - assert.Equal(t, "555300000", c.String(), "Expected %s, got %s", "555300000", c.String()) + assert.Equal(t, "555300000", c.String()) } @@ -651,10 +650,10 @@ func TestDecimal_Div(t *testing.T) { } got := num.div(denom) expected, _ := NewFromString(expectedStr) - assert.True(t, got.Equal(expected), "expected %v when dividing %v by %v, got %v", expected, num, denom, got) + assert.True(t, got.Equal(expected)) got2 := num.divRound(denom, int32(divisionPrecision)) - assert.True(t, got2.Equal(expected), "expected %v on divRound (%v,%v), got %v", expected, num, denom, got2) + assert.True(t, got2.Equal(expected)) } @@ -686,7 +685,7 @@ func TestDecimal_Div(t *testing.T) { expected = "-" + expectedAbs } got := num.div(denom) - assert.Equal(t, expected, got.String(), "expected %s when dividing %v by %v, got %v", expected, num, denom, got) + assert.Equal(t, expected, got.String()) } } @@ -728,9 +727,8 @@ func TestDecimal_QuoRem(t *testing.T) { t.Errorf("bad QuoRem division %s , %s , %d got %v, %v expected %s , %s", inp4.d, inp4.d2, prec, q, r, inp4.q, inp4.r) } - assert.True(t, d.Equal(d2.mul(q).Add(r)), "not fitting: d=%v, d2= %v, prec=%d, q=%v, r=%v", d, d2, prec, q, r) - assert.True(t, q.Equal(q.Truncate(prec)), "quotient wrong precision: d=%v, d2= %v, prec=%d, q=%v, r=%v", d, d2, prec, q, r) - + assert.True(t, d.Equal(d2.mul(q).Add(r))) + assert.True(t, q.Equal(q.Truncate(prec))) if r.Abs().Cmp(d2.Abs().mul(New(1, -prec))) >= 0 { t.Errorf("remainder too large: d=%v, d2= %v, prec=%d, q=%v, r=%v", d, d2, prec, q, r) @@ -786,9 +784,9 @@ func TestDecimal_QuoRem2(t *testing.T) { q, r := d.QuoRem(d2, prec) assert. // rule 1: d = d2*q +r - True(t, d.Equal(d2.mul(q).Add(r)), "not fitting, d=%v, d2=%v, prec=%d, q=%v, r=%v", d, d2, prec, q, r) + True(t, d.Equal(d2.mul(q).Add(r))) assert.True(t, // rule 2: q is integral multiple of 10^(-prec) - q.Equal(q.Truncate(prec)), "quotient wrong precision, d=%v, d2=%v, prec=%d, q=%v, r=%v", d, d2, prec, q, r) + q.Equal(q.Truncate(prec))) // rule 3: abs(r)= 0 { @@ -852,7 +850,7 @@ func TestDecimal_DivRound(t *testing.T) { if x.Cmp(d2.Abs().mul(New(-1, -prec))) <= 0 { t.Errorf("wrong rounding, got: %v/%v prec=%d is about %v", d, d2, prec, q) } - assert.True(t, q.Equal(result), "rounded division wrong %s / %s scale %d = %s, got %v", s.d, s.d2, prec, s.result, q) + assert.True(t, q.Equal(result)) } } @@ -910,7 +908,7 @@ func TestDecimal_Mod(t *testing.T) { t.FailNow() } c := a.mod(b) - assert.Equal(t, res, c.String(), "expected %s, got %s", res, c.String()) + assert.Equal(t, res, c.String()) } } @@ -928,7 +926,7 @@ func TestDecimal_Overflow(t *testing.T) { func TestDecimal_Scale(t *testing.T) { a := New(1234, -3) - assert.Equal(t, -3, a.Exponent(), "error") + assert.Equal(t, int32(-3), a.Exponent()) } @@ -937,7 +935,7 @@ func TestDecimal_Abs1(t *testing.T) { b := New(1234, -4) c := a.Abs() - assert.Equal(t, 0, c.Cmp(b), "error") + assert.Equal(t, 0, c.Cmp(b)) } @@ -946,7 +944,7 @@ func TestDecimal_Abs2(t *testing.T) { b := New(1234, -4) c := b.Abs() - assert.NotEqual(t, 0, c.Cmp(a), "error") + assert.NotEqual(t, 0, c.Cmp(a)) } @@ -961,14 +959,14 @@ func TestDecimal_ScalesNotEqual(t *testing.T) { func TestDecimal_Cmp1(t *testing.T) { a := New(123, 3) b := New(-1234, 2) - assert.Equal(t, 1, a.Cmp(b), "Error") + assert.Equal(t, 1, a.Cmp(b)) } func TestDecimal_Cmp2(t *testing.T) { a := New(123, 3) b := New(1234, 2) - assert.Equal(t, -1, a.Cmp(b), "Error") + assert.Equal(t, -1, a.Cmp(b)) } @@ -994,19 +992,19 @@ func TestDecimal_IsInteger(t *testing.T) { if err != nil { t.Fatal(err) } - assert.Equal(t, testCase.IsInteger, d.isInteger(), "expect %t, got %t, for %s", testCase.IsInteger, d.isInteger(), testCase.Dec) + assert.Equal(t, testCase.IsInteger, d.isInteger()) } } func TestDecimal_Sign(t *testing.T) { - assert.Equal(t, 0, Zero.Sign(), "%q should have sign 0", Zero) + assert.Equal(t, 0, Zero.Sign()) one := New(1, 0) - assert.Equal(t, 1, one.Sign(), "%q should have sign 1", one) + assert.Equal(t, 1, one.Sign()) mone := New(-1, 0) - assert.Equal(t, -1, mone.Sign(), "%q should have sign -1", mone) + assert.Equal(t, -1, mone.Sign()) } From 8f9426a480643cfe4a36298653dd7a3240683261 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Fri, 16 Feb 2024 09:48:35 +0530 Subject: [PATCH 07/15] chore: remove unneeded assert messages Signed-off-by: Manik Rana --- go/cache/theine/singleflight_test.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/go/cache/theine/singleflight_test.go b/go/cache/theine/singleflight_test.go index 787280527ba..6725ac400e8 100644 --- a/go/cache/theine/singleflight_test.go +++ b/go/cache/theine/singleflight_test.go @@ -43,9 +43,8 @@ func TestDo(t *testing.T) { return "bar", nil }) - assert.Equal(t, "bar (string)", fmt.Sprintf("%v (%T)", v, v), "incorrect Do value") - assert.NoError(t, err, "got Do error") - + assert.Equal(t, "bar (string)", fmt.Sprintf("%v (%T)", v, v)) + assert.NoError(t, err) } func TestDoErr(t *testing.T) { @@ -57,7 +56,6 @@ func TestDoErr(t *testing.T) { assert.Equal(t, someErr, err) assert.Equal(t, "", v) - } func TestDoDupSuppress(t *testing.T) { @@ -87,11 +85,11 @@ func TestDoDupSuppress(t *testing.T) { defer wg2.Done() wg1.Done() v, err, _ := g.Do("key", fn) - if !assert.NoError(t, err, "unexpected Do error") { + if !assert.NoError(t, err) { return } - assert.Equal(t, "bar", v, "unexpected Do value") + assert.Equal(t, "bar", v) }() } wg1.Wait() @@ -100,7 +98,7 @@ func TestDoDupSuppress(t *testing.T) { c <- "bar" wg2.Wait() got := atomic.LoadInt32(&calls) - assert.True(t, got > 0 && got < n, "number of calls not between 0 and %d", n) + assert.True(t, got > 0 && got < n) } // Test singleflight behaves correctly after Do panic. @@ -133,7 +131,7 @@ func TestPanicDo(t *testing.T) { select { case <-done: - assert.Equal(t, int32(n), panicCount, "unexpected number of panics") + assert.Equal(t, int32(n), panicCount) case <-time.After(time.Second): require.Fail(t, "Do hangs") } From b8f5b3e5aa41052b20404b185f7ec7c13de7fce1 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Fri, 16 Feb 2024 09:51:55 +0530 Subject: [PATCH 08/15] chore: continue remove unneeded assert messages Signed-off-by: Manik Rana --- go/cache/lru_cache_test.go | 2 +- go/event/event_test.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/go/cache/lru_cache_test.go b/go/cache/lru_cache_test.go index 834333af919..12d535654ab 100644 --- a/go/cache/lru_cache_test.go +++ b/go/cache/lru_cache_test.go @@ -29,7 +29,7 @@ type CacheValue struct { func TestInitialState(t *testing.T) { cache := NewLRUCache[*CacheValue](5) l, sz, c, e, h, m := cache.Len(), cache.UsedCapacity(), cache.MaxCapacity(), cache.Evictions(), cache.Hits(), cache.Misses() - assert.Equal(t, 0, l, "length = %v, want 0", l) + assert.Equal(t, 0, l) assert.Equal(t, int64(0), sz) assert.Equal(t, int64(5), c) assert.Equal(t, int64(0), e) diff --git a/go/event/event_test.go b/go/event/event_test.go index 2f2cab9aee6..da3bc331181 100644 --- a/go/event/event_test.go +++ b/go/event/event_test.go @@ -58,7 +58,7 @@ func TestStaticListener(t *testing.T) { AddListener(func(testEvent1) { triggered = true }) AddListener(func(testEvent2) { t.Errorf("wrong listener type triggered") }) Dispatch(testEvent1{}) - assert.True(t, triggered, "static listener failed to trigger") + assert.True(t, triggered) } @@ -69,7 +69,7 @@ func TestPointerListener(t *testing.T) { AddListener(func(ev *testEvent2) { ev.triggered = true }) AddListener(func(testEvent2) { t.Errorf("non-pointer listener triggered on pointer type") }) Dispatch(testEvent) - assert.True(t, testEvent.triggered, "pointer listener failed to trigger") + assert.True(t, testEvent.triggered) } @@ -80,7 +80,7 @@ func TestInterfaceListener(t *testing.T) { AddListener(func(testInterface1) { triggered = true }) AddListener(func(testInterface2) { t.Errorf("interface listener triggered on non-matching type") }) Dispatch(testEvent1{}) - assert.True(t, triggered, "interface listener failed to trigger") + assert.True(t, triggered) } @@ -90,7 +90,7 @@ func TestEmptyInterfaceListener(t *testing.T) { triggered := false AddListener(func(any) { triggered = true }) Dispatch("this should match any") - assert.True(t, triggered, "any listener failed to trigger") + assert.True(t, triggered) } From fed3dbc7371698d90a2409ed6b7ed7f527f1b1cd Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Fri, 16 Feb 2024 09:52:50 +0530 Subject: [PATCH 09/15] lint Signed-off-by: Manik Rana --- go/cache/lru_cache_test.go | 1 - go/event/event_test.go | 3 --- 2 files changed, 4 deletions(-) diff --git a/go/cache/lru_cache_test.go b/go/cache/lru_cache_test.go index 12d535654ab..2982cc5849d 100644 --- a/go/cache/lru_cache_test.go +++ b/go/cache/lru_cache_test.go @@ -35,7 +35,6 @@ func TestInitialState(t *testing.T) { assert.Equal(t, int64(0), e) assert.Equal(t, int64(0), h) assert.Equal(t, int64(0), m) - } func TestSetInsertsValue(t *testing.T) { diff --git a/go/event/event_test.go b/go/event/event_test.go index da3bc331181..8f8fc94fc6d 100644 --- a/go/event/event_test.go +++ b/go/event/event_test.go @@ -59,7 +59,6 @@ func TestStaticListener(t *testing.T) { AddListener(func(testEvent2) { t.Errorf("wrong listener type triggered") }) Dispatch(testEvent1{}) assert.True(t, triggered) - } func TestPointerListener(t *testing.T) { @@ -70,7 +69,6 @@ func TestPointerListener(t *testing.T) { AddListener(func(testEvent2) { t.Errorf("non-pointer listener triggered on pointer type") }) Dispatch(testEvent) assert.True(t, testEvent.triggered) - } func TestInterfaceListener(t *testing.T) { @@ -81,7 +79,6 @@ func TestInterfaceListener(t *testing.T) { AddListener(func(testInterface2) { t.Errorf("interface listener triggered on non-matching type") }) Dispatch(testEvent1{}) assert.True(t, triggered) - } func TestEmptyInterfaceListener(t *testing.T) { From c7de6a8bab2733b588e2b22190c9db3dba37b4cc Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Tue, 20 Feb 2024 11:58:56 +0530 Subject: [PATCH 10/15] chore: readd messages on test failure Signed-off-by: Manik Rana --- go/event/event_test.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/go/event/event_test.go b/go/event/event_test.go index 8f8fc94fc6d..304658d63bf 100644 --- a/go/event/event_test.go +++ b/go/event/event_test.go @@ -58,7 +58,7 @@ func TestStaticListener(t *testing.T) { AddListener(func(testEvent1) { triggered = true }) AddListener(func(testEvent2) { t.Errorf("wrong listener type triggered") }) Dispatch(testEvent1{}) - assert.True(t, triggered) + assert.True(t, triggered, "static listener failed to trigger") } func TestPointerListener(t *testing.T) { @@ -68,7 +68,7 @@ func TestPointerListener(t *testing.T) { AddListener(func(ev *testEvent2) { ev.triggered = true }) AddListener(func(testEvent2) { t.Errorf("non-pointer listener triggered on pointer type") }) Dispatch(testEvent) - assert.True(t, testEvent.triggered) + assert.True(t, testEvent.triggered, "pointer listener failed to trigger") } func TestInterfaceListener(t *testing.T) { @@ -78,7 +78,7 @@ func TestInterfaceListener(t *testing.T) { AddListener(func(testInterface1) { triggered = true }) AddListener(func(testInterface2) { t.Errorf("interface listener triggered on non-matching type") }) Dispatch(testEvent1{}) - assert.True(t, triggered) + assert.True(t, triggered, "interface listener failed to trigger") } func TestEmptyInterfaceListener(t *testing.T) { @@ -87,8 +87,7 @@ func TestEmptyInterfaceListener(t *testing.T) { triggered := false AddListener(func(any) { triggered = true }) Dispatch("this should match any") - assert.True(t, triggered) - + assert.True(t, triggered, "empty listener failed to trigger") } func TestMultipleListeners(t *testing.T) { @@ -176,7 +175,7 @@ func TestDispatchPointerToValueInterfaceListener(t *testing.T) { triggered = true }) Dispatch(&testEvent1{}) - assert.True(t, triggered) + assert.True(t, triggered, "Dispatch by pointer failed to trigger interface listener") } @@ -188,8 +187,7 @@ func TestDispatchValueToValueInterfaceListener(t *testing.T) { triggered = true }) Dispatch(testEvent1{}) - assert.True(t, triggered) - + assert.True(t, triggered, "Dispatch by value failed to trigger interface listener") } func TestDispatchPointerToPointerInterfaceListener(t *testing.T) { @@ -198,7 +196,7 @@ func TestDispatchPointerToPointerInterfaceListener(t *testing.T) { triggered := false AddListener(func(testInterface2) { triggered = true }) Dispatch(&testEvent2{}) - assert.True(t, triggered) + assert.True(t, triggered, "interface listener failed to trigger for pointer") } From d9d6bda8afb8253818624506d637d617bd0f0c19 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Tue, 20 Feb 2024 12:03:45 +0530 Subject: [PATCH 11/15] refac: use proper assertion methods Signed-off-by: Manik Rana --- go/cache/theine/singleflight_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/go/cache/theine/singleflight_test.go b/go/cache/theine/singleflight_test.go index 6725ac400e8..24fe1e193f2 100644 --- a/go/cache/theine/singleflight_test.go +++ b/go/cache/theine/singleflight_test.go @@ -54,8 +54,8 @@ func TestDoErr(t *testing.T) { return "", someErr }) - assert.Equal(t, someErr, err) - assert.Equal(t, "", v) + assert.ErrorIs(t, err, someErr, "incorrect Do error") + assert.Empty(t, v, "unexpected non-empty value") } func TestDoDupSuppress(t *testing.T) { @@ -98,7 +98,8 @@ func TestDoDupSuppress(t *testing.T) { c <- "bar" wg2.Wait() got := atomic.LoadInt32(&calls) - assert.True(t, got > 0 && got < n) + assert.Greater(t, got, int32(0)) + assert.Less(t, got, int32(n)) } // Test singleflight behaves correctly after Do panic. From bc8b48cff42616052f8fd4ef33fccd508a9d6319 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 21 Feb 2024 11:26:12 +0530 Subject: [PATCH 12/15] chore: format tests Signed-off-by: Manik Rana --- go/event/event_test.go | 2 +- go/mysql/decimal/decimal_test.go | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/go/event/event_test.go b/go/event/event_test.go index 304658d63bf..cdca98abd85 100644 --- a/go/event/event_test.go +++ b/go/event/event_test.go @@ -227,7 +227,7 @@ func TestDispatchUpdate(t *testing.T) { ev := &testUpdateEvent{} DispatchUpdate(ev, "hello") - assert.True(t, triggered) + assert.True(t, triggered, "listener failed to trigger on DispatchUpdate()") want := "hello" if got := ev.update.(string); got != want { diff --git a/go/mysql/decimal/decimal_test.go b/go/mysql/decimal/decimal_test.go index 894d0a6997a..e1a09930e12 100644 --- a/go/mysql/decimal/decimal_test.go +++ b/go/mysql/decimal/decimal_test.go @@ -782,11 +782,10 @@ func TestDecimal_QuoRem2(t *testing.T) { d2 := tc.d2 prec := tc.prec q, r := d.QuoRem(d2, prec) - assert. - // rule 1: d = d2*q +r - True(t, d.Equal(d2.mul(q).Add(r))) - assert.True(t, // rule 2: q is integral multiple of 10^(-prec) - q.Equal(q.Truncate(prec))) + // rule 1: d = d2*q +r + assert.True(t, d.Equal(d2.mul(q).Add(r))) + // rule 2: q is integral multiple of 10^(-prec) + assert.True(t, q.Equal(q.Truncate(prec))) // rule 3: abs(r)= 0 { From 3e9e55a2d1bfd1c82b366c396566d522910d94a1 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 21 Feb 2024 12:59:59 +0530 Subject: [PATCH 13/15] refac: use assert.EqualValues instead of equal Signed-off-by: Manik Rana --- go/cache/lru_cache_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/go/cache/lru_cache_test.go b/go/cache/lru_cache_test.go index 2982cc5849d..7b86cdc222b 100644 --- a/go/cache/lru_cache_test.go +++ b/go/cache/lru_cache_test.go @@ -30,11 +30,11 @@ func TestInitialState(t *testing.T) { cache := NewLRUCache[*CacheValue](5) l, sz, c, e, h, m := cache.Len(), cache.UsedCapacity(), cache.MaxCapacity(), cache.Evictions(), cache.Hits(), cache.Misses() assert.Equal(t, 0, l) - assert.Equal(t, int64(0), sz) - assert.Equal(t, int64(5), c) - assert.Equal(t, int64(0), e) - assert.Equal(t, int64(0), h) - assert.Equal(t, int64(0), m) + assert.EqualValues(t, 0, sz) + assert.EqualValues(t, 5, c) + assert.EqualValues(t, 0, e) + assert.EqualValues(t, 0, h) + assert.EqualValues(t, 0, m) } func TestSetInsertsValue(t *testing.T) { From 303c47e57dda44506d18144d999d46b30873f5ae Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 21 Feb 2024 13:01:13 +0530 Subject: [PATCH 14/15] refac: use assert.Zero/NotZero Signed-off-by: Manik Rana --- go/mysql/decimal/decimal_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go/mysql/decimal/decimal_test.go b/go/mysql/decimal/decimal_test.go index e1a09930e12..2142a720895 100644 --- a/go/mysql/decimal/decimal_test.go +++ b/go/mysql/decimal/decimal_test.go @@ -943,7 +943,7 @@ func TestDecimal_Abs2(t *testing.T) { b := New(1234, -4) c := b.Abs() - assert.NotEqual(t, 0, c.Cmp(a)) + assert.NotZero(t, c.Cmp(a)) } @@ -997,7 +997,7 @@ func TestDecimal_IsInteger(t *testing.T) { } func TestDecimal_Sign(t *testing.T) { - assert.Equal(t, 0, Zero.Sign()) + assert.Zero(t, Zero.Sign()) one := New(1, 0) assert.Equal(t, 1, one.Sign()) From faadfce558c905c749c9b68630f9ee377ec2436f Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 21 Feb 2024 13:05:04 +0530 Subject: [PATCH 15/15] refac: replace assert.equal with appropriate assertions Signed-off-by: Manik Rana --- go/cache/lru_cache_test.go | 4 ++-- go/cache/theine/singleflight_test.go | 2 +- go/mysql/decimal/decimal_test.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/go/cache/lru_cache_test.go b/go/cache/lru_cache_test.go index 7b86cdc222b..af9db72852e 100644 --- a/go/cache/lru_cache_test.go +++ b/go/cache/lru_cache_test.go @@ -29,7 +29,7 @@ type CacheValue struct { func TestInitialState(t *testing.T) { cache := NewLRUCache[*CacheValue](5) l, sz, c, e, h, m := cache.Len(), cache.UsedCapacity(), cache.MaxCapacity(), cache.Evictions(), cache.Hits(), cache.Misses() - assert.Equal(t, 0, l) + assert.Zero(t, l) assert.EqualValues(t, 0, sz) assert.EqualValues(t, 5, c) assert.EqualValues(t, 0, e) @@ -128,7 +128,7 @@ func TestCapacityIsObeyed(t *testing.T) { cache.Set("key4", value) sz, evictions := cache.UsedCapacity(), cache.Evictions() assert.Equal(t, size, sz) - assert.Equal(t, int64(1), evictions) + assert.EqualValues(t, 1, evictions) // Check various other stats if l := cache.Len(); int64(l) != size { diff --git a/go/cache/theine/singleflight_test.go b/go/cache/theine/singleflight_test.go index 24fe1e193f2..bf5018a8891 100644 --- a/go/cache/theine/singleflight_test.go +++ b/go/cache/theine/singleflight_test.go @@ -132,7 +132,7 @@ func TestPanicDo(t *testing.T) { select { case <-done: - assert.Equal(t, int32(n), panicCount) + assert.EqualValues(t, n, panicCount) case <-time.After(time.Second): require.Fail(t, "Do hangs") } diff --git a/go/mysql/decimal/decimal_test.go b/go/mysql/decimal/decimal_test.go index 2142a720895..3499ab8f802 100644 --- a/go/mysql/decimal/decimal_test.go +++ b/go/mysql/decimal/decimal_test.go @@ -925,7 +925,7 @@ func TestDecimal_Overflow(t *testing.T) { func TestDecimal_Scale(t *testing.T) { a := New(1234, -3) - assert.Equal(t, int32(-3), a.Exponent()) + assert.EqualValues(t, -3, a.Exponent()) } @@ -934,7 +934,7 @@ func TestDecimal_Abs1(t *testing.T) { b := New(1234, -4) c := a.Abs() - assert.Equal(t, 0, c.Cmp(b)) + assert.Zero(t, c.Cmp(b)) }