-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD & REFACTOR] Boolean, BoolInt, BoolString faker (#25)
- Loading branch information
Showing
5 changed files
with
74 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters