Skip to content

Commit

Permalink
added BoolWithChance() function (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksey-rezvov authored Oct 7, 2020
1 parent 25517ca commit 9fc136a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ func (f Faker) Bool() bool {
return f.IntBetween(0, 100) > 50
}

// BoolWithChance returns true with chance chanceTrue in percents otherwise returns false
func (f Faker) BoolWithChance(chanceTrue int) bool {
if chanceTrue <= 0 {
return false
} else if chanceTrue >= 100 {
return true
}

return f.IntBetween(0, 100) < chanceTrue
}

func (f Faker) Lorem() Lorem {
return Lorem{&f}
}
Expand Down
11 changes: 11 additions & 0 deletions faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,14 @@ func TestBool(t *testing.T) {
tp := reflect.TypeOf(f.Bool())
Expect(t, "bool", tp.String())
}

func TestBoolWithChance(t *testing.T) {
f := New()
tp := reflect.TypeOf(f.BoolWithChance(30))
Expect(t, "bool", tp.String())

Expect(t, true, f.BoolWithChance(100))
Expect(t, false, f.BoolWithChance(0))
Expect(t, true, f.BoolWithChance(101))
Expect(t, false, f.BoolWithChance(-1))
}

0 comments on commit 9fc136a

Please sign in to comment.