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

Fix minitest deprecation warnings #51

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
55 changes: 52 additions & 3 deletions lib/street_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ class US
'state' => STATE_CODES,
}

STREET_NUMBER_WORDS = %{one two three four five six seven eight nine ten}

class << self
attr_accessor(
:street_type_regexp,
Expand All @@ -533,8 +535,10 @@ class << self
:corner_regexp,
:unit_regexp,
:street_regexp,
:po_street_regexp,
:place_regexp,
:address_regexp,
:po_address_regexp,
:informal_address_regexp,
:dircode_regexp,
:unit_prefix_numbered_regexp,
Expand Down Expand Up @@ -602,6 +606,8 @@ class << self
)
/ix;

self.po_street_regexp = /^(?<street>p\.?o\.?\s?(?:box|\#)?\s\d\d*[-a-z]*)/ix;

# http://pe.usps.com/text/pub28/pub28c2_003.htm
# TODO add support for those that don't require a number
# TODO map to standard names/abbreviations
Expand Down Expand Up @@ -672,6 +678,14 @@ class << self
\z # right up to end of string
/ix;

self.po_address_regexp = /
\A
#{po_street_regexp} \W*
#{place_regexp}
\W* # require on non-word chars at end
\z # right up to end of string
/ix;

self.sep_regexp = /(?:\W+|\Z)/;
self.sep_avoid_unit_regexp = /(?:[^\#\w]+|\Z)/;

Expand Down Expand Up @@ -705,7 +719,7 @@ def parse(location, args={})
if( corner_regexp.match(location) )
return parse_intersection(location, args)
else
return parse_address(location, args) || parse_informal_address(location, args)
return parse_po_address(location, args) || parse_address(location, args) || parse_informal_address(location, args)
end
end

Expand All @@ -715,6 +729,12 @@ def parse_address(address, args={})
to_address( match_to_hash(match), args )
end

def parse_po_address(address, args={})
return unless match = po_address_regexp.match(address)

to_address(match_to_hash(match), args)
end

def parse_informal_address(address, args={})
return unless match = informal_address_regexp.match(address)

Expand Down Expand Up @@ -799,12 +819,17 @@ def to_address(input, args)
}
end

%w(street street_type street2 street_type2 city unit_prefix).each do |k|
input[k] = input[k].split.map(&:capitalize).join(' ') if input[k]
%w(street street_type street2 street_type2 city unit_prefix postal_code).each do |k|
input[k] = input[k].split.map { |elem| upcase_or_capitalize(elem) }.join(' ') if input[k]
end

return StreetAddress::US::Address.new( input )
end

def upcase_or_capitalize(elem)
return elem.upcase if elem.downcase.match(/^(po|ne|nw|sw|se)$/)
elem.capitalize
end
end

class Address
Expand Down Expand Up @@ -901,12 +926,36 @@ def to_s(format = :default)
s << line1(s)
when :line2
s << line2(s)
when :street_address_1
s << street_address_1
when :street_address_2
s << street_address_2
when :city_state_zip
s << line2(s)
else
s << [line1, line2].select{ |l| !l.empty? }.join(', ')
end
s
end

def street_address_1
s = ""
s = number + " " unless number.nil?
s += prefix + " " unless prefix.nil?
s += street + " " unless street.nil?
s += street_type unless street_type.nil?
s.strip
end

def street_address_2
s = ""
if( !unit_prefix.nil? && !unit.nil? )
s += unit_prefix + " " + unit
elsif( unit_prefix.nil? && !unit.nil? )
s += "#" + unit
end
s
end

def to_h
self.instance_variables.each_with_object({}) do |var_name, hash|
Expand Down
80 changes: 74 additions & 6 deletions test/address_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ class AddressTest < MiniTest::Test
},
"1005 N Gravenstein Highway, Sebastopol, CA" => {
:line1 => "1005 N Gravenstein Hwy",
:line2 => "Sebastopol, CA"
:line2 => "Sebastopol, CA",
:street_address_1 => "1005 N Gravenstein Hwy",
:street_address_2 => ""
},
"1005 N Gravenstein Highway, Suite 500, Sebastopol, CA" => {
:line1 => "1005 N Gravenstein Hwy Suite 500",
:line2 => "Sebastopol, CA"
:line2 => "Sebastopol, CA",
:street_address_1 => "1005 N Gravenstein Hwy",
:street_address_2 => "Suite 500"
},
"1005 N Gravenstein Hwy Suite 500 Sebastopol, CA" => {
:line1 => "1005 N Gravenstein Hwy Suite 500",
Expand Down Expand Up @@ -134,6 +138,40 @@ class AddressTest < MiniTest::Test
"44 Canal Center Plaza Suite 500, Alexandria, VA 22314" => {
:line1 => "44 Canal Center Plz Suite 500",
:line2 => "Alexandria, VA 22314"
},
"One East 161st Street, Bronx, NY 10451" => {
:line1 => "One East 161st St",
:line2 => "Bronx, NY 10451",
:street_address_1 => "One East 161st St",
:street_address_2 => ""
},
"One East 161st Street Suite 10, Bronx, NY 10451" => {
:line1 => "One East 161st St Suite 10",
:line2 => "Bronx, NY 10451",
:street_address_1 => "One East 161st St",
:street_address_2 => "Suite 10"
},
"P.O. Box 280568 Queens Village, New York 11428" => {
:line1 => "PO Box 280568",
:line2 => "Queens Village, NY 11428"
},
"PO BOX 280568 Queens Village, New York 11428" => {
:line1 => "PO Box 280568",
:line2 => "Queens Village, NY 11428",
:street_address_1 => "PO Box 280568",
:street_address_2 => ""
},
"PO 280568 Queens Village, New York 11428" => {
:line1 => "PO 280568",
:line2 => "Queens Village, NY 11428"
},
"Two Pennsylvania Plaza New York, NY 10121-0091" => {
:line1 => "Two Pennsylvania Plz",
:line2 => "New York, NY 10121-0091"
},
"1400 CONNECTICUT AVE NW, WASHINGTON, DC 20036" => {
:line1 => "1400 Connecticut Ave NW",
:line2 => "Washington, DC 20036"
}
}

Expand Down Expand Up @@ -199,20 +237,27 @@ class AddressTest < MiniTest::Test
:line1 => "233 S Wacker Dr Lobby",
:line2 => "60606"
}
#FIXME
# FIXME
# "(233 S Wacker Dr lobby 60606)" => {
# :line1 => "233 S Wacker Dr Lobby",
# :line2 => ""}
# :line2 => ""}
}


