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

1693 USA driving license #2090

Merged
merged 9 commits into from
Aug 15, 2020
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
9 changes: 9 additions & 0 deletions doc/default/driving_licence.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ Faker::DrivingLicence.northern_irish_driving_licence #=> "70702548"
# Keyword arguments: last_name, initials, date_of_birth, gender
Faker::DrivingLicence.uk_driving_licence #=> "OCARR815246J91HT"
Faker::DrivingLicence.uk_driving_licence #=> "70702548"

# Generate a USA driving licence number
# Default state for driving licence is California
# Keyword arguments: state
Faker::DrivingLicence.usa_driving_licence #=> "E124590"
# USA driving licence for Massachusetts state
Faker::DrivingLicence.usa_driving_licence('new mexico') #=> "85793820"
Faker::DrivingLicence.usa_driving_licence('New Mexico') #=> "57382918"
Faker::DrivingLicence.usa_driving_licence('NEW MEXICO') #=> "38593028"
23 changes: 23 additions & 0 deletions lib/faker/default/driving_licence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ def uk_driving_licence(*args)
end
end

##
# Produces a random USA driving licence number by state code passed.
#
# @return [String]
#
# @example
# Faker::DrivingLicence.usa_driving_licence #=> "V5598249"
# Faker::DrivingLicence.usa_driving_licence('new mexico') #=> "270692028"
# Faker::DrivingLicence.usa_driving_licence('New Mexico') #=> "68178637"
#
# @faker.version next
def usa_driving_licence(state = 'California')
sudeeptarlekar marked this conversation as resolved.
Show resolved Hide resolved
bothify(fetch("driving_licence.usa.#{state.to_s.strip.downcase.gsub(' ', '_')}"))
rescue I18n::MissingTranslationData => _e
raise InvalidStatePassed, "Invalid state code passed for USA, '#{state}'"
end

private

def random_gender
Expand All @@ -104,4 +121,10 @@ def gb_licence_checksum
end
end
end

class InvalidStatePassed < StandardError
def initialize(msg = 'Invalid state code passed')
super
end
end
end
178 changes: 178 additions & 0 deletions lib/locales/en/driving_license.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
en:
faker:
driving_licence:
usa:
alabama:
- '######'
- '#######'
- '########'
alaska:
- '#######'
- '######'
arizona:
- '?########'
- '#########'
arkansas:
- '########'
- '#########'
california:
- '?#######'
colorado:
- '#########'
- '?######'
- '??#####'
connecticut:
- '#########'
delaware:
- '######'
- '#######'
district_of_columbia:
- '#######'
- '#########'
florida:
- '?############'
georgia:
- '#######'
- '########'
- '#########'
hawaii:
- '?########'
- '#########'
idaho:
- '??######'
- '?#########'
illinois:
- '?###########'
- '?############'
indiana:
- '?#########'
- '#########'
- '##########'
iowa:
- '#########'
- '###??####'
kansas:
- '?#?#?'
- '?########'
- '#########'
kentucky:
- '?########'
- '?#########'
- '#########'
louisiana:
- '########'
- '#########'
maine:
- '#######'
- '#######?'
- '########'
maryland:
- '?############'
massachusetts:
- '?########'
- '#########'
michigan:
- '?##########'
- '?############'
minnesota:
- '?############'
mississippi:
- '#########'
missouri:
- '?#########'
- '?######R'
- '########??'
- '#########?'
- '#########'
montana:
- '?########'
- '#########'
- '#############'
- '##############'
nebraska:
- '?######'
- '?#######'
- '?########'
nevada:
- '#########'
- '##########'
- '############'
- 'X########'
new_hampshire:
- '##???#####'
new_jersey:
- '?##############'
new_mexico:
- '########'
- '#########'
new_york:
- '?#######'
- '?##################'
- '########'
- '##########'
- '################'
- '????????'
north_carolina:
- '##########'
- '############'
ohio:
- '?########'
- '??#######'
- '########'
oklahoma:
- '?#########'
- '#########'
oregon:
- '########'
- '#########'
pennsylvania:
- '########'
rhode_island:
- '#######'
- '?######'
south_carolina:
- '########'
- '#########'
- '##########'
- '###########'
south_dakota:
- '########'
- '#########'
- '##########'
- '############'
tennessee:
- '#######'
- '########'
- '#########'
texas:
- '#######'
- '########'
utah:
- '#########'
- '##########'
vermont:
- '########'
- '#######A'
virginia:
- '?########'
- '?#########'
- '?##########'
- '?###########'
- '#########'
washington:
- '???????#####'
- '????????####'
- '?????????###'
- '??????????##'
- '???????????#'
- '????????????'
- '???????####*'
west_virginia:
- '#######'
- '?######'
- '??#####'
wisconsin:
- '?#############'
wyoming:
- '#########'
- '##########'
26 changes: 26 additions & 0 deletions test/faker/default/test_faker_driving_licence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,30 @@ def test_british_licence_correctly_builds_initials
truncated = @tester.british_driving_licence(initials: 'NLTC')
assert_equal 'NL', truncated[11..12]
end

def test_usa_driving_licence
# When state is not passed to method it returns CA licence format by default
licence_number = @tester.usa_driving_licence
assert_match %r{[A-Z][0-9]{7}}, licence_number
end

def test_usa_driving_licence_for_different_states
# When state Washington is passed
licence_number = @tester.usa_driving_licence('Washington')
assert_match %r{[A-Z]{7,12}[0-9]{0,5}\**}, licence_number

# When state Alaska is passed
licence_number = @tester.usa_driving_licence('alaska')
assert_match %r{[0-9]{6,7}}, licence_number
end

def test_usa_driving_licence_with_faker_code
assert_raises(Faker::InvalidStatePassed) do
@tester.usa_driving_licence('abc')
end

assert_raises(Faker::InvalidStatePassed) do
@tester.usa_driving_licence(123)
end
end
end