Skip to content

Commit

Permalink
Refactor digit provider
Browse files Browse the repository at this point in the history
  • Loading branch information
sepisoltani committed Apr 30, 2024
1 parent b61d479 commit 0b4e3f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 6 additions & 4 deletions providers/digit/digit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"time"
)

var rng = rand.New(rand.NewSource(time.Now().UnixNano()))

// faNums contains Persian digits.
var faNums = []string{"۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"}

// PersianDigit returns a random Persian digit.
func PersianDigit() string {
src := rand.NewSource(time.Now().UnixNano())
rng := rand.New(src)
type Digit struct {
}

// GeneratePersianDigit returns a random Persian digit.
func (Digit) GeneratePersianDigit() string {
return faNums[rng.Intn(len(faNums))]
}
9 changes: 5 additions & 4 deletions providers/digit/digit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"testing"
)

// TestPersianDigit checks if the digit returned by PersianDigit is one of the valid Persian digits.
func TestPersianDigit(t *testing.T) {
digit := PersianDigit()
// TestGeneratePersianDigit checks if the digit returned by GeneratePersianDigit is one of the valid Persian digits.
func TestGeneratePersianDigit(t *testing.T) {
d := &Digit{}
digit := d.GeneratePersianDigit()
found := false
for _, d := range faNums {
if d == digit {
Expand All @@ -15,6 +16,6 @@ func TestPersianDigit(t *testing.T) {
}
}
if !found {
t.Errorf("PersianDigit returned an invalid digit: %s", digit)
t.Errorf("GeneratePersianDigit returned an invalid digit: %s", digit)
}
}

0 comments on commit 0b4e3f2

Please sign in to comment.