From c1674d38df317f90cda88c9098812e827284c23e Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Thu, 2 May 2024 23:09:14 -0500 Subject: [PATCH] test: Refactor tests to use testify --- go.mod | 2 ++ go.sum | 1 + internal/nightscout/delta_test.go | 5 ++-- internal/nightscout/fetch_test.go | 26 ++++++++------------ internal/nightscout/mills_test.go | 34 ++++++++++---------------- internal/nightscout/properties_test.go | 6 ++--- internal/nightscout/reading_test.go | 26 ++++++++------------ internal/util/bg_test.go | 10 +++++--- internal/util/date_test.go | 10 +++----- 9 files changed, 51 insertions(+), 69 deletions(-) diff --git a/go.mod b/go.mod index 70bfc5c..9276adc 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/pelletier/go-toml/v2 v2.2.2 github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 github.com/spf13/pflag v1.0.5 + github.com/stretchr/testify v1.9.0 ) require ( @@ -42,4 +43,5 @@ require ( golang.org/x/crypto v0.17.0 // indirect golang.org/x/image v0.15.0 // indirect golang.org/x/sys v0.17.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 0e8584f..a3cb926 100644 --- a/go.sum +++ b/go.sum @@ -117,6 +117,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= diff --git a/internal/nightscout/delta_test.go b/internal/nightscout/delta_test.go index 39f404c..f663e00 100644 --- a/internal/nightscout/delta_test.go +++ b/internal/nightscout/delta_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/gabe565/nightscout-menu-bar/internal/config" + "github.com/stretchr/testify/assert" ) func TestDelta_Display(t *testing.T) { @@ -63,9 +64,7 @@ func TestDelta_Display(t *testing.T) { Scaled: tt.fields.Scaled, Times: tt.fields.Times, } - if got := d.Display(); got != tt.want { - t.Errorf("Display() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, d.Display()) }) } } diff --git a/internal/nightscout/fetch_test.go b/internal/nightscout/fetch_test.go index 9d530d1..8f2ecf7 100644 --- a/internal/nightscout/fetch_test.go +++ b/internal/nightscout/fetch_test.go @@ -4,12 +4,13 @@ import ( _ "embed" "net/http" "net/http/httptest" - "reflect" "testing" "time" "github.com/gabe565/nightscout-menu-bar/internal/config" "github.com/hhsnopek/etag" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) //go:embed fetch_test_properties.json @@ -41,12 +42,12 @@ func TestFetch(t *testing.T) { etag string want *Properties wantEtag string - wantErr bool + wantErr require.ErrorAssertionFunc }{ - {"no url", "", "", nil, "", true}, - {"success", server.URL, "", properties, propertiesEtag, false}, - {"same etag", server.URL, propertiesEtag, nil, propertiesEtag, true}, - {"different etag", server.URL, differentEtag, properties, propertiesEtag, false}, + {"no url", "", "", nil, "", require.Error}, + {"success", server.URL, "", properties, propertiesEtag, require.NoError}, + {"same etag", server.URL, propertiesEtag, nil, propertiesEtag, require.Error}, + {"different etag", server.URL, differentEtag, properties, propertiesEtag, require.NoError}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -54,16 +55,9 @@ func TestFetch(t *testing.T) { lastEtag = tt.etag got, err := Fetch() - if (err != nil) != tt.wantErr { - t.Errorf("Fetch() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Fetch() got = %v, want %v", got, tt.want) - } - if !reflect.DeepEqual(lastEtag, tt.wantEtag) { - t.Errorf("Fetch() got etag = %v, want etag %v", lastEtag, tt.wantEtag) - } + tt.wantErr(t, err) + assert.Equal(t, tt.want, got) + assert.Equal(t, tt.wantEtag, lastEtag) }) } } diff --git a/internal/nightscout/mills_test.go b/internal/nightscout/mills_test.go index e98daf0..ba08f38 100644 --- a/internal/nightscout/mills_test.go +++ b/internal/nightscout/mills_test.go @@ -1,10 +1,12 @@ package nightscout import ( - "reflect" "strconv" "testing" "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestMills_MarshalJSON(t *testing.T) { @@ -20,10 +22,10 @@ func TestMills_MarshalJSON(t *testing.T) { name string fields fields want []byte - wantErr bool + wantErr require.ErrorAssertionFunc }{ - {"0", fields{unix0}, []byte("0"), false}, - {"now", fields{now}, []byte(nowStr), false}, + {"0", fields{unix0}, []byte("0"), require.NoError}, + {"now", fields{now}, []byte(nowStr), require.NoError}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -31,13 +33,8 @@ func TestMills_MarshalJSON(t *testing.T) { Time: tt.fields.Time, } got, err := m.MarshalJSON() - if (err != nil) != tt.wantErr { - t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want) - } + tt.wantErr(t, err) + assert.Equal(t, tt.want, got) }) } } @@ -52,21 +49,16 @@ func TestMills_UnmarshalJSON(t *testing.T) { name string args args want Mills - wantErr bool + wantErr require.ErrorAssertionFunc }{ - {"now", args{[]byte(strconv.Itoa(int(now.UnixMilli())))}, Mills{now}, false}, - {"error", args{[]byte("a")}, Mills{time.Time{}}, true}, + {"now", args{[]byte(strconv.Itoa(int(now.UnixMilli())))}, Mills{now}, require.NoError}, + {"error", args{[]byte("a")}, Mills{time.Time{}}, require.Error}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var m Mills - if err := m.UnmarshalJSON(tt.args.bytes); (err != nil) != tt.wantErr { - t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(m, tt.want) { - t.Errorf("UnmarshalJSON() got = %v, want %v", m, tt.want) - } + tt.wantErr(t, m.UnmarshalJSON(tt.args.bytes)) + assert.Equal(t, tt.want, m) }) } } diff --git a/internal/nightscout/properties_test.go b/internal/nightscout/properties_test.go index 527d57b..617a6df 100644 --- a/internal/nightscout/properties_test.go +++ b/internal/nightscout/properties_test.go @@ -3,6 +3,8 @@ package nightscout import ( "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestProperties_String(t *testing.T) { @@ -38,9 +40,7 @@ func TestProperties_String(t *testing.T) { Delta: tt.fields.Delta, Direction: tt.fields.Direction, } - if got := p.String(); got != tt.want { - t.Errorf("String() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, p.String()) }) } } diff --git a/internal/nightscout/reading_test.go b/internal/nightscout/reading_test.go index 01f3b29..915c25b 100644 --- a/internal/nightscout/reading_test.go +++ b/internal/nightscout/reading_test.go @@ -6,6 +6,8 @@ import ( "time" "github.com/gabe565/nightscout-menu-bar/internal/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestReading_Arrow(t *testing.T) { @@ -45,9 +47,7 @@ func TestReading_Arrow(t *testing.T) { ToMills: tt.fields.ToMills, Sgvs: tt.fields.Sgvs, } - if got := r.Arrow(); got != tt.want { - t.Errorf("Arrow() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, r.Arrow()) }) } } @@ -88,9 +88,7 @@ func TestReading_String(t *testing.T) { ToMills: tt.fields.ToMills, Sgvs: tt.fields.Sgvs, } - if got := r.String(); got != tt.want { - t.Errorf("String() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, r.String()) }) } } @@ -140,9 +138,7 @@ func TestReading_DisplayBg(t *testing.T) { ToMills: tt.fields.ToMills, Sgvs: tt.fields.Sgvs, } - if got := r.DisplayBg(); got != tt.want { - t.Errorf("DisplayBg() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, r.DisplayBg()) }) } } @@ -205,25 +201,25 @@ func TestReading_UnmarshalJSON(t *testing.T) { name string fields fields args args - wantErr bool + wantErr require.ErrorAssertionFunc }{ { "simple", fields{}, args{[]byte(fmt.Sprintf(normalReading, now.UnixMilli(), now.UnixMilli()))}, - false, + require.NoError, }, { "low", fields{}, args{[]byte(fmt.Sprintf(lowReading, now.UnixMilli()))}, - false, + require.NoError, }, { "error", fields{}, args{[]byte("{")}, - true, + require.Error, }, } for _, tt := range tests { @@ -237,9 +233,7 @@ func TestReading_UnmarshalJSON(t *testing.T) { ToMills: tt.fields.ToMills, Sgvs: tt.fields.Sgvs, } - if err := r.UnmarshalJSON(tt.args.bytes); (err != nil) != tt.wantErr { - t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) - } + tt.wantErr(t, r.UnmarshalJSON(tt.args.bytes)) }) } } diff --git a/internal/util/bg_test.go b/internal/util/bg_test.go index 5efc2a1..b717a85 100644 --- a/internal/util/bg_test.go +++ b/internal/util/bg_test.go @@ -1,6 +1,10 @@ package util -import "testing" +import ( + "testing" + + "github.com/stretchr/testify/assert" +) func TestToMmol(t *testing.T) { type args struct { @@ -17,9 +21,7 @@ func TestToMmol(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := ToMmol(tt.args.mgdl); got != tt.want { - t.Errorf("ToMmol() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, ToMmol(tt.args.mgdl)) }) } } diff --git a/internal/util/date_test.go b/internal/util/date_test.go index c18410e..ede4d85 100644 --- a/internal/util/date_test.go +++ b/internal/util/date_test.go @@ -3,6 +3,8 @@ package util import ( "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestMinAgo(t *testing.T) { @@ -25,9 +27,7 @@ func TestMinAgo(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := MinAgo(tt.args.date); got != tt.want { - t.Errorf("MinAgo() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, MinAgo(tt.args.date)) }) } } @@ -56,9 +56,7 @@ func TestGetNextMinChange(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.want = tt.want.Round(time.Second) got := GetNextMinChange(tt.args.t).Round(time.Second) - if got != tt.want { - t.Errorf("GetNextMinChange() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, got) }) } }