Skip to content

Commit

Permalink
100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jackspirou committed Sep 15, 2024
1 parent 7c8f59c commit ec8f4a9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ The publicid package generates and validates NanoID strings designed to be publi
<a name="New"></a>
## func [New](<https://github.com/agentstation/publicid/blob/master/publicid.go#L35>)
## func [New](<https://github.com/agentstation/publicid/blob/master/publicid.go#L38>)
```go
func New(opts ...Option) (string, error)
Expand All @@ -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.
<a name="NewLong"></a>
## func [NewLong](<https://github.com/agentstation/publicid/blob/master/publicid.go#L40>)
## func [NewLong](<https://github.com/agentstation/publicid/blob/master/publicid.go#L43>)
```go
func NewLong(opts ...Option) (string, error)
Expand All @@ -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.
<a name="Validate"></a>
## func [Validate](<https://github.com/agentstation/publicid/blob/master/publicid.go#L64>)
## func [Validate](<https://github.com/agentstation/publicid/blob/master/publicid.go#L67>)
```go
func Validate(id string) error
Expand All @@ -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.

<a name="ValidateLong"></a>
## func [ValidateLong](<https://github.com/agentstation/publicid/blob/master/publicid.go#L70>)
## func [ValidateLong](<https://github.com/agentstation/publicid/blob/master/publicid.go#L73>)

```go
func ValidateLong(id string) error
Expand All @@ -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.
<a name="Option"></a>
## type [Option](<https://github.com/agentstation/publicid/blob/master/publicid.go#L20>)
## type [Option](<https://github.com/agentstation/publicid/blob/master/publicid.go#L23>)
Option is a function type for configuring ID generation.
Expand All @@ -161,7 +161,7 @@ type Option func(*config)
```
<a name="Attempts"></a>
### func [Attempts](<https://github.com/agentstation/publicid/blob/master/publicid.go#L28>)
### func [Attempts](<https://github.com/agentstation/publicid/blob/master/publicid.go#L31>)
```go
func Attempts(n int) Option
Expand Down
5 changes: 4 additions & 1 deletion publicid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 25 additions & 0 deletions publicid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package publicid

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -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())
}
}
}

0 comments on commit ec8f4a9

Please sign in to comment.