Skip to content

Commit

Permalink
Merge pull request #119 from mailgun/toutpetitadrien/randomizer
Browse files Browse the repository at this point in the history
  • Loading branch information
ToutPetitAdrien authored Aug 5, 2022
2 parents 85600d1 + 25fe1ec commit 7b1e8d9
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
47 changes: 47 additions & 0 deletions random/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package random

import (
"fmt"
"math/rand"
"strings"
)

const NumericRunes = "0123456789"
const LowerAlphaRunes = "abcdefghijklmnopqrstuvwxyz"
const UpperAlphaRunes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const AlphaRunes = UpperAlphaRunes + LowerAlphaRunes

// Return a random string made up of characters passed
func Runes(prefix string, length int, runes ...string) string {
chars := strings.Join(runes, "")
var bytes = make([]byte, length)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = chars[b%byte(len(chars))]
}
return prefix + string(bytes)
}

// Return a random string of alpha characters
func Alpha(prefix string, length int) string {
return Runes(prefix, length, AlphaRunes)
}

// Return a random string of alpha and numeric characters
func String(prefix string, length int) string {
return Runes(prefix, length, AlphaRunes, NumericRunes)
}

// Given a list of strings, return one of the strings randomly
func Item(items ...string) string {
var bytes = make([]byte, 1)
rand.Read(bytes)
return items[bytes[0]%byte(len(items))]
}

// Return a random domain name in the form "randomAlpha.net"
func DomainName() string {
return fmt.Sprintf("%s.%s",
Alpha("", 14),
Item("net", "com", "org", "io", "gov"))
}
91 changes: 91 additions & 0 deletions random/random_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package random

import (
"fmt"
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestString(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for _, tc := range []struct {
msg string
prefix string
length int
contains string
expectedLength int
}{{
msg: "only made of alpha and numeric characters",
prefix: "",
length: 10,
contains: AlphaRunes + NumericRunes,
expectedLength: 10,
}, {
msg: "with prefix and add to length",
prefix: "abc",
length: 5,
contains: AlphaRunes + NumericRunes,
expectedLength: 5 + 3, // = len("abc")
}} {
t.Run(tc.msg, func(t *testing.T) {
res := String(tc.prefix, tc.length)
assert.Equal(t, tc.expectedLength, len(res))
assert.Contains(t, res, tc.prefix)
for _, ch := range res {
assert.Contains(t, tc.contains, fmt.Sprintf("%c", ch))
}
})
}
}

func TestAlpha(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for _, tc := range []struct {
msg string
prefix string
length int
contains string
expectedLength int
}{{
msg: "only made of alpha characters",
prefix: "",
length: 10,
contains: AlphaRunes,
expectedLength: 10,
}, {
msg: "with prefix and add to length",
prefix: "abc",
length: 5,
contains: AlphaRunes,
expectedLength: 5 + 3, // = len("abc")
}} {
t.Run(tc.msg, func(t *testing.T) {
res := Alpha(tc.prefix, tc.length)
assert.Equal(t, tc.expectedLength, len(res))
assert.Contains(t, res, tc.prefix)
for _, ch := range res {
assert.Contains(t, tc.contains, fmt.Sprintf("%c", ch))
}
})
}
}

func TestItem(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for _, tc := range []struct {
msg string
items []string
expected string
}{{
msg: "one of the given list",
items: []string{"com", "net", "org"},
}} {
t.Run(tc.msg, func(t *testing.T) {
res := Item(tc.items...)
assert.Contains(t, tc.items, res)
})
}
}

0 comments on commit 7b1e8d9

Please sign in to comment.