Skip to content

Commit

Permalink
person.go: 100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ab22 committed Oct 22, 2020
1 parent 1fa8ba6 commit 30ff5c2
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 9 deletions.
26 changes: 26 additions & 0 deletions internal/test/fake_source.go
@@ -0,0 +1,26 @@
package test

// FakeSource implements the rand.Source interface. This is useful for when we
// want to supply dummy/predictable values to test specific scenarios that depend
// on rand.Rand.
type FakeSource struct {
seed int64
}

// NewFakeSource returns a new instance of FakeSource. FakeSource does not
// generates random numbers, so the passed in seed number is the returned "random"
// number.
func NewFakeSource(seed int64) *FakeSource {
return &FakeSource{seed: seed}
}

// Seed sets the internal int to the passed in seed value. This will be the returned
// value when using FakeSource.Int63().
func (s *FakeSource) Seed(seed int64) {
s.seed = seed
}

// Int63 returns the dummy seed value.
func (s *FakeSource) Int63() int64 {
return s.seed
}
17 changes: 17 additions & 0 deletions internal/test/fake_source_test.go
@@ -0,0 +1,17 @@
package test

import "testing"

func TestFakeGenerator(t *testing.T) {
s := NewFakeSource(100)

if n := s.Int63(); n != 100 {
t.Errorf("expected 100 but got %d", n)
}

s.Seed(200)

if n := s.Int63(); n != 200 {
t.Errorf("expected 200 but got %d", n)
}
}
4 changes: 2 additions & 2 deletions person.go
Expand Up @@ -134,7 +134,7 @@ func (p Person) GenderFemale() string {

// Title returns a fake title for Person
func (p Person) Title() string {
if p.Faker.IntBetween(0, 1) == 0 {
if p.Faker.IntBetween(0, 2) == 0 {
return p.TitleMale()
}

Expand Down Expand Up @@ -168,7 +168,7 @@ func (p Person) LastName() string {
// Name returns a fake name for Person
func (p Person) Name() string {
formats := append(maleNameFormats, femaleNameFormats...)
name := formats[p.Faker.IntBetween(0, len(formats)-1)]
name := formats[p.Faker.IntBetween(0, len(formats))]

// {{titleMale}}
if strings.Contains(name, "{{titleMale}}") {
Expand Down
56 changes: 49 additions & 7 deletions person_test.go
Expand Up @@ -3,6 +3,8 @@ package faker
import (
"strings"
"testing"

"github.com/jaswdr/faker/internal/test"
)

func TestTitleMale(t *testing.T) {
Expand All @@ -16,8 +18,22 @@ func TestTitleFemale(t *testing.T) {
}

func TestTitle(t *testing.T) {
p := New().Person()
Expect(t, 3, len(p.Title()))
// Test Title Male
s := test.NewFakeSource(0)
f := NewWithSeed(s)
maleTitle := f.Person().Title()

Expect(t, 3, len(maleTitle))

// Test Title Female
s.Seed(1)
femaleTitle := f.Person().Title()

Expect(t, 3, len(femaleTitle))

if maleTitle == femaleTitle {
t.Errorf("expected male title '%s' to be different from female title '%s'", maleTitle, femaleTitle)
}
}

func TestSuffix(t *testing.T) {
Expand Down Expand Up @@ -51,8 +67,25 @@ func TestLastName(t *testing.T) {
}

func TestName(t *testing.T) {
p := New().Person()
name := p.Name()
// Set fake generator int to 6 to grab this maleNameFormat:
// "{{titleMale}} {{firstNameMale}} {{lastName}} {{suffix}}"
s := test.NewFakeSource(6)
f := NewWithSeed(s)
name := f.Person().Name()

Expect(t, true, len(name) > 0)
Expect(t, false, strings.Contains(name, "{{titleMale}}"))
Expect(t, false, strings.Contains(name, "{{firstNameMale}}"))
Expect(t, false, strings.Contains(name, "{{titleFemale}}"))
Expect(t, false, strings.Contains(name, "{{firstNameFemale}}"))
Expect(t, false, strings.Contains(name, "{{lastName}}"))
Expect(t, false, strings.Contains(name, "{{suffix}}"))

// Set int to 13 to grab this femaleNameFormat:
// "{{titleFemale}} {{firstNameFemale}} {{lastName}} {{suffix}}"
s.Seed(13)
name = f.Person().Name()

Expect(t, true, len(name) > 0)
Expect(t, false, strings.Contains(name, "{{titleMale}}"))
Expect(t, false, strings.Contains(name, "{{firstNameMale}}"))
Expand All @@ -79,8 +112,17 @@ func TestGenderFemale(t *testing.T) {
}

func TestNameAndGender(t *testing.T) {
p := New().Person()
name, gender := p.NameAndGender()
// Test Name and Gender Female
s := test.NewFakeSource(0)
f := NewWithSeed(s)
name, gender := f.Person().NameAndGender()

Expect(t, true, name != "")
Expect(t, true, gender == "Male" || gender == "Female")
Expect(t, true, gender == "Female")

// Test Name and Gender Female
s.Seed(51)
name, gender = f.Person().NameAndGender()
Expect(t, true, name != "")
Expect(t, true, gender == "Male")
}

0 comments on commit 30ff5c2

Please sign in to comment.