Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

person.go: 100% test coverage #33

Merged
merged 1 commit into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions internal/test/fake_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package test
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this file, the Faker structure should be enough to fake the seed

Copy link
Contributor Author

@ab22 ab22 Oct 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that I have to use Faker.NewWithSource but how will I be able to control the returned number from the rand.Source without a dummy implementation like this?

I apologize if I'm not following. I think might be missing something from the implementation of rand.Sourceso if you could provide a small sample on how to control the returned numbers it would be great. @jaswdr


// 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package test
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this file, the Faker structure should be enough to fake the seed, if you need you can add tests to the faker_test.go file


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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package faker
import (
"strings"
"testing"

"github.com/jaswdr/faker/internal/test"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need an internal package, the current structure is intentionally simple and only one single deep level

)

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
ab22 marked this conversation as resolved.
Show resolved Hide resolved
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:
ab22 marked this conversation as resolved.
Show resolved Hide resolved
// "{{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 Male.
s.Seed(51)
name, gender = f.Person().NameAndGender()
Expect(t, true, name != "")
Expect(t, true, gender == "Male")
}