Skip to content

Commit f33014e

Browse files
committed
Refactored tests, added test data, improved the extractor regex.
* Test data now stored in input => expected_output structure * new test input/output added easily w/ the test_input helper * each test iterates over all of the test data * added a few new address formats to the tests, improved regex to recognize those additional formats.
1 parent 0c94838 commit f33014e

File tree

3 files changed

+82
-49
lines changed

3 files changed

+82
-49
lines changed

lib/address_extractor.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def useful_address?(hash)
152152
(
153153
\d+ # A few numbers
154154
\s+
155-
(?:[A-Za-z'.-]+\s?){1,3} # Followed by a street name
155+
(?:[A-Za-z'.-]+\s?){0,2} (?:[A-Za-z'.-]+) # Followed by a street name
156156
)
157157
\s* ,? \s*
158158
(
@@ -163,8 +163,8 @@ def useful_address?(hash)
163163
\s* ,? \s* # a comma, optionally
164164
(?:
165165
(?:
166-
((?:[A-Za-z]+\s?){1,3}) # city
167-
\s+
166+
( (?:[A-Za-z]+\s?){0,2} (?:[A-Za-z]+) ) # city
167+
\s* ,? \s* # a comma, optionally
168168
\b(#{STATE_REGEX})\b # state
169169
\s* ,? \s* # a comma, optionally
170170
(\d{6})? # a zip code, optionally

test/test_address_extractor.rb

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,79 @@
22

33
require 'test/unit'
44
require 'address_extractor.rb'
5+
require 'test_helper.rb'
6+
include TestDataHelper
57

68
class AddressExtractorTest < Test::Unit::TestCase
9+
include Helpers
710

811
def test_first_address_extraction
9-
address = AddressExtractor.first_address(DATA1)
10-
assert_first_address(address)
12+
each_test_data do |test_data|
13+
address = AddressExtractor.first_address(test_data[:input])
14+
flunk "No address found in:\n#{test_data[:input]}" if address.nil?
15+
assert_equal_hashes address, test_data[:expected_output].first
16+
end
1117
end
1218

1319
def test_find_addresses
14-
addresses = AddressExtractor.find_addresses(DATA1)
15-
assert_first_address addresses[0]
16-
assert_second_address addresses[1]
20+
each_test_data do |test_data|
21+
addresses = AddressExtractor.find_addresses(test_data[:input])
22+
assert_equal addresses.size, test_data[:expected_output].size
23+
test_data[:expected_output].each do |expected_output|
24+
assert_equal_hashes addresses.shift, expected_output
25+
end
26+
end
1727
end
1828

1929
def test_replace_first_address
20-
string = AddressExtractor.replace_first_address(DATA1) do |address_hash, address|
21-
assert_first_address address_hash
22-
assert_first_address_string address
30+
string = AddressExtractor.replace_first_address(test_data.first[:input]) do |address_hash, address|
31+
assert_equal_hashes address_hash, test_data.first[:expected_output].first
32+
assert_match /^\s*123 Foo St., Someplace FL\s*/, address
2333
"skidoosh"
2434
end
2535
assert string =~ /Please send the package to skidoosh/
2636
end
27-
37+
2838
def test_replace_addresses
29-
string = AddressExtractor.replace_addresses(DATA1) do |address_hash, address|
39+
string = AddressExtractor.replace_addresses(test_data.first[:input]) do |address_hash, address|
3040
"skidoosh"
3141
end
3242
assert string =~ /Please send the package to skidoosh/
33-
assert string =~ /via mail at:\n skidoosh/
43+
assert string =~ /via mail at:\s+skidoosh/
3444
end
35-
45+
3646
def test_no_addresses_found
3747
assert_nil AddressExtractor.first_address("foo")
3848
assert_equal [], AddressExtractor.find_addresses("foo")
3949
assert_equal "foo", AddressExtractor.replace_first_address("foo")
4050
assert_equal "foo", AddressExtractor.replace_addresses("foo")
4151
end
42-
43-
module Helpers
44-
def assert_first_address(a)
45-
assert_not_nil a
46-
assert_equal "123 Foo St.", a[:street1]
47-
assert_equal nil, a[:street2]
48-
assert_equal "Someplace", a[:city]
49-
assert_equal "FL", a[:state]
50-
assert_equal nil, a[:zip]
51-
end
52-
53-
def assert_first_address_string(string)
54-
assert_match /^123 Foo St\., Someplace FL\s*$/, string
55-
end
56-
57-
58-
def assert_second_address(a)
59-
assert_not_nil a
60-
assert_equal "123 Goob Avenue", a[:street1]
61-
assert_equal "Apt 123", a[:street2]
62-
assert_equal "Nice Town", a[:city]
63-
assert_equal "CA", a[:state]
64-
assert_equal "123456", a[:zip]
65-
end
66-
end
67-
include Helpers
6852
end
6953

70-
DATA1 = <<EOF
71-
Please send the package to 123 Foo St., Someplace FL
54+
# Test Input/Expected outputs defined below using test_input helper
55+
# Expanding the tests will probably start with adding new test input
56+
57+
test_input "
58+
Please send the package to 123 Foo St., Someplace FL
59+
60+
My phone number is 123-1234 and St. Marc of Israel can be reached
61+
via mail at:
62+
123 Goob Avenue
63+
Apt 123
64+
Nice Town CA 123456
65+
",
66+
{ :street1 => "123 Foo St.", :street2 => nil, :city => "Someplace", :state => "FL", :zip => nil },
67+
{ :street1 => "123 Goob Avenue", :street2 => "Apt 123", :city => "Nice Town", :state => "CA", :zip => "123456" }
68+
69+
test_input "Let's meet tomorrow at noon at 123 Foo Bar Street, Scooby NY 123456",
70+
{ :street1 => "123 Foo Bar Street", :street2 => nil, :city => "Scooby", :state => "NY", :zip => "123456" }
71+
72+
test_input "Let's meet tomorrow at noon at 123 Foo Bar Street, Scooby, NY 123456",
73+
{ :street1 => "123 Foo Bar Street", :street2 => nil, :city => "Scooby", :state => "NY", :zip => "123456" }
74+
75+
test_input "Let's meet tomorrow at noon at 123 Foo Bar Street, Scooby, NY, 123456",
76+
{ :street1 => "123 Foo Bar Street", :street2 => nil, :city => "Scooby", :state => "NY", :zip => "123456" }
77+
78+
test_input "Let's meet tomorrow at noon at 123 Foo Bar Street, 123456",
79+
{ :street1 => "123 Foo Bar Street", :street2 => nil, :city => nil, :state => nil, :zip => "123456" }
7280

73-
My phone number is 123-1234 and St. Marc of Israel can be reached
74-
via mail at:
75-
123 Goob Avenue
76-
Apt 123
77-
Nice Town CA 123456
78-
EOF

test/test_helper.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module TestDataHelper
2+
3+
TEST_DATA = []
4+
5+
def test_input(input_string, *expected_outputs)
6+
test_data << { :input => input_string, :expected_output => expected_outputs }
7+
end
8+
9+
def each_test_data
10+
test_data.each { |t| yield(t) }
11+
end
12+
13+
def test_data
14+
TEST_DATA
15+
end
16+
17+
end
18+
19+
module Helpers
20+
21+
def assert_equal_hashes(a,b)
22+
(a.keys + b.keys).uniq.each do |k|
23+
assert_equal a[k], b[k], "a[#{k.inspect}] = #{a[k].inspect} != b[#{k.inspect}] = #{b[k].inspect}"
24+
end
25+
end
26+
27+
def assert_first_address_string(string)
28+
assert_match /^123 Foo St\., Someplace FL\s*$/, string
29+
end
30+
31+
end

0 commit comments

Comments
 (0)