diff options
author | EuAndreh <eu@euandre.org> | 2025-01-14 17:57:49 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-01-14 20:06:05 -0300 |
commit | 10686420e5adc0f1abd08290267d21684d33591b (patch) | |
tree | a6e82e42666bcfa30e03889aa7117e9671055c7c /src/guuid.go | |
parent | .gitignore: Include pattern for cgo (diff) | |
download | uuid-10686420e5adc0f1abd08290267d21684d33591b.tar.gz uuid-10686420e5adc0f1abd08290267d21684d33591b.tar.xz |
tests/guuid.go: Add test for panic inside New()
Diffstat (limited to 'src/guuid.go')
-rw-r--r-- | src/guuid.go | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/guuid.go b/src/guuid.go index f4c6d7b..e4f5f14 100644 --- a/src/guuid.go +++ b/src/guuid.go @@ -17,8 +17,8 @@ const ( ) var ( - dashIndexes = []int { 8, 13, 18, 23 } - emptyUUID = UUID {} + dashIndexes = []int{ 8, 13, 18, 23 } + randomReader = rand.Reader ErrBadLength = errors.New("guuid: str isn't of the correct length") ErrBadDashCount = errors.New("guuid: Bad count of dashes in string") @@ -35,7 +35,7 @@ func NewFrom(r io.Reader) (UUID, error) { var uuid UUID _, err := io.ReadFull(r, uuid[:]) if err != nil { - return emptyUUID, err + return UUID{}, err } uuid[6] = (uuid[6] & 0x0f) | 0x40 // v4 uuid[8] = (uuid[8] & 0x3f) | 0x80 // variant 10 @@ -43,7 +43,7 @@ func NewFrom(r io.Reader) (UUID, error) { } func New() UUID { - uuid, err := NewFrom(rand.Reader) + uuid, err := NewFrom(randomReader) if err != nil { panic(err) } @@ -77,23 +77,23 @@ func (uuid UUID) String() string { func FromString(str string) (UUID, error) { if len(str) != encodedLength { - return emptyUUID, ErrBadLength + return UUID{}, ErrBadLength } if strings.Count(str, "-") != dashCount { - return emptyUUID, ErrBadDashCount + return UUID{}, ErrBadDashCount } for _, idx := range dashIndexes { if str[idx] != '-' { - return emptyUUID, ErrBadDashPosition + return UUID{}, ErrBadDashPosition } } hexstr := strings.Join(strings.Split(str, "-"), "") data, err := hex.DecodeString(hexstr) if err != nil { - return emptyUUID, err + return UUID{}, err } return [ByteCount]byte(data), nil |