mirror of
https://github.com/jimeh/rands.git
synced 2026-02-19 03:16:39 +00:00
feat(error): export error variables
This commit is contained in:
6
ints.go
6
ints.go
@@ -6,12 +6,12 @@ import (
|
||||
"math/big"
|
||||
)
|
||||
|
||||
var errInvalidMaxInt = fmt.Errorf("%w: max cannot be less than 1", errBase)
|
||||
var ErrInvalidMaxInt = fmt.Errorf("%w: max cannot be less than 1", Err)
|
||||
|
||||
// Int generates a random int ranging between 0 and max.
|
||||
func Int(max int) (int, error) {
|
||||
if max < 1 {
|
||||
return 0, errInvalidMaxInt
|
||||
return 0, ErrInvalidMaxInt
|
||||
}
|
||||
|
||||
r, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
|
||||
@@ -25,7 +25,7 @@ func Int(max int) (int, error) {
|
||||
// Int64 generates a random int64 ranging between 0 and max.
|
||||
func Int64(max int64) (int64, error) {
|
||||
if max < 1 {
|
||||
return 0, errInvalidMaxInt
|
||||
return 0, ErrInvalidMaxInt
|
||||
}
|
||||
|
||||
r, err := rand.Int(rand.Reader, big.NewInt(max))
|
||||
|
||||
27
ints_test.go
27
ints_test.go
@@ -1,7 +1,6 @@
|
||||
package rands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -16,67 +15,67 @@ var testIntCases = []struct {
|
||||
{
|
||||
name: "n=-2394345",
|
||||
max: -2394345,
|
||||
errIs: errInvalidMaxInt,
|
||||
errIs: ErrInvalidMaxInt,
|
||||
errStr: "rands: max cannot be less than 1",
|
||||
},
|
||||
{
|
||||
name: "n=-409600",
|
||||
max: -409600,
|
||||
errIs: errInvalidMaxInt,
|
||||
errIs: ErrInvalidMaxInt,
|
||||
errStr: "rands: max cannot be less than 1",
|
||||
},
|
||||
{
|
||||
name: "n=-1024",
|
||||
max: -1024,
|
||||
errIs: errInvalidMaxInt,
|
||||
errIs: ErrInvalidMaxInt,
|
||||
errStr: "rands: max cannot be less than 1",
|
||||
},
|
||||
{
|
||||
name: "n=-128",
|
||||
max: -128,
|
||||
errIs: errInvalidMaxInt,
|
||||
errIs: ErrInvalidMaxInt,
|
||||
errStr: "rands: max cannot be less than 1",
|
||||
},
|
||||
{
|
||||
name: "n=-32",
|
||||
max: -32,
|
||||
errIs: errInvalidMaxInt,
|
||||
errIs: ErrInvalidMaxInt,
|
||||
errStr: "rands: max cannot be less than 1",
|
||||
},
|
||||
{
|
||||
name: "n=-16",
|
||||
max: -16,
|
||||
errIs: errInvalidMaxInt,
|
||||
errIs: ErrInvalidMaxInt,
|
||||
errStr: "rands: max cannot be less than 1",
|
||||
},
|
||||
{
|
||||
name: "n=-8",
|
||||
max: -8,
|
||||
errIs: errInvalidMaxInt,
|
||||
errIs: ErrInvalidMaxInt,
|
||||
errStr: "rands: max cannot be less than 1",
|
||||
},
|
||||
{
|
||||
name: "n=-7",
|
||||
max: -7,
|
||||
errIs: errInvalidMaxInt,
|
||||
errIs: ErrInvalidMaxInt,
|
||||
errStr: "rands: max cannot be less than 1",
|
||||
},
|
||||
{
|
||||
name: "n=-2",
|
||||
max: -2,
|
||||
errIs: errInvalidMaxInt,
|
||||
errIs: ErrInvalidMaxInt,
|
||||
errStr: "rands: max cannot be less than 1",
|
||||
},
|
||||
{
|
||||
name: "n=-1",
|
||||
max: -1,
|
||||
errIs: errInvalidMaxInt,
|
||||
errIs: ErrInvalidMaxInt,
|
||||
errStr: "rands: max cannot be less than 1",
|
||||
},
|
||||
{
|
||||
name: "n=0",
|
||||
max: 0,
|
||||
errIs: errInvalidMaxInt,
|
||||
errIs: ErrInvalidMaxInt,
|
||||
errStr: "rands: max cannot be less than 1",
|
||||
},
|
||||
{name: "n=1", max: 1},
|
||||
@@ -102,7 +101,7 @@ func TestInt(t *testing.T) {
|
||||
}
|
||||
|
||||
if tt.errIs != nil {
|
||||
assert.True(t, errors.Is(err, errInvalidMaxInt))
|
||||
assert.ErrorIs(t, err, tt.errIs)
|
||||
}
|
||||
|
||||
if tt.errStr != "" {
|
||||
@@ -133,7 +132,7 @@ func TestInt64(t *testing.T) {
|
||||
}
|
||||
|
||||
if tt.errIs != nil {
|
||||
assert.True(t, errors.Is(err, errInvalidMaxInt))
|
||||
assert.ErrorIs(t, err, tt.errIs)
|
||||
}
|
||||
|
||||
if tt.errStr != "" {
|
||||
|
||||
2
rands.go
2
rands.go
@@ -8,4 +8,4 @@ package rands
|
||||
|
||||
import "errors"
|
||||
|
||||
var errBase = errors.New("rands")
|
||||
var Err = errors.New("rands")
|
||||
|
||||
12
strings.go
12
strings.go
@@ -22,12 +22,12 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
errNonASCIIAlphabet = fmt.Errorf(
|
||||
"%w: alphabet contains non-ASCII characters", errBase,
|
||||
ErrNonASCIIAlphabet = fmt.Errorf(
|
||||
"%w: alphabet contains non-ASCII characters", Err,
|
||||
)
|
||||
|
||||
errDNSLabelLength = fmt.Errorf(
|
||||
"%w: DNS labels must be between 1 and 63 characters in length", errBase,
|
||||
ErrDNSLabelLength = fmt.Errorf(
|
||||
"%w: DNS labels must be between 1 and 63 characters in length", Err,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -127,7 +127,7 @@ func LowerNumeric(n int) (string, error) {
|
||||
// UnicodeString() if you need a alphabet with Unicode characters.
|
||||
func String(n int, alphabet string) (string, error) {
|
||||
if !isASCII(alphabet) {
|
||||
return "", errNonASCIIAlphabet
|
||||
return "", ErrNonASCIIAlphabet
|
||||
}
|
||||
|
||||
l := big.NewInt(int64(len(alphabet)))
|
||||
@@ -180,7 +180,7 @@ func UnicodeString(n int, alphabet []rune) (string, error) {
|
||||
func DNSLabel(n int) (string, error) {
|
||||
switch {
|
||||
case n < 1 || n > 63:
|
||||
return "", errDNSLabelLength
|
||||
return "", ErrDNSLabelLength
|
||||
case n == 1:
|
||||
return String(1, lowerChars)
|
||||
default:
|
||||
|
||||
@@ -3,7 +3,6 @@ package rands
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -262,7 +261,7 @@ var stringTestCases = []struct {
|
||||
n: 32,
|
||||
alphabet: "αβγδεζηθικλμνξοπρστυφχψωςΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΩ" +
|
||||
"άίόύώέϊϋΐΰΆΈΌΏΎΊ",
|
||||
errIs: errNonASCIIAlphabet,
|
||||
errIs: ErrNonASCIIAlphabet,
|
||||
errStr: "rands: alphabet contains non-ASCII characters",
|
||||
},
|
||||
{
|
||||
@@ -270,7 +269,7 @@ var stringTestCases = []struct {
|
||||
n: 32,
|
||||
alphabet: "的一是不了人我在有他这为之大来以个中上们到说国和地也子" +
|
||||
"时道出而要于就下得可你年生",
|
||||
errIs: errNonASCIIAlphabet,
|
||||
errIs: ErrNonASCIIAlphabet,
|
||||
errStr: "rands: alphabet contains non-ASCII characters",
|
||||
},
|
||||
{
|
||||
@@ -278,7 +277,7 @@ var stringTestCases = []struct {
|
||||
n: 32,
|
||||
alphabet: "一九七二人入八力十下三千上口土夕大女子小山川五天中六円" +
|
||||
"手文日月木水火犬王正出本右四",
|
||||
errIs: errNonASCIIAlphabet,
|
||||
errIs: ErrNonASCIIAlphabet,
|
||||
errStr: "rands: alphabet contains non-ASCII characters",
|
||||
},
|
||||
{
|
||||
@@ -359,7 +358,7 @@ func TestString(t *testing.T) {
|
||||
}
|
||||
|
||||
if tt.errIs != nil {
|
||||
assert.True(t, errors.Is(err, errNonASCIIAlphabet))
|
||||
assert.ErrorIs(t, err, tt.errIs)
|
||||
}
|
||||
|
||||
if tt.errStr != "" {
|
||||
@@ -507,14 +506,14 @@ var dnsLabelTestCases = []struct {
|
||||
{
|
||||
name: "n=-128",
|
||||
n: -128,
|
||||
errIs: errDNSLabelLength,
|
||||
errIs: ErrDNSLabelLength,
|
||||
errStr: "rands: DNS labels must be between 1 and 63 characters " +
|
||||
"in length",
|
||||
},
|
||||
{
|
||||
name: "n=0",
|
||||
n: 0,
|
||||
errIs: errDNSLabelLength,
|
||||
errIs: ErrDNSLabelLength,
|
||||
errStr: "rands: DNS labels must be between 1 and 63 characters " +
|
||||
"in length",
|
||||
},
|
||||
@@ -532,14 +531,14 @@ var dnsLabelTestCases = []struct {
|
||||
{
|
||||
name: "n=64",
|
||||
n: 64,
|
||||
errIs: errDNSLabelLength,
|
||||
errIs: ErrDNSLabelLength,
|
||||
errStr: "rands: DNS labels must be between 1 and 63 characters " +
|
||||
"in length",
|
||||
},
|
||||
{
|
||||
name: "n=128",
|
||||
n: 128,
|
||||
errIs: errDNSLabelLength,
|
||||
errIs: ErrDNSLabelLength,
|
||||
errStr: "rands: DNS labels must be between 1 and 63 characters " +
|
||||
"in length",
|
||||
},
|
||||
@@ -559,7 +558,7 @@ func TestDNSLabel(t *testing.T) {
|
||||
}
|
||||
|
||||
if tt.errIs != nil {
|
||||
require.True(t, errors.Is(err, errDNSLabelLength))
|
||||
require.ErrorIs(t, err, tt.errIs)
|
||||
}
|
||||
|
||||
if tt.errStr != "" {
|
||||
|
||||
Reference in New Issue
Block a user