coderifous-address_extractor 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('address_extractor', '0.1.1') do |p|
5
+ Echoe.new('address_extractor', '0.1.2') do |p|
6
6
  p.description = "Give it text. It finds addresses in it."
7
7
  p.url = "http://github.com/coderifous/address_extractor"
8
8
  p.author = "Jim Garvin"
@@ -1,14 +1,14 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{address_extractor}
3
- s.version = "0.1.1"
3
+ s.version = "0.1.2"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["Jim Garvin"]
7
- s.date = %q{2008-11-14}
7
+ s.date = %q{2008-11-16}
8
8
  s.description = %q{Give it text. It finds addresses in it.}
9
9
  s.email = %q{jim at thegarvin dot com}
10
10
  s.extra_rdoc_files = ["lib/address_extractor.rb", "LICENSE.textile", "README.textile"]
11
- s.files = ["lib/address_extractor.rb", "LICENSE.textile", "Manifest", "Rakefile", "README.textile", "test/test_address_extractor.rb", "address_extractor.gemspec"]
11
+ s.files = ["lib/address_extractor.rb", "LICENSE.textile", "Manifest", "Rakefile", "README.textile", "test/test_address_extractor.rb", "address_extractor.gemspec", "test/test_helper.rb"]
12
12
  s.has_rdoc = true
13
13
  s.homepage = %q{http://github.com/coderifous/address_extractor}
14
14
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Address_extractor", "--main", "README.textile"]
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.rubyforge_project = %q{address_extractor}
17
17
  s.rubygems_version = %q{1.2.0}
18
18
  s.summary = %q{Give it text. It finds addresses in it.}
19
- s.test_files = ["test/test_address_extractor.rb"]
19
+ s.test_files = ["test/test_address_extractor.rb", "test/test_helper.rb"]
20
20
 
21
21
  if s.respond_to? :specification_version then
22
22
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -152,7 +152,7 @@ class AddressExtractor
152
152
  (
153
153
  \d+ # A few numbers
154
154
  \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
156
156
  )
157
157
  \s* ,? \s*
158
158
  (
@@ -163,8 +163,8 @@ class AddressExtractor
163
163
  \s* ,? \s* # a comma, optionally
164
164
  (?:
165
165
  (?:
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
168
168
  \b(#{STATE_REGEX})\b # state
169
169
  \s* ,? \s* # a comma, optionally
170
170
  (\d{6})? # a zip code, optionally
@@ -2,77 +2,79 @@ $: << File.dirname(__FILE__)+"/../lib"
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,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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coderifous-address_extractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Garvin
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-14 00:00:00 -08:00
12
+ date: 2008-11-16 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -31,6 +31,7 @@ files:
31
31
  - README.textile
32
32
  - test/test_address_extractor.rb
33
33
  - address_extractor.gemspec
34
+ - test/test_helper.rb
34
35
  has_rdoc: true
35
36
  homepage: http://github.com/coderifous/address_extractor
36
37
  post_install_message:
@@ -64,3 +65,4 @@ specification_version: 2
64
65
  summary: Give it text. It finds addresses in it.
65
66
  test_files:
66
67
  - test/test_address_extractor.rb
68
+ - test/test_helper.rb