def test_line1_with_valid_addresses
ADDRESSES.each_pair do |address, expected|
addr = StreetAddress::US.parse(address)
assert_equal addr.line1, expected[:line1]
assert_equal expected[:line1], addr.line1
end
end

def test_street_address_1_with_valid_addresses
ADDRESSES.select { |k,v| !v[:street_address_1].nil? }.each_pair do |address, expected|
addr = StreetAddress::US.parse(address)
assert_equal expected[:street_address_1], addr.street_address_1
assert_equal expected[:street_address_2], addr.street_address_2
end
end

def test_line1_with_intersections
INTERSECTIONS.each_pair do |address, expected|
Expand Down Expand Up @@ -259,7 +304,7 @@ def test_to_s_with_valid_addresses
addr = StreetAddress::US.parse(address)
expected_result = expected[:to_s]
expected_result ||= [expected[:line1], expected[:line2]].select{|l| !l.empty?}.join(', ')
assert_equal addr.to_s, expected_result
assert_equal expected_result, addr.to_s
end
end

Expand Down Expand Up @@ -311,6 +356,29 @@ def test_to_s_for_line2
assert_equal addr.to_s(:line2), addr.line2
end

def test_to_s_for_street_address_1
address = "7800 Mill Station Rd, Apt. 7B, Sebastopol CA 95472-1234"
addr = StreetAddress::US.parse(address)
assert_equal addr.to_s(:street_address_1), "7800 Mill Station Rd"
end

