|
2 | 2 |
|
3 | 3 | require 'test/unit'
|
4 | 4 | require 'address_extractor.rb'
|
| 5 | +require 'test_helper.rb' |
| 6 | +include TestDataHelper |
5 | 7 |
|
6 | 8 | class AddressExtractorTest < Test::Unit::TestCase
|
| 9 | + include Helpers |
7 | 10 |
|
8 | 11 | 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 |
11 | 17 | end
|
12 | 18 |
|
13 | 19 | 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 |
17 | 27 | end
|
18 | 28 |
|
19 | 29 | 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 |
23 | 33 | "skidoosh"
|
24 | 34 | end
|
25 | 35 | assert string =~ /Please send the package to skidoosh/
|
26 | 36 | end
|
27 |
| - |
| 37 | + |
28 | 38 | 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| |
30 | 40 | "skidoosh"
|
31 | 41 | end
|
32 | 42 | assert string =~ /Please send the package to skidoosh/
|
33 |
| - assert string =~ /via mail at:\n skidoosh/ |
| 43 | + assert string =~ /via mail at:\s+skidoosh/ |
34 | 44 | end
|
35 |
| - |
| 45 | + |
36 | 46 | def test_no_addresses_found
|
37 | 47 | assert_nil AddressExtractor.first_address("foo")
|
38 | 48 | assert_equal [], AddressExtractor.find_addresses("foo")
|
39 | 49 | assert_equal "foo", AddressExtractor.replace_first_address("foo")
|
40 | 50 | assert_equal "foo", AddressExtractor.replace_addresses("foo")
|
41 | 51 | 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 |
68 | 52 | end
|
69 | 53 |
|
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" } |
72 | 80 |
|
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 |
|
0 commit comments