Skip to content

Commit

Permalink
chore: fixing random, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zostay committed Jan 16, 2024
1 parent df6f22a commit 4c40c6d
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 60 deletions.
91 changes: 32 additions & 59 deletions pkg/ref/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,6 @@ import (
"math/rand"
)

// RandomCanonical returns a random book of the Bible.
func RandomCanonical() *Book {
return &Canonical.Books[rand.Int()%len(Canonical.Books)] //nolint:gosec // weak random is fine here
}

// RandomPassage returns a random passage from the given book of the Bible. It
// returns a two element slice of Verse, the first being the start of the
// passage and the second being the end of the passage. As of this writing, the
// verse references may be up to 30 Verses apart.
func RandomPassage(b *Book) []Verse {
x := rand.Int() % len(b.Verses) //nolint:gosec // weak random is fine here
o := rand.Int() % 30 //nolint:gosec // weak random is fine here
y := x + o
if y >= len(b.Verses) {
y = len(b.Verses) - 1
}

return b.Verses[x:y]
}

func RandomPassageFromRef(b *Resolved) []Verse {
x := rand.Int() % len(b.Verses()) //nolint:gosec // weak random is fine here
o := rand.Int() % 30 //nolint:gosec // weak random is fine here
y := x + o
if y >= len(b.Verses()) {
y = len(b.Verses()) - 1
}

return b.Verses()[x:y]
}

type randomOpts struct {
category string
book string
Expand Down Expand Up @@ -108,7 +77,7 @@ func Random(opt ...RandomReferenceOption) (*Resolved, error) {

b = ex.Ref.Book
} else {
b = RandomCanonical()
b = RandomCanonical(o.canon)
}

vs = RandomPassage(b)
Expand All @@ -123,30 +92,34 @@ func Random(opt ...RandomReferenceOption) (*Resolved, error) {
}, nil
}

// // RandomReference returns a random reference to a passage in the Bible in a
// // standard notation recognizable for American English speakers.
// //
// // This uses RandomCanonical and RandomPassage to generate the reference.
// //
// // Deprecated: Use Random() instead.
// func RandomReference() string {
// b := RandomCanonical()
// vs := RandomPassage(b)
//
// v1 := vs[0]
// v2 := vs[len(vs)-1]
//
// if len(vs) > 1 {
// return fmt.Sprintf("%s %s-%s", b.Name, v1.Ref(), v2.Ref())
// } else {
// return fmt.Sprintf("%s %s", b.Name, v1.Ref())
// }
// }
//
// // RandomVerse returns a random verse from the Bible. This uses RandomReference
// // to select a random reference and then uses GetVerse to retrieve the text of
// // the verse.
// func RandomVerse() (string, error) {
// ref := RandomReference()
// return GetVerse(ref)
// }
// RandomCanonical returns a random book of the Bible.
func RandomCanonical(c *Canon) *Book {
return &c.Books[rand.Int()%len(c.Books)] //nolint:gosec // weak random is fine here
}

// RandomPassage returns a random passage from the given book of the Bible. It
// returns a two element slice of Verse, the first being the start of the
// passage and the second being the end of the passage. As of this writing, the
// verse references may be up to 30 Verses apart.
func RandomPassage(b *Book) []Verse {
x := rand.Int() % len(b.Verses) //nolint:gosec // weak random is fine here
o := rand.Int()%29 + 1 //nolint:gosec // weak random is fine here
y := x + o
if y > len(b.Verses) {
y = len(b.Verses)
}

vs := b.Verses[x:y]
return vs
}

func RandomPassageFromRef(b *Resolved) []Verse {
x := rand.Int() % len(b.Verses()) //nolint:gosec // weak random is fine here
o := rand.Int()%29 + 1 //nolint:gosec // weak random is fine here
y := x + o
if y > len(b.Verses()) {
y = len(b.Verses())
}

return b.Verses()[x:y]
}
78 changes: 77 additions & 1 deletion pkg/ref/random_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,77 @@
package ref
package ref_test

import (
"testing"

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

"github.com/zostay/today/pkg/ref"
)

func TestRandomCanonical(t *testing.T) {
t.Parallel()

b := ref.RandomCanonical(ref.Canonical)
assert.NotNil(t, b)

found := false
for i := range ref.Canonical.Books {
if b == &ref.Canonical.Books[i] {
found = true
break
}
}

assert.True(t, found)
}

func TestRandomPassage(t *testing.T) {
t.Parallel()

for i := range ref.Canonical.Books {
b := &ref.Canonical.Books[i]

p := ref.RandomPassage(b)
assert.NotNil(t, p)
assert.NotEmpty(t, p)

for i := range p {
assert.True(t, b.Contains(p[i]))
}
}
}

func TestRandomPassageFromRef(t *testing.T) {
t.Parallel()

for i := range ref.Canonical.Books {
b := &ref.Canonical.Books[i]

p := ref.RandomPassage(b)
require.NotNil(t, p)
require.NotEmpty(t, p)

firstp := p[0]
lastp := p[len(p)-1]

r := &ref.Resolved{
Book: b,
First: p[0],
Last: p[len(p)-1],
}

vs := ref.RandomPassageFromRef(r)
require.NotNil(t, vs)
require.NotEmpty(t, vs)

firstv := vs[0]
lastv := vs[len(vs)-1]

// p range should contain v range
assert.True(t, firstp.Equal(firstv) || firstp.Before(lastv))
assert.True(t, firstv.Equal(lastp) || firstv.Before(lastp))
assert.True(t, firstp.Equal(lastv) || firstp.Before(lastv))
assert.True(t, lastv.Equal(lastp) || lastv.Before(lastp))
}
}

0 comments on commit 4c40c6d

Please sign in to comment.