Skip to content

Commit

Permalink
Test suite, code linting: rules 'bool-compare,compares,nil-compare,ne…
Browse files Browse the repository at this point in the history
…gative-positive'
  • Loading branch information
deining committed Jan 15, 2025
1 parent 1101fef commit 6530b66
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 49 deletions.
2 changes: 1 addition & 1 deletion canvas/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestText_MinSize_NoMultiLine(t *testing.T) {
text = canvas.NewText("Bre\nak", color.NRGBA{0, 0, 0, 0xff})
min2 := text.MinSize()
assert.Greater(t, min2.Width, min.Width)
assert.True(t, min2.Height == min.Height)
assert.Equal(t, min2.Height, min.Height)
}

func TestText_Layout(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/fyne/internal/commands/mock_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ func (t *testCommandRuns) verifyExpectation() {

for _, value := range t.runs {
if value.dir != nil {
assert.Equal(t.t, true, value.dirSet)
assert.True(t.t, value.dirSet)
}
if len(value.env) > 0 {
assert.Equal(t.t, true, value.envSet)
assert.True(t.t, value.envSet)
}
}
}
8 changes: 4 additions & 4 deletions cmd/fyne/internal/util/mobile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ func Test_IsMobile(t *testing.T) {
osList := []string{"darwin", "linux", "windows", "android/arm", "android/arm64", "android/amd64", "android/386", "android", "ios"}
osIsMobile := []bool{false, false, false, true, true, true, true, true, true}

assert.Equal(t, false, IsMobile(""))
assert.False(t, IsMobile(""))

for i, os := range osList {
assert.Equal(t, IsMobile(os), osIsMobile[i])
}

assert.Equal(t, false, IsMobile("amiga"))
assert.False(t, IsMobile("amiga"))
}

func Test_IsAndroid(t *testing.T) {
osList := []string{"darwin", "linux", "windows", "android/arm", "android/arm64", "android/amd64", "android/386", "android", "ios"}
osIsMobile := []bool{false, false, false, true, true, true, true, true, false}

assert.Equal(t, false, IsAndroid(""))
assert.False(t, IsAndroid(""))

for i, os := range osList {
assert.Equal(t, IsAndroid(os), osIsMobile[i])
}

assert.Equal(t, false, IsAndroid("amiga"))
assert.False(t, IsAndroid("amiga"))
}
2 changes: 1 addition & 1 deletion data/binding/binditems_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestNewURI(t *testing.T) {
f := NewURI()
v, err := f.Get()
require.NoError(t, err)
assert.Equal(t, nil, v)
assert.Nil(t, v)

err = f.Set(storage.NewFileURI("/var"))
require.NoError(t, err)
Expand Down
14 changes: 7 additions & 7 deletions data/binding/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestBoolToString(t *testing.T) {
require.NoError(t, err)
v2, err := b.Get()
require.NoError(t, err)
assert.Equal(t, false, v2)
assert.False(t, v2)
}

func TestBoolToStringWithFormat(t *testing.T) {
Expand All @@ -56,7 +56,7 @@ func TestBoolToStringWithFormat(t *testing.T) {
require.NoError(t, err)
v2, err := b.Get()
require.NoError(t, err)
assert.Equal(t, false, v2)
assert.False(t, v2)
}

func TestFloatToString(t *testing.T) {
Expand Down Expand Up @@ -164,13 +164,13 @@ func TestStringToBool(t *testing.T) {
b := StringToBool(s)
v, err := b.Get()
require.NoError(t, err)
assert.Equal(t, false, v)
assert.False(t, v)

err = s.Set("true")
require.NoError(t, err)
v, err = b.Get()
require.NoError(t, err)
assert.Equal(t, true, v)
assert.True(t, v)

err = s.Set("trap") // bug in fmt.SScanf means "wrong" parses as "false"
require.NoError(t, err)
Expand All @@ -190,13 +190,13 @@ func TestStringToBoolWithFormat(t *testing.T) {
b := StringToBoolWithFormat(s, "%tly")
v, err := b.Get()
require.NoError(t, err)
assert.Equal(t, false, v)
assert.False(t, v)

err = s.Set("truely")
require.NoError(t, err)
v, err = b.Get()
require.NoError(t, err)
assert.Equal(t, true, v)
assert.True(t, v)

err = s.Set("true") // valid bool but not valid format
require.NoError(t, err)
Expand Down Expand Up @@ -317,7 +317,7 @@ func TestStringToURI(t *testing.T) {
u := StringToURI(s)
v, err := u.Get()
require.NoError(t, err)
assert.Equal(t, nil, v)
assert.Nil(t, v)

err = s.Set("file:///tmp/test.txt")
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion data/binding/mapbinding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func TestUntypedMap_Delete(t *testing.T) {
assert.Len(t, b.Keys(), 1)
v, err = b.GetValue("foo")
require.Error(t, err)
assert.Equal(t, nil, v)
assert.Nil(t, v)
v, err = b.GetValue("val")
require.NoError(t, err)
assert.Equal(t, 5, v)
Expand Down
2 changes: 1 addition & 1 deletion data/binding/sprintf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestSprintfConversionReadWrite(t *testing.T) {
err = sp.Set("Bool false , Float 7.000000 , Int 42 , Rune 67 , String nospacestring , URI file:///var/")
require.NoError(t, err)

assert.Equal(t, false, b)
assert.False(t, b)
assert.Equal(t, float64(7), f)
assert.Equal(t, 42, i)
assert.Equal(t, 'C', r)
Expand Down
4 changes: 2 additions & 2 deletions internal/async/goroutine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestGoroutineID(t *testing.T) {
testID2 := goroutineID()

assert.Equal(t, testID1, testID2)
assert.Greater(t, childID1, uint64(0))
assert.Positive(t, childID1)
assert.NotEqual(t, testID1, childID1)
assert.Greater(t, childID2, uint64(0))
assert.Positive(t, childID2)
assert.NotEqual(t, childID1, childID2)
}
20 changes: 10 additions & 10 deletions internal/cache/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func TestCacheClean(t *testing.T) {
t.Run("no_expired_objects", func(t *testing.T) {
lastClean = tm.createTime(10, 20)
Clean(false)
assert.Len(t, svgs, 40)
assert.Equal(t, 40, svgs.Len())
assert.Equal(t, 40, renderers.Len())
assert.Len(t, canvases, 40)
assert.Zero(t, destroyedRenderersCnt)
assert.Equal(t, tm.now, lastClean)

tm.setTime(10, 30)
Clean(true)
assert.Equal(t, svgs.Len(), 40)
assert.Equal(t, 40, svgs.Len())
assert.Equal(t, 40, renderers.Len())
assert.Len(t, canvases, 40)
assert.Zero(t, destroyedRenderersCnt)
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestCacheClean(t *testing.T) {
Clean(true)
assert.Equal(t, tm.now, lastClean)

assert.Len(t, svgs, 40)
assert.Equal(t, 40, svgs.Len())
assert.Equal(t, 40, renderers.Len())
assert.Len(t, canvases, 40)
assert.Zero(t, destroyedRenderersCnt)
Expand All @@ -81,14 +81,14 @@ func TestCacheClean(t *testing.T) {
lastClean = tm.createTime(10, 11)
tm.setTime(11, 12)
Clean(false)
assert.Len(t, svgs, 20)
assert.Equal(t, 20, svgs.Len())
assert.Equal(t, 40, renderers.Len())
assert.Len(t, canvases, 40)
assert.Zero(t, destroyedRenderersCnt)

tm.setTime(11, 42)
Clean(false)
assert.Len(t, svgs, 0)
assert.Equal(t, 0, svgs.Len())
assert.Equal(t, 40, renderers.Len())
assert.Len(t, canvases, 40)
assert.Zero(t, destroyedRenderersCnt)
Expand All @@ -98,15 +98,15 @@ func TestCacheClean(t *testing.T) {
lastClean = tm.createTime(10, 11)
tm.setTime(11, 11)
Clean(true)
assert.Len(t, svgs, 0)
assert.Equal(t, 0, svgs.Len())
assert.Equal(t, 20, renderers.Len())
assert.Len(t, canvases, 20)
assert.Equal(t, 20, destroyedRenderersCnt)

tm.setTime(11, 22)
Clean(true)
assert.Len(t, svgs, 0)
assert.Empty(t, renderers)
assert.Equal(t, 0, svgs.Len())
assert.Equal(t, 0, renderers.Len())
assert.Empty(t, canvases)
assert.Equal(t, 40, destroyedRenderersCnt)
})
Expand All @@ -131,7 +131,7 @@ func TestCacheClean(t *testing.T) {
Clean(false)
assert.False(t, skippedCleanWithCanvasRefresh)
assert.Equal(t, tm.now, lastClean)
assert.Empty(t, renderers)
assert.Equal(t, 0, renderers.Len())
})
}

Expand Down Expand Up @@ -170,7 +170,7 @@ func TestCleanCanvas(t *testing.T) {
}

CleanCanvas(dcanvas2)
assert.Empty(t, renderers)
assert.Equal(t, 0, renderers.Len())
assert.Empty(t, canvases)
assert.Equal(t, 42, destroyedRenderersCnt)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cache/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestTextCacheGet(t *testing.T) {
assert.Equal(t, float32(0), base)

SetFontMetrics("hi", 10, fyne.TextStyle{}, nil, fyne.NewSize(10, 10), 8)
assert.Len(t, fontSizeCache, 1)
assert.Equal(t, 1, fontSizeCache.Len())

bound, base = GetFontMetrics("hi", 10, fyne.TextStyle{}, nil)
assert.Equal(t, fyne.NewSize(10, 10), bound)
Expand Down
6 changes: 3 additions & 3 deletions internal/driver/glfw/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
func Test_Device(t *testing.T) {
dev := &glDevice{}

assert.Equal(t, false, dev.IsMobile())
assert.False(t, dev.IsMobile())
assert.Equal(t, fyne.OrientationHorizontalLeft, dev.Orientation())
assert.Equal(t, true, dev.HasKeyboard())
assert.Equal(t, false, dev.IsBrowser())
assert.True(t, dev.HasKeyboard())
assert.False(t, dev.IsBrowser())
}
6 changes: 3 additions & 3 deletions internal/driver/mobile/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func Test_Device(t *testing.T) {
dev := &device{}

assert.Equal(t, true, dev.IsMobile())
assert.Equal(t, false, dev.IsBrowser())
assert.Equal(t, false, dev.HasKeyboard())
assert.True(t, dev.IsMobile())
assert.False(t, dev.IsBrowser())
assert.False(t, dev.HasKeyboard())
}
12 changes: 6 additions & 6 deletions internal/repository/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@ func TestHTTPRepositoryExists(t *testing.T) {

exists, err := storage.Exists(resExists)
require.NoError(t, err)
assert.Equal(t, true, exists)
assert.True(t, exists)

// test a valid host with an invalid path
resNotExists, err := storage.ParseURI(validHost + invalidPath)
require.NoError(t, err)

exists, err = storage.Exists(resNotExists)
require.NoError(t, err)
assert.Equal(t, false, exists)
assert.False(t, exists)

// test an invalid host
resInvalid, err := storage.ParseURI(invalidHost + invalidPath)
require.NoError(t, err)

exists, err = storage.Exists(resInvalid)
require.Error(t, err)
assert.Equal(t, false, exists)
assert.False(t, exists)
}

func TestHTTPRepositoryReader(t *testing.T) {
Expand Down Expand Up @@ -161,21 +161,21 @@ func TestHTTPRepositoryCanRead(t *testing.T) {

canRead, err := storage.CanRead(resExists)
require.NoError(t, err)
assert.Equal(t, true, canRead)
assert.True(t, canRead)

// test a invalid url for a valid host
resNotExists, err := storage.ParseURI(validHost + invalidPath)
require.NoError(t, err)

canRead, err = storage.CanRead(resNotExists)
require.Error(t, err)
assert.Equal(t, false, canRead)
assert.False(t, canRead)

// test a invalid host
resInvalid, err := storage.ParseURI(invalidHost)
require.NoError(t, err)

canRead, err = storage.CanRead(resInvalid)
require.Error(t, err)
assert.Equal(t, false, canRead)
assert.False(t, canRead)
}
4 changes: 2 additions & 2 deletions internal/repository/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestInMemoryRepositoryRegistration(t *testing.T) {
// test that re-registration also works
m2 := NewInMemoryRepository("mem")
repository.Register("mem", m2)
assert.False(t, m == m2) // this is explicitly intended to be pointer comparison
assert.NotSame(t, m, m2) // this is explicitly intended to be pointer comparison
repo, err = repository.ForURI(foo)
require.NoError(t, err)
assert.Equal(t, m2, repo)
Expand All @@ -43,7 +43,7 @@ func TestInMemoryRepositoryParsingWithEmptyList(t *testing.T) {

canList, err := storage.CanList(foo)
require.Error(t, err)
assert.Equal(t, false, canList)
assert.False(t, canList)

listing, err := storage.List(foo)
require.NoError(t, err)
Expand Down
9 changes: 4 additions & 5 deletions widget/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"fyne.io/fyne/v2/widget"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestEntry_Binding(t *testing.T) {
Expand All @@ -32,13 +31,13 @@ func TestEntry_Binding(t *testing.T) {
assert.Equal(t, "", entry.Text)

err := str.Set("Updated")
require.NoError(t, err)
assert.NoError(t, err)
waitForBinding()
assert.Equal(t, "Updated", entry.Text)

entry.SetText("Typed")
v, err := str.Get()
require.NoError(t, err)
assert.NoError(t, err)
assert.Equal(t, "Typed", v)

entry.Unbind()
Expand Down Expand Up @@ -1806,15 +1805,15 @@ func TestMultiLineEntry_MinSize(t *testing.T) {
func TestNewEntryWithData(t *testing.T) {
str := binding.NewString()
err := str.Set("Init")
require.NoError(t, err)
assert.NoError(t, err)

entry := widget.NewEntryWithData(str)
waitForBinding()
assert.Equal(t, "Init", entry.Text)

entry.SetText("Typed")
v, err := str.Get()
require.NoError(t, err)
assert.NoError(t, err)
assert.Equal(t, "Typed", v)
}

Expand Down

0 comments on commit 6530b66

Please sign in to comment.