From 512b657a42880af87e9f0d863aa6dccf3540d4ba Mon Sep 17 00:00:00 2001 From: Charlie Lukman Date: Wed, 31 Mar 2021 08:36:51 +0700 Subject: [PATCH] feat: add public matcher function for custom error type invalidLengthError (#78) --- uuid.go | 6 ++++++ uuid_test.go | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/uuid.go b/uuid.go index 60d26bb..d732ae8 100644 --- a/uuid.go +++ b/uuid.go @@ -41,6 +41,12 @@ func (err invalidLengthError) Error() string { return fmt.Sprintf("invalid UUID length: %d", err.len) } +// IsInvalidLengthError is matcher function for custom error invalidLengthError +func IsInvalidLengthError(err error) bool { + _, ok := err.(invalidLengthError) + return ok +} + // Parse decodes s into a UUID or returns an error. Both the standard UUID // forms of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded as well as the diff --git a/uuid_test.go b/uuid_test.go index 709e34c..99f0bed 100644 --- a/uuid_test.go +++ b/uuid_test.go @@ -526,6 +526,13 @@ func TestWrongLength(t *testing.T) { } } +func TestIsWrongLength(t *testing.T) { + _, err := Parse("12345") + if !IsInvalidLengthError(err) { + t.Errorf("expected error type is invalidLengthError") + } +} + var asString = "f47ac10b-58cc-0372-8567-0e02b2c3d479" var asBytes = []byte(asString)