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

Add Vehicle, Music, and Person to Parse method #565

Open
Perks-of-Being-a-Cauliflower opened this issue Sep 24, 2024 · 1 comment
Open

Comments

@Perks-of-Being-a-Cauliflower

Description

I am using Faker dynamically and it would be helpful if the Vehicle, Music, and Person methods were added to the Parse method. Vehicle and Music are simple, they just need to be added to the list of MustsacheMethods passed into the Parse method. Person appears to be a bit more complex. When it is added to Parse it seems to mess up the seed which causes some of the randomizer parse tests to fail. I assume this is because Person takes Randomizer as a parameter itself.

I have attempted the changes on a feature branch with Vehicle and Music working fine but seems a bit redundant if I can't get Person working. Person would also require methods for each of its properties so they can be registered as MustacheMethods.

LINQPad Code Example

Bogus Version="35.6.1"

using Bogus;

void Main()
{
    var faker = new Faker();
    var manufacturer = faker.Parse("{{vehicle.manufacturer}}");
    var genre = faker.Parse("{{music.genre}}");
    var firstname = faker.Parse("{{person.getfirstname}}");
    var lastname = faker.Parse("{{person.getlastname}}");
    var fullname = faker.Parse("{{person.getfullname}}");
	
    if (manufacturer == null)
    {
        throw new Exception("Parse failed manufacturer is null");
    }
	
    if (genre == null)
    {
        throw new Exception("Parse failed genre is null");
    }
	
    if (firstname == null || lastname == null || fullname == null)
    {
        throw new Exception("Parse failed Person values are null");
    }
	
    if ($"{firstname} {lastname}" != fullname)
    {
        throw new Exception("Person values not generated correctly with Parse");
    }
}

What alternatives have you considered?

For Person I tried using a switch statement but this is messy and not ideal for my use case.

Could you help with a pull-request?

Yes

@Perks-of-Being-a-Cauliflower
Copy link
Author

After some more investigation, it looks like the issue with Person is that it is initialised when it's passed into Parse. When Populate() is called it generates a bunch of values for Person which in turn means that other values returned from Parse will not be returning the first expected value for the seed. I could just update the tests to use whatever the new value is but am hoping someone else has a more elegant solution. I was looking into doing a lazy load of person but it doesn't seem to work with the existing functionality.

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

No branches or pull requests

1 participant