Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
slices: update Int.Scan() to parse empty slices
Previously, Int.Scan() returned the slice '[0]' when reading an empty slice of ints from the database. This is a bug, as the output should also be empty. This patch fixes the bug and adds regression tests. Additionally, the code is updated to percolate up the errors from integer to string conversion, which was previously swallowed.
- Loading branch information
Showing
2 changed files
with
49 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package slices | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Int_Scan(t *testing.T) { | ||
r := require.New(t) | ||
t.Run("empty slice", func(t *testing.T) { | ||
in := "{}" | ||
v := &Int{} | ||
r.NoError(v.Scan(in)) | ||
r.Len(*v, 0) | ||
}) | ||
|
||
t.Run("non-empty slice", func(t *testing.T) { | ||
in := "{44,55}" | ||
v := &Int{} | ||
r.NoError(v.Scan(in)) | ||
r.Equal([]int(*v), []int{44, 55}) | ||
}) | ||
|
||
t.Run("invalid entry", func(t *testing.T) { | ||
in := "{44,word}" | ||
v := &Int{} | ||
r.Error(v.Scan(in)) | ||
}) | ||
} |