Skip to content
Open
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
8 changes: 7 additions & 1 deletion lib/locales/en-GB.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
en-GB:
faker:
address:
postcode: "/[A-PR-UWYZ]([A-HK-Y][0-9][ABEHMNPRVWXY0-9]?|[0-9][ABCDEFGHJKPSTUW0-9]?) [0-9][ABD-HJLNP-UW-Z]{2}/"
postcode:
- "/[A-PR-UWYZ][0-9] [0-9][ABD-HJLNP-UW-Z]{2}/" # AN NAA (e.g. M1 1AA)
- "/[A-PR-UWYZ][0-9]{2} [0-9][ABD-HJLNP-UW-Z]{2}/" # ANN NAA (e.g. M60 1NW)
- "/[A-PR-UWYZ][A-HK-Y][0-9] [0-9][ABD-HJLNP-UW-Z]{2}/" # AAN NAA (e.g. CR2 6XH)
- "/[A-PR-UWYZ][A-HK-Y][0-9]{2} [0-9][ABD-HJLNP-UW-Z]{2}/" # AANN NAA (e.g. DN55 1PT)
- "/[A-PR-UWYZ][0-9][ABCDEFGHJKPSTUW] [0-9][ABD-HJLNP-UW-Z]{2}/" # ANA NAA (e.g. W1A 1HQ)
- "/[A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY] [0-9][ABD-HJLNP-UW-Z]{2}/" # AANA NAA (e.g. EC1A 1BB)
county:
- Avon
- Bedfordshire
Expand Down
17 changes: 17 additions & 0 deletions test/test_en_gb_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ def test_en_gb_postcode_has_outcode_and_incode
assert_equal 2, postcode.split.length
end

POSTCODE_FORMATS = Regexp.union(
/\A[A-PR-UWYZ][0-9] [0-9][ABD-HJLNP-UW-Z]{2}\z/, # AN NAA
/\A[A-PR-UWYZ][0-9]{2} [0-9][ABD-HJLNP-UW-Z]{2}\z/, # ANN NAA
/\A[A-PR-UWYZ][A-HK-Y][0-9] [0-9][ABD-HJLNP-UW-Z]{2}\z/, # AAN NAA
/\A[A-PR-UWYZ][A-HK-Y][0-9]{2} [0-9][ABD-HJLNP-UW-Z]{2}\z/, # AANN NAA
/\A[A-PR-UWYZ][0-9][ABCDEFGHJKPSTUW] [0-9][ABD-HJLNP-UW-Z]{2}\z/, # ANA NAA
/\A[A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY] [0-9][ABD-HJLNP-UW-Z]{2}\z/ # AANA NAA
)

def test_en_gb_postcode_always_matches_a_valid_format
100.times do
postcode = Faker::Address.postcode

assert_match POSTCODE_FORMATS, postcode, "#{postcode.inspect} is not a valid UK postcode format"
end
end

def test_en_gb_postcode_incode_is_valid
# The letters C I K M O V are not used in the second part of the Postcode.
incode = Faker::Address.postcode.split[1]
Expand Down