Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions v2/debounce_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package debounce_test

import (
"slices"
"testing"
"time"

"github.com/floatdrop/debounce/v2"

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

// helper to collect output with a timeout
Expand Down Expand Up @@ -42,8 +41,11 @@ func TestDebounce_LastValueOnly(t *testing.T) {
close(in)
}()

expected := []int{4}
result := collect(out, 1*time.Second)
assert.Equal(t, []int{4}, result)
if !slices.Equal(expected, result) {
t.Errorf("expected result = %v, got %v", expected, result)
}
}

func TestDebounce_MultipleValuesSpacedOut(t *testing.T) {
Expand All @@ -60,8 +62,11 @@ func TestDebounce_MultipleValuesSpacedOut(t *testing.T) {
close(in)
}()

expected := []int{1, 2, 3}
result := collect(out, 1*time.Second)
assert.Equal(t, []int{1, 2, 3}, result)
if !slices.Equal(expected, result) {
t.Errorf("expected result = %v, got %v", expected, result)
}
}

func TestDebounce_WithLimit(t *testing.T) {
Expand All @@ -80,8 +85,11 @@ func TestDebounce_WithLimit(t *testing.T) {
close(in)
}()

expected := []int{3, 4}
result := collect(out, 1*time.Second)
assert.Equal(t, []int{3, 4}, result)
if !slices.Equal(expected, result) {
t.Errorf("expected result = %v, got %v", expected, result)
}
}

func TestDebounce_ChannelCloses(t *testing.T) {
Expand All @@ -93,8 +101,11 @@ func TestDebounce_ChannelCloses(t *testing.T) {
close(in)
}()

expected := []int{42}
result := collect(out, 1*time.Second)
assert.Equal(t, []int{42}, result)
if !slices.Equal(expected, result) {
t.Errorf("expected result = %v, got %v", expected, result)
}
}

func TestDebounce_EmptyChannelCloses(t *testing.T) {
Expand All @@ -105,14 +116,19 @@ func TestDebounce_EmptyChannelCloses(t *testing.T) {
close(in)
}()

expected := []int(nil)
result := collect(out, 1*time.Second)
assert.Equal(t, []int(nil), result)
if !slices.Equal(expected, result) {
t.Errorf("expected result = %v, got %v", expected, result)
}
}

func TestDebounce_ZeroDelay(t *testing.T) {
in := make(chan int)
out := debounce.Chan(in)
assert.Equal(t, (<-chan int)(in), out)
if (<-chan int)(in) != out {
t.Errorf("expected result = %v, got %v", (<-chan int)(in), out)
}
}

func BenchmarkDebounce_Insert(b *testing.B) {
Expand Down
8 changes: 0 additions & 8 deletions v2/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
module github.com/floatdrop/debounce/v2

go 1.23

require github.com/stretchr/testify v1.10.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 0 additions & 10 deletions v2/go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
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.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading