Skip to content

Commit

Permalink
1693 USA driving license (faker-ruby#2090)
Browse files Browse the repository at this point in the history
US driving licence for states
  • Loading branch information
sudeeptarlekar authored and tjozwik committed Sep 14, 2020
1 parent f0a84aa commit c2d8dc6
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 0 deletions.
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')
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

0 comments on commit c2d8dc6

Please sign in to comment.