diff --git a/README.md b/README.md
index 68d64c3..4ec96cd 100644
--- a/README.md
+++ b/README.md
@@ -116,7 +116,7 @@ The publicid package generates and validates NanoID strings designed to be publi
-## func [New]()
+## func [New]()
```go
func New(opts ...Option) (string, error)
@@ -125,7 +125,7 @@ func New(opts ...Option) (string, error)
New generates a unique nanoID with a length of 8 characters and the given options.
-## func [NewLong]()
+## func [NewLong]()
```go
func NewLong(opts ...Option) (string, error)
@@ -134,7 +134,7 @@ func NewLong(opts ...Option) (string, error)
NewLong generates a unique nanoID with a length of 12 characters and the given options.
-## func [Validate]()
+## func [Validate]()
```go
func Validate(id string) error
@@ -143,7 +143,7 @@ func Validate(id string) error
Validate checks if a given field name's public ID value is valid according to the constraints defined by package publicid.
-## func [ValidateLong]()
+## func [ValidateLong]()
```go
func ValidateLong(id string) error
@@ -152,7 +152,7 @@ func ValidateLong(id string) error
validateLong checks if a given field name's public ID value is valid according to the constraints defined by package publicid.
-## type [Option]()
+## type [Option]()
Option is a function type for configuring ID generation.
@@ -161,7 +161,7 @@ type Option func(*config)
```
-### func [Attempts]()
+### func [Attempts]()
```go
func Attempts(n int) Option
diff --git a/publicid.go b/publicid.go
index 4f8290e..e16fafe 100644
--- a/publicid.go
+++ b/publicid.go
@@ -16,6 +16,9 @@ const (
shortLen = 8
)
+// generator is the function used to generate nanoIDs.
+var generator = nanoid.Generate
+
// Option is a function type for configuring ID generation.
type Option func(*config)
@@ -50,7 +53,7 @@ func generateID(length int, opts ...Option) (string, error) {
var lastErr error
for i := 0; i < cfg.attempts; i++ {
- id, err := nanoid.Generate(alphabet, length)
+ id, err := generator(alphabet, length)
if err == nil {
return id, nil
}
diff --git a/publicid_test.go b/publicid_test.go
index 2e05254..a437f15 100644
--- a/publicid_test.go
+++ b/publicid_test.go
@@ -4,6 +4,7 @@
package publicid
import (
+ "fmt"
"testing"
)
@@ -104,3 +105,27 @@ func TestValidateLong(t *testing.T) {
})
}
}
+
+func TestNewFailsAfterAttempts(t *testing.T) {
+
+ // generator functions for testing
+ nanoIDGenerator := generator
+ mockGenerator := func(alphabet string, size int) (string, error) {
+ return "", fmt.Errorf("mocked error")
+ }
+
+ // Replace the Generate function with our mock generator function,
+ // and restore the original generator function in the deferred function.
+ generator = mockGenerator
+ defer func() { generator = nanoIDGenerator }()
+
+ _, err := New(Attempts(3))
+ if err == nil {
+ t.Error("Expected an error, but got nil")
+ } else {
+ expectedErrMsg := "failed to generate ID after 3 attempts: mocked error"
+ if err.Error() != expectedErrMsg {
+ t.Errorf("Expected error message '%s', but got '%s'", expectedErrMsg, err.Error())
+ }
+ }
+}