-
-
Notifications
You must be signed in to change notification settings - Fork 59
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove this file, the |
||
|
||
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) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ package faker | |
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/jaswdr/faker/internal/test" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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) { | ||
|
@@ -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}}")) | ||
|
@@ -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") | ||
} |
There was a problem hiding this comment.
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 seedThere was a problem hiding this comment.
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 therand.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.Source
so if you could provide a small sample on how to control the returned numbers it would be great. @jaswdr