From d745cd128ed25c6daf67724935c554464432eed8 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 11 May 2017 16:35:49 -0500 Subject: [PATCH] Add Address.mailing_address method. Add tests for Faker::Address. Fix bg with zip_code(state_abbreviation), nullifying need for one test in test_faker_zip_code.rb --- doc/address.md | 2 + lib/faker/address.rb | 4 ++ lib/locales/en.yml | 56 +++++++++++++++++++++++- test/test_faker_address.rb | 85 +++++++++++++++++++++++++++++++++++++ test/test_faker_zip_code.rb | 5 ++- 5 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 test/test_faker_address.rb diff --git a/doc/address.md b/doc/address.md index 4e770815ae..e61b6b65ec 100644 --- a/doc/address.md +++ b/doc/address.md @@ -37,4 +37,6 @@ Faker::Address.latitude #=> "-58.17256227443719" Faker::Address.longitude #=> "-156.65548382095133" +Faker::Address.mailing_address #=> "Jaylen Bode II\n1592 Volkman Canyon\n99837-5073\nLithuania" + ``` diff --git a/lib/faker/address.rb b/lib/faker/address.rb index 35a1d26d3a..585605e49d 100644 --- a/lib/faker/address.rb +++ b/lib/faker/address.rb @@ -35,6 +35,10 @@ def time_zone fetch('address.time_zone') end + def mailing_address + parse('address.mailing_address') + end + alias_method :zip, :zip_code alias_method :postcode, :zip_code diff --git a/lib/locales/en.yml b/lib/locales/en.yml index 95e5e3cedd..2289f02f0b 100755 --- a/lib/locales/en.yml +++ b/lib/locales/en.yml @@ -17,7 +17,59 @@ en: secondary_address: ['Apt. ###', 'Suite ###'] # Though these are US-specific, they are here (in the default locale) for backwards compatibility postcode: ['#####', '#####-####'] - postcode_by_state: ['#####', '#####-####'] + postcode_by_state: + AL: '350##' + AK: '995##' + AS: '967##' + AZ: '850##' + AR: '717##' + CA: '900##' + CO: '800##' + CT: '061##' + DC: '204##' + DE: '198##' + FL: '322##' + GA: '301##' + HI: '967##' + ID: '832##' + IL: '600##' + IN: '463##' + IA: '510##' + KS: '666##' + KY: '404##' + LA: '701##' + ME: '042##' + MD: '210##' + MA: '026##' + MI: '480##' + MN: '555##' + MS: '387##' + MO: '650##' + MT: '590##' + NE: '688##' + NV: '898##' + NH: '036##' + NJ: '076##' + NM: '880##' + NY: '122##' + NC: '288##' + ND: '586##' + OH: '444##' + OK: '730##' + OR: '979##' + PA: '186##' + RI: '029##' + SC: '299##' + SD: '577##' + TN: '383##' + TX: '798##' + UT: '847##' + VT: '050##' + VA: '222##' + WA: '990##' + WV: '247##' + WI: '549##' + WY: '831##' state: [Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware, Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina, North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina, South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia, Wisconsin, Wyoming] state_abbr: [AL, AK, AZ, AR, CA, CO, CT, DE, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA, WV, WI, WY] time_zone: [Pacific/Midway, Pacific/Pago_Pago, Pacific/Honolulu, America/Juneau, America/Los_Angeles, America/Tijuana, America/Denver, America/Phoenix, America/Chihuahua, America/Mazatlan, America/Chicago, America/Regina, America/Mexico_City, America/Mexico_City, America/Monterrey, America/Guatemala, America/New_York, America/Indiana/Indianapolis, America/Bogota, America/Lima, America/Lima, America/Halifax, America/Caracas, America/La_Paz, America/Santiago, America/St_Johns, America/Sao_Paulo, America/Argentina/Buenos_Aires, America/Guyana, America/Godthab, Atlantic/South_Georgia, Atlantic/Azores, Atlantic/Cape_Verde, Europe/Dublin, Europe/London, Europe/Lisbon, Europe/London, Africa/Casablanca, Africa/Monrovia, Etc/UTC, Europe/Belgrade, Europe/Bratislava, Europe/Budapest, Europe/Ljubljana, Europe/Prague, Europe/Sarajevo, Europe/Skopje, Europe/Warsaw, Europe/Zagreb, Europe/Brussels, Europe/Copenhagen, Europe/Madrid, Europe/Paris, Europe/Amsterdam, Europe/Berlin, Europe/Berlin, Europe/Rome, Europe/Stockholm, Europe/Vienna, Africa/Algiers, Europe/Bucharest, Africa/Cairo, Europe/Helsinki, Europe/Kiev, Europe/Riga, Europe/Sofia, Europe/Tallinn, Europe/Vilnius, Europe/Athens, Europe/Istanbul, Europe/Minsk, Asia/Jerusalem, Africa/Harare, Africa/Johannesburg, Europe/Moscow, Europe/Moscow, Europe/Moscow, Asia/Kuwait, Asia/Riyadh, Africa/Nairobi, Asia/Baghdad, Asia/Tehran, Asia/Muscat, Asia/Muscat, Asia/Baku, Asia/Tbilisi, Asia/Yerevan, Asia/Kabul, Asia/Yekaterinburg, Asia/Karachi, Asia/Karachi, Asia/Tashkent, Asia/Kolkata, Asia/Kolkata, Asia/Kolkata, Asia/Kolkata, Asia/Kathmandu, Asia/Dhaka, Asia/Dhaka, Asia/Colombo, Asia/Almaty, Asia/Novosibirsk, Asia/Rangoon, Asia/Bangkok, Asia/Bangkok, Asia/Jakarta, Asia/Krasnoyarsk, Asia/Shanghai, Asia/Chongqing, Asia/Hong_Kong, Asia/Urumqi, Asia/Kuala_Lumpur, Asia/Singapore, Asia/Taipei, Australia/Perth, Asia/Irkutsk, Asia/Ulaanbaatar, Asia/Seoul, Asia/Tokyo, Asia/Tokyo, Asia/Tokyo, Asia/Yakutsk, Australia/Darwin, Australia/Adelaide, Australia/Melbourne, Australia/Melbourne, Australia/Sydney, Australia/Brisbane, Australia/Hobart, Asia/Vladivostok, Pacific/Guam, Pacific/Port_Moresby, Asia/Magadan, Asia/Magadan, Pacific/Noumea, Pacific/Fiji, Asia/Kamchatka, Pacific/Majuro, Pacific/Auckland, Pacific/Auckland, Pacific/Tongatapu, Pacific/Fakaofo, Pacific/Apia] @@ -31,6 +83,8 @@ en: - "#{Name.last_name} #{street_suffix}" street_address: - "#{building_number} #{street_name}" + mailing_address: + - "#{Name.first_name} #{Name.last_name}\n#{building_number} #{street_name}\n#{postcode}\n#{country}" full_address: - "#{street_address}, #{city}, #{state_abbr} #{zip_code}" - "#{secondary_address} #{street_address}, #{city}, #{state_abbr} #{zip_code}" diff --git a/test/test_faker_address.rb b/test/test_faker_address.rb new file mode 100644 index 0000000000..a157264546 --- /dev/null +++ b/test/test_faker_address.rb @@ -0,0 +1,85 @@ +require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb') + +class TestFakerAddress < Test::Unit::TestCase + + def setup + @tester = Faker::Address + end + + def test_city + assert @tester.city.match(/\w+/) + end + + def test_street_name + assert @tester.street_name.match(/\w+\s\w+/) + end + + def test_street_address + assert @tester.street_address.match(/\d+\s\w+\s\w+/) + end + + def test_secondary_address + assert @tester.secondary_address.match(/\w+\.?\s\d+/) + end + + def test_building_number + assert @tester.building_number.match(/\d+/) + end + + def test_zip_code + assert @tester.zip_code.match(/^\d+-?\d*$/) + end + + def test_time_zone + assert @tester.time_zone.match(/\w+\/\w+/) + end + + def test_mailing_address + lines = @tester.mailing_address.split("\n") + assert_equal(lines.length, 4) + assert lines[0].match(/\w+/) + assert lines[1].match(/\d+\s\w+\s\w+/) + assert lines[2].match(/^\d+-?\d*$/) + assert lines[3].match(/\w+/) + end + + def test_street_suffix + assert @tester.street_suffix.match(/\w+/) + end + + def test_city_suffix + assert @tester.city_suffix.match(/\w+/) + end + + def test_city_prefix + assert @tester.city_prefix.match(/\w+/) + end + + def test_state_abbr + assert @tester.state_abbr.match(/[A-Z]{2}/) + end + + def test_state + assert @tester.state.match(/\w+/) + end + + def test_country + assert @tester.country.match(/\w+/) + end + + def test_country_code + assert @tester.country_code.match(/[A-Z]{2}/) + end + + def test_latitude + assert @tester.latitude.match(/-?\d+\.\d+/) + end + + def test_longitude + assert @tester.longitude.match(/-?\d+\.\d+/) + end + + def test_full_address + assert @tester.full_address.match(/\w*\.?\s?\d*\s?\d+\s\w+\s\w+,\s\w+\s?\w*,\s[A-Z]{2}\s\d+/) + end +end diff --git a/test/test_faker_zip_code.rb b/test/test_faker_zip_code.rb index 28e53b021f..bfb8d9ed1c 100644 --- a/test/test_faker_zip_code.rb +++ b/test/test_faker_zip_code.rb @@ -55,12 +55,13 @@ def test_zip_codes_with_states end end end - +=begin def test_zip_codes_with_states_with_inexisting_state assert_raises I18n::MissingTranslationData do I18n.with_locale(:xz) do @tester.zip_code('MI') - end + end end end +=end end