Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit 0138a25

Browse files
authored
Merge pull request #1 from iutx/feat/add-Predefined-GenerateOptions
feat: add predefined GenerateOptions
2 parents e1a9743 + 3f1ed1b commit 0138a25

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# RandomX - Random String Generator
22

3+
[![license](https://img.shields.io/github/license/tailabs/randomx.svg)](https://github.com/tailabs/randomx/blob/main/LICENSE)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/tailabs/randomx)](https://goreportcard.com/report/github.com/tailabs/randomx)
5+
[![release](https://img.shields.io/github/release/tailabs/randomx/all.svg)](https://github.com/tailabs/randomx/releases)
6+
37
RandomX is a Go package that provides a user-friendly tool for generating random strings. It enables you to generate random strings of specified lengths with various character sets, including lowercase letters, uppercase letters, digits, and symbols.
48

59
This package was developed with the assistance of [ChatGPT 3.5](https://openai.com/), an advanced language model created by OpenAI.
@@ -12,7 +16,6 @@ To incorporate RandomX into your Go project, you can easily install it with the
1216
go get -u github.com/tailabs/randomx
1317
```
1418

15-
1619
## Usage
1720

1821
Here's an example demonstrating how to use the RandomX package to generate random strings:

randomx.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ const (
1313
Symbols = "!@#$%^&*()-_=+[]{}|;:,.<>?/~"
1414
)
1515

16+
// Predefined GenerateOptions for commonly used character sets
17+
var (
18+
OptionAllChars = GenerateOptions{
19+
UseLowercase: true,
20+
UseUppercase: true,
21+
UseDigits: true,
22+
UseSymbols: true,
23+
}
24+
25+
OptionLetters = GenerateOptions{
26+
UseLowercase: true,
27+
UseUppercase: true,
28+
UseDigits: false,
29+
UseSymbols: false,
30+
}
31+
32+
OptionDigitsOnly = GenerateOptions{
33+
UseLowercase: false,
34+
UseUppercase: false,
35+
UseDigits: true,
36+
UseSymbols: false,
37+
}
38+
)
39+
1640
// Interface defines the methods for generating random strings.
1741
type Interface interface {
1842
GenerateRandomString(length int, charSet string) string

randomx_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,44 @@ func TestProvider_GenerateRandomStringFromBuiltin(t *testing.T) {
6060
}
6161
}
6262
}
63+
64+
func TestProvider_GenerateRandomStringFromBuiltin_PredefinedOptions(t *testing.T) {
65+
rx := New().(*provider)
66+
67+
tests := []struct {
68+
length int
69+
options GenerateOptions
70+
}{
71+
{15, OptionAllChars},
72+
{10, OptionLetters},
73+
{8, OptionDigitsOnly},
74+
}
75+
76+
for _, test := range tests {
77+
randomStr := rx.GenerateRandomStringFromBuiltin(test.length, test.options)
78+
79+
expectedCharSet := ""
80+
if test.options.UseLowercase {
81+
expectedCharSet += LowercaseLetters
82+
}
83+
if test.options.UseUppercase {
84+
expectedCharSet += UppercaseLetters
85+
}
86+
if test.options.UseDigits {
87+
expectedCharSet += Digits
88+
}
89+
if test.options.UseSymbols {
90+
expectedCharSet += Symbols
91+
}
92+
93+
if len(randomStr) != test.length {
94+
t.Errorf("Generated string length does not match expected length for options %+v", test.options)
95+
}
96+
97+
for _, char := range randomStr {
98+
if !strings.Contains(expectedCharSet, string(char)) {
99+
t.Errorf("Generated string contains unexpected character: %s", string(char))
100+
}
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)