Skip to content

Commit

Permalink
[ADD & REFACTOR] Boolean, BoolInt, BoolString faker (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
bagusays authored Oct 9, 2020
1 parent d920e77 commit 3ca4d10
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 33 deletions.
34 changes: 34 additions & 0 deletions boolean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package faker

// Boolean is a faker struct for Boolean
type Boolean struct {
Faker *Faker
}

// Bool returns a fake bool for Faker
func (b Boolean) Bool() bool {
return b.Faker.IntBetween(0, 100) > 50
}

// BoolWithChance returns true with a given percentual chance that the value is true, otherwise returns false
func (b Boolean) BoolWithChance(chanceTrue int) bool {
if chanceTrue <= 0 {
return false
} else if chanceTrue >= 100 {
return true
}

return b.Faker.IntBetween(0, 100) < chanceTrue
}

// BoolInt returns a fake bool for Integer Boolean
func (b Boolean) BoolInt() int {
return b.Faker.RandomIntElement([]int{0, 1})
}

// BoolString returns a fake bool for string Boolean
func (b Boolean) BoolString(firstArg string, secondArg string) string {
boolean := []string{firstArg, secondArg}

return b.Faker.RandomStringElement(boolean)
}
36 changes: 36 additions & 0 deletions boolean_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package faker

import (
"reflect"
"testing"
)

func TestBooleanBool(t *testing.T) {
f := New().Boolean()
tp := reflect.TypeOf(f.Bool())
Expect(t, "bool", tp.String())
}

func TestBooleanBoolWithChance(t *testing.T) {
f := New().Boolean()
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))
}

func TestBooleanInt(t *testing.T) {
p := New().Boolean()
result := p.BoolInt()
Expect(t, true, result == 1 || result == 0)
}

func TestBooleanString(t *testing.T) {
p := New().Boolean()
args := []string{"yes", "no"}
result := p.BoolString(args[0], args[1])
Expect(t, true, result == args[0] || result == args[1])
}
17 changes: 3 additions & 14 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,9 @@ func (f Faker) Asciify(in string) (out string) {
return
}

// Bool returns a fake bool for Faker
func (f Faker) Bool() bool {
return f.IntBetween(0, 100) > 50
}

// BoolWithChance returns true with a given percentual chance that the value is true, 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
// Boolean returns a fake Boolean instance for Faker
func (f Faker) Boolean() Boolean {
return Boolean{&f}
}

// Lorem returns a fake Lorem instance for Faker
Expand Down
18 changes: 0 additions & 18 deletions faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package faker
import (
"fmt"
"math/rand"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -175,20 +174,3 @@ func TestAsciify(t *testing.T) {
Expect(t, true, strings.Contains(value, "?"))
Expect(t, false, strings.Contains(value, "*"))
}

func TestBool(t *testing.T) {
f := New()
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))
}
2 changes: 1 addition & 1 deletion internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (i Internet) Query() string {
lorem := i.Faker.Lorem()
query := "?" + lorem.Word() + "=" + lorem.Word()
for j := 0; j < i.Faker.IntBetween(1, 3); j++ {
if i.Faker.Bool() {
if i.Faker.Boolean().Bool() {
query += "&" + lorem.Word() + "=" + lorem.Word()
} else {
query += "&" + lorem.Word() + "=" + strconv.Itoa(i.Faker.RandomDigitNotNull())
Expand Down

0 comments on commit 3ca4d10

Please sign in to comment.