def test_to_s_for_street_address_2
address = "7800 Mill Station Rd, Apt. 7B, Sebastopol CA 95472-1234"
addr = StreetAddress::US.parse(address)
assert_equal addr.to_s(:street_address_2), "Apt 7B"
end

def test_to_s_for_city_state_zip
address = "7800 Mill Station Rd Sebastopol CA 95472-1234"
addr = StreetAddress::US.parse(address)
assert_equal addr.to_s(:line2), addr.line2
end

def test_to_s_for_PO_box
address = "PO 7800 Sebastopol CA 95472-1234"
addr = StreetAddress::US.parse(address)
assert_equal "PO 7800", addr.to_s(:line1)
end

def test_full_postal_code
address = "7800 Mill Station Rd Sebastopol CA 95472-1234"
Expand Down
31 changes: 24 additions & 7 deletions test/street_address_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class StreetAddressUsTest < MiniTest::Test
:city => "Alexandria",
:street2 => nil
},
"1600 Pennsylvania Ave Washington DC" => {
"1600 Pennsylvania Ave NW Washington DC" => {
:number => "1600",
:postal_code => nil,
:prefix => nil,
Expand All @@ -311,7 +311,8 @@ class StreetAddressUsTest < MiniTest::Test
:unit => nil,
:unit_prefix => nil,
:city => "Washington",
:street2 => nil
:street2 => nil,
:suffix => "NW"
},
"1005 Gravenstein Hwy N, Sebastopol CA 95472" => {
:number => "1005",
Expand All @@ -338,6 +339,20 @@ class StreetAddressUsTest < MiniTest::Test
:state=>"VA",
:postal_code=>"22206",
:postal_code_ext=>nil
},

"P.O. BOX 293930, ARLINGTON, VA 22206" => {
:number=>nil,
:street=>"PO Box 293930",
:street_type=>nil,
:unit=>nil,
:unit_prefix=>nil,
:suffix=>nil,
:prefix=>nil,
:city=>"Arlington",
:state=>"VA",
:postal_code=>"22206",
:postal_code_ext=>nil
}
}

Expand Down Expand Up @@ -489,8 +504,6 @@ class StreetAddressUsTest < MiniTest::Test
"Gravenstein Hwy 95472",
"E1005 Gravenstein Hwy 95472",
# "1005E Gravenstein Hwy 95472"
## adding from original ruby test suite
"PO BOX 450, Chicago IL 60657"
]


Expand Down Expand Up @@ -581,8 +594,8 @@ def test_informal_parse_informal_address_trailing_words
end

def test_parse
assert_equal StreetAddress::US.parse("&"), nil
assert_equal StreetAddress::US.parse(" and "), nil
assert_nil StreetAddress::US.parse("&")
assert_nil StreetAddress::US.parse(" and ")

parseable = [
"1600 Pennsylvania Ave Washington DC 20006",
Expand Down Expand Up @@ -617,7 +630,11 @@ def test_comparison

def compare_expected_to_actual_hash(expected, actual, address)
expected.each_pair do |expected_key, expected_value|
assert_equal actual[expected_key], expected_value, "For address '#{address}', #{actual[expected_key]} != #{expected_value}"
if expected_value.nil?
assert_nil actual[expected_key], "For address '#{address}', expected nil value for field '#{expected_key}', got: #{actual[expected_key]}"
else
assert_equal actual[expected_key], expected_value, "For address '#{address}', expected value '#{expected[expected_key]}' for field '#{expected_key}', got: #{actual[expected_key]}"
end
end
end

Expand Down