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 chess sport #2701

Merged
merged 1 commit into from
Feb 8, 2023
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main

- [Faker::Sports](doc/sports/sports.md)
- [Faker::Sports::Basketball](doc/sports/basketball.md)
- [Faker::Sports::Chess](doc/sports/chess.md)
- [Faker::Sports::Football](doc/sports/football.md)
- [Faker::Sports::Mountaineering](doc/sports/mountaineering.md)
- [Faker::Sports::Volleyball](doc/sports/volleyball.md)
Expand Down
17 changes: 17 additions & 0 deletions doc/sports/chess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Faker::Sports::Chess

```ruby
Faker::Sports::Chess.player #=> "Emanuel Lasker"

Faker::Sports::Chess.federation #=> "CUB"

Faker::Sports::Chess.tournament #=> "Khanty-Mansisyk (Grand Prix 2014–2015)"

Faker::Sports::Chess.rating #=> "2514"

Faker::Sports::Chess.rating(from: 2600, to: 2700) #=> "2688"

Faker::Sports::Chess.opening #=> "Queen’s Indian Defense"

Faker::Sports::Chess.title #=> "WGM"
```
90 changes: 90 additions & 0 deletions lib/faker/sports/chess.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# frozen_string_literal: true

module Faker
class Sports
class Chess < Base
class << self
##
# Produces the name of a chess player name.
#
# @return [String]
#
# @example
# Faker::Sports::Chess.player #=> "Golden State Warriors"
#
# @faker.version next
def player
fetch('chess.players')
end

##
# Produces a long (alpha-3) ISO 3166 country code.
#
# @return [String]
#
# @example
# Faker::Chess.federation #=> "COL"
#
# @faker.version next
def federation
Faker::Address.country_code_long
end

def tournament
##
# Produces the name of a famous chess tournament name.
#
# @return [String]
#
# @example
# Faker::Chess.tournament #=> "Khanty-Mansisyk (Candidates Tournament)"
#
# @faker.version next
fetch('chess.tournaments')
end

def rating(from: 2000, to: 2900)
##
# Produces a rating between two provided values. Boundaries are inclusive.
#
# @param from [Numeric] The lowest number to include.
# @param to [Numeric] The highest number to include.
# @return [Numeric]
#
# @example
# Faker::Sports::Chess.rating #=> 2734
# Faker::Sports::Chess.rating(from: 2400, to: 2700) #=> 2580
#
# @faker.version next
Faker::Base.rand_in_range(from, to)
end

##
# Produces the name of a chess opening.
#
# @return [String]
#
# @example
# Faker::Sports::Chess.opening #=> "Giuoco Piano"
#
# @faker.version next
def opening
fetch('chess.openings')
end

##
# Produces a chess title.
#
# @return [String]
#
# @example
# Faker::Sports::Chess.title #=> "GM"
#
# @faker.version next
def title
fetch('chess.titles')
end
end
end
end
end
103 changes: 103 additions & 0 deletions lib/locales/en/chess.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
en:
faker:
chess:
players:
- Alexander Alekhine
- Alexei Shirov
- Alexis Vargas
- Anatoly Karpov
- Bobby Fischer
- Emanuel Lasker
- Fabiano Caruana
- Garry Kasparov
- Hikaru Nakamura
- Jose Raul Capablanca
- Levon Aronian
- Magnus Carlsen
- Mikhail Botvinnik
- Radjabov Teimour
- Sergey Karjakin
- Tigran Petrosian
- Viswanathan Anand
- Vladimir Kramnik
- Wesley So
- Paul Morphy
tournaments:
- Wijk aan Zee
- Linares
- Astrakhan
- Dortmund
- Shanghai
- Bilbao
- Nanjing
- Moscow
- London
- Moscow
- Tromsø (Chess World Cup)
- Paris (Grand Prix 2012–2013)
- Bucharest
- Nizhny Novgorod (Russian Championship)
- Zurich
- Khanty-Mansisyk (Candidates Tournament)
- Tbilisi (Grand Prix 2014–2015)
- Khanty-Mansisyk (Grand Prix 2014–2015)
- Baku (Chess World Cup)
- London (Grand Chess Tour)
- Gibraltar
openings:
- Alekhine’s Defense
- Benko Gambit
- Benoni Defense
- Bird’s Opening
- Bogo-Indian Defense
- Budapest Gambit
- Catalan Opening
- Caro-Kann Defense
- Colle System
- Dutch Defense
- Giuoco Piano
- English Opening
- Evans Gambit
- Four Knights Game
- French Defense
- Grünfeld Defense
- Italian Game
- King’s Gambit
- King’s Indian Attack
- King’s Indian Defense
- King’s Pawn Game
- London System
- Modern Defense
- Nimzo-Indian Defense
- Nimzowitsch Defense
- Petrov’s Defense
- Philidor’s Defense
- Pirc Defense
- Queen’s Pawn Game
- Queen’s Gambit Accepted
- Queen’s Gambit Declined
- Queen’s Indian Defense
- Réti Opening
- Ruy Lopez
- Scandinavian Defense
- Scotch Game
- Sicilian Defense
- Slav Defense
- Torre Attack
- Two Knights Defense
- Vienna Game
- Wade Defense
titles:
- GM
- IM
- FM
- CM
- WGM
- WIM
- WFM
- WCM
- AGM
- AIM
- AFM
- ACM

38 changes: 38 additions & 0 deletions test/faker/sports/test_faker_chess.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require_relative '../../test_helper'

class TestFakerChess < Test::Unit::TestCase
def setup
@tester = Faker::Sports::Chess
end

def test_player
assert_match(/\w+/, @tester.player)
end

def test_federation
assert_match(/\w+/, @tester.federation)
end

def test_tournament
assert_match(/\w+/, @tester.tournament)
end

def test_rating
100.times do
random_number = @tester.rating(from: 2000, to: 2900)

assert random_number >= 2000, "Expected >= 2000, but got #{random_number}"
assert random_number <= 2900, "Expected <= 2900, but got #{random_number}"
end
end

def test_opening
assert_match(/\w+/, @tester.opening)
end

def test_title
assert_match(/\w+/, @tester.title)
end
end