-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors_test.go
197 lines (144 loc) · 5.4 KB
/
errors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) 2024 Six After, Inc
//
// This source code is licensed under the Apache 2.0 License found in the
// LICENSE file in the root directory of this source tree.
package nanoid
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestErrDuplicateCharacters ensures that the generator returns ErrDuplicateCharacters
// when the provided alphabet contains duplicate characters.
func TestErrDuplicateCharacters(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Alphabet with duplicate characters
alphabet := "abcabc"
_, err := NewGenerator(WithAlphabet(alphabet))
is.Equal(ErrDuplicateCharacters, err)
}
// TestErrInvalidLength verifies that the generator returns ErrInvalidLength
// when the LengthHint is set to zero, which is invalid.
func TestErrInvalidLength(t *testing.T) {
t.Parallel()
is := assert.New(t)
// LengthHint set to 0, which is invalid
_, err := NewGenerator(WithLengthHint(0))
is.Equal(ErrInvalidLength, err)
}
// TestErrInvalidAlphabet checks that the generator returns ErrInvalidAlphabet
// when an empty alphabet is provided.
func TestErrInvalidAlphabet(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Empty alphabet string, which is invalid
alphabet := ""
_, err := NewGenerator(WithAlphabet(alphabet))
is.Equal(ErrInvalidAlphabet, err)
}
// TestErrNonUTF8Alphabet ensures that the generator returns ErrNonUTF8Alphabet
// when the alphabet contains invalid UTF-8 characters.
func TestErrNonUTF8Alphabet(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Invalid UTF-8 string (e.g., invalid byte sequences)
// For example, byte 0xFF is invalid in UTF-8
alphabet := string([]byte{0xFF, 0xFE, 0xFD})
_, err := NewGenerator(WithAlphabet(alphabet))
is.Equal(ErrNonUTF8Alphabet, err)
}
// TestErrAlphabetTooShort verifies that the generator returns ErrAlphabetTooShort
// when the alphabet has fewer than 2 characters.
func TestErrAlphabetTooShort(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Alphabet with only 1 character, which is too short
alphabet := "A"
_, err := NewGenerator(WithAlphabet(alphabet))
is.Equal(ErrAlphabetTooShort, err)
}
// TestErrAlphabetTooLong checks that the generator returns ErrAlphabetTooLong
// when the alphabet exceeds 256 characters.
func TestErrAlphabetTooLong(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Create a unique alphabet with 257 unique characters
alphabet := makeUnicodeAlphabet(257)
// Verify the length. We expect 514 because each character is represented by 2 bytes.
is.Equal(514, len(alphabet))
_, err := NewGenerator(WithAlphabet(alphabet))
is.Equal(ErrAlphabetTooLong, err)
}
// TestErrNilRandReader ensures that the generator returns ErrNilRandReader
// when the random reader (RandReader) is set to nil.
func TestErrNilRandReader(t *testing.T) {
t.Parallel()
is := assert.New(t)
// RandReader set to nil, which is invalid
_, err := NewGenerator(WithRandReader(nil))
is.Equal(ErrNilRandReader, err)
}
// alwaysInvalidRandReader is a mock implementation of io.Reader that always returns invalid indices.
// For an alphabet of "ABC" (length 3), it returns bytes with value 3, which are invalid indices.
type alwaysInvalidRandReader struct{}
// Read fills the provided byte slice with the invalid byte (3) and never returns an error.
func (a *alwaysInvalidRandReader) Read(p []byte) (int, error) {
for i := range p {
p[i] = 3 // Invalid index for alphabet "ABC"
}
return len(p), nil
}
// TestErrExceededMaxAttempts verifies that the generator returns ErrExceededMaxAttempts
// when it cannot produce a valid ID within the maximum number of attempts.
func TestErrExceededMaxAttempts(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Initialize the mockRandReader that always returns invalid indices.
mockReader := &alwaysInvalidRandReader{}
const length = 5
generator, err := NewGenerator(
WithAlphabet("ABC"), // Non-power-of-two alphabet (length 3)
WithRandReader(mockReader), // Mocked RandReader returning invalid indices (3)
WithLengthHint(length),
)
is.NoError(err, "Expected no error when initializing generator with valid configuration")
// Attempt to generate an ID; expect ErrExceededMaxAttempts
_, err = generator.New(length)
is.Equal(ErrExceededMaxAttempts, err, "Expected ErrExceededMaxAttempts when generator cannot find valid indices")
}
// TestErrNilPointer_MarshalText ensures that MarshalText returns ErrNilPointer
// when called on a nil *ID.
func TestErrNilPointer_MarshalText(t *testing.T) {
t.Parallel()
is := assert.New(t)
var id *ID = nil
_, err := id.MarshalText()
is.Equal(ErrNilPointer, err)
}
// TestErrNilPointer_UnmarshalText ensures that UnmarshalText returns ErrNilPointer
// when called on a nil *ID.
func TestErrNilPointer_UnmarshalText(t *testing.T) {
t.Parallel()
is := assert.New(t)
var id *ID = nil
err := id.UnmarshalText([]byte("test"))
is.Equal(ErrNilPointer, err)
}
// TestErrNilPointer_MarshalBinary ensures that MarshalBinary returns ErrNilPointer
// when called on a nil *ID.
func TestErrNilPointer_MarshalBinary(t *testing.T) {
t.Parallel()
is := assert.New(t)
var id *ID = nil
_, err := id.MarshalBinary()
is.Equal(ErrNilPointer, err)
}
// TestErrNilPointer_UnmarshalBinary ensures that UnmarshalBinary returns ErrNilPointer
// when called on a nil *ID.
func TestErrNilPointer_UnmarshalBinary(t *testing.T) {
t.Parallel()
is := assert.New(t)
var id *ID = nil
err := id.UnmarshalBinary([]byte("test"))
is.Equal(ErrNilPointer, err)
}