Skip to content

fix: a female or a male Name on each call - #107

Merged
bxcodec merged 1 commit into
go-faker:mainfrom
simonkundrik:fix/name-gender-per-call
Nov 25, 2026
Merged

bxcodec merged 1 commit into
go-faker:mainfrom
simonkundrik:fix/name-gender-per-call

Conversation

@simonkundrik

Copy link
Copy Markdown
Contributor

Name() was a female name for the whole run of the program, or a male name for the whole run: 2,000 calls gave titles like Mrs., Lady and Queen and none like Mr., Lord and King, or the other way round (four runs of the new test on master: 1,356 and 0, 1,310 and 0, 0 and 1,514, 0 and 1,490 names with a title that only one gender has).

Why

name picks the female or the male lists with randNameFlag > 50, and randNameFlag is rand.Intn(100) drawn once in init(). So the coin was flipped when the package started and every call used the same side. The tests set the flag to 51 and to 20 to reach each list, but they set it after the call, so neither list is tested, and the flag is left at 20 for the tests that follow.

What changed

  • name flips the coin on every call (rand.Intn(2) == 0). randNameFlag and its line in init() are gone.
  • The four tests that set randNameFlag after calling Name() check that the name is not empty, as they did, without the flag.
  • New TestNameGenderVaries: of 2,000 names about 667 have a title that only a man has (Mr., Prince, King, Lord) and about 750 a title that only a woman has (Dr. and Prof. are in both lists and are left out); it wants 500 of each and fails on master. New TestNameIsOneGender: the title and the first name of a Name() come from the same list; it passes on master and keeps it that way.

It is the same kind of process wide value as the card type in #105. I came across it reading the generators.

How it was tested
  • TestNameGenderVaries fails on master in every run (one gender only), passes on the change in 6 runs in a row.
  • go test ./..., go vet and gofmt pass. I could not run -race or golangci-lint here (no C compiler and no linter installed), CI does.

name() chose between the female and the male lists with randNameFlag, which
was drawn once when the package started, so every Name() of a run was a
female name or every one was a male name (2,000 calls gave 0 male titles, or
0 female ones, depending on the run). The choice is drawn on every call now,
and randNameFlag is gone.

The tests that set randNameFlag after the call did not test either list, they
only left the flag at 51 or 20 for the tests after them. They check that a
name is not empty as before, and two new tests check that both genders come
up and that the title and the first name of a Name are from the same gender.

@bxcodec bxcodec left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM on this, thanks for the PR

@bxcodec
bxcodec merged commit b53acd6 into go-faker:main Nov 25, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants