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

Fix: ensure sport generator produces single sport unless a number of sports is requested #2616

Merged

Conversation

si-lens
Copy link
Contributor

@si-lens si-lens commented Nov 3, 2022

Summary

Fixes Issue #2608
there was a bug that resulted in Faker::Sport.sport(include_ancient: true) generating an array of sports instead of a single sport.
The problem was caused by this line.
When using << to add ancient_olympics sports to the sports array we were creating a nested array that looked like that:

      ['3x3 basketball', 'Archery', 'Artistic gymnastics', 'Artistic swimming', 'Athletics',
       'Badminton', 'Baseball', 'Basketball', 'Beach volleyball', 'BMX freestyle', 'BMX racing',
       'Boxing', 'Canoe/kayak flatwater', 'Canoe/kayak slalom', 'Diving', 'Equestrian', 'Fencing',
       'Football', 'Golf', 'Handball', 'Hockey', 'Judo', 'Karate', 'Marathon swimming', 'Modern pentathlon',
       'Mountain bike', 'Rhythmic gymnastics', 'Road cycling', 'Rowing', 'Rugby', 'Sailing', 'Shooting',
       'Skateboarding', 'Softball', 'Sport climbing', 'Surfing', 'Swimming', 'Table tennis', 'Taekwondo',
       'Tennis', 'Track cycling', 'Trampoline', 'Triathlon', 'Volleyball', 'Water polo', 'Weight lifting', 'Wrestling',
       'Alpine skiing', 'Biathlon', 'Bobsleigh', 'Cross-country', 'Curling', 'Figure skating', 'Freestyle skiing',
       'Ice hockey', 'Luge', 'Nordic combined', 'Short track speed skating', 'Skeleton', 'Ski jumping', 'Snowboard',
       'Speed skating', 'Archery', 'Athletics', 'Badminton', 'Boccia', 'Canoe', 'Cycling', 'Equestrian',
       'Football (5-a-side)', 'Goalball', 'Judo', 'Powerlifting', 'Rowing', 'Shooting', 'Sitting volleyball',
       'Swimming', 'Table tennis', 'Taekwondo', 'Triathlon', 'Wheelchair basketball', 'Wheelchair fencing',
       'Wheelchair rugby', 'Wheelchair tennis', 'Alpine skiing', 'Biathlon', 'Cross-country skiing',
       'Para ice hockey', 'Snowboard', 'Wheelchair curling',
       ['Boxing', 'Chariot racing', 'Discus', 'Horse racing', 'Long jump', 'Pankration', 'Pentathlon', 'Running', 'Wrestling']]

So once in a while, this last element was picked by the .sample method and caused the problem.

I changed the sport method to use concat method to combine arrays correctly.

Other Information

This way we won't create nested arrays, plus it's faster.
@si-lens si-lens force-pushed the fix_nested_array_bug_when_producing_sport branch from 609b3ac to b2787e8 Compare November 3, 2022 10:48
@si-lens
Copy link
Contributor Author

si-lens commented Nov 3, 2022

In docs it states:
Only refactoring and documentation changes require no new tests. If you are adding functionality or fixing a bug, we need a test! We use [Minitest](https://github.com/seattlerb/minitest) in this project.
And I am not really sure whether I should write a test for that. If so, what kind of test would you like to have to cover it?

@si-lens si-lens marked this pull request as ready for review November 3, 2022 10:59
Copy link
Contributor

@thdaraujo thdaraujo left a comment

Choose a reason for hiding this comment

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

nice catch, thank you!

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