uk_postcode 1.0.1 → 2.0.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
data/test/tree_test.rb ADDED
@@ -0,0 +1,84 @@
1
+ require_relative "./test_helper"
2
+ require "uk_postcode/tree"
3
+
4
+ describe UKPostcode::Tree do
5
+ it "should build a nested hash via insert" do
6
+ tree = UKPostcode::Tree.new
7
+ tree.insert %w[a b c d e], :foo
8
+ tree.insert %w[a b c d f], :bar
9
+ tree.insert %w[a b g h i], :baz
10
+
11
+ expected = {
12
+ "a" => {
13
+ "b" => {
14
+ "c" => { "d" => { "e" => :foo, "f" => :bar } },
15
+ "g" => { "h" => { "i" => :baz } }
16
+ }
17
+ }
18
+ }
19
+
20
+ tree.to_h.must_equal expected
21
+ end
22
+
23
+ it "should compress paths to identical leaves" do
24
+ tree = UKPostcode::Tree.new
25
+ tree.insert %w[a b c d e], :foo
26
+ tree.insert %w[a b c d f], :foo
27
+ tree.insert %w[a b g h i], :baz
28
+
29
+ expected = {
30
+ "a" => {
31
+ "b" => {
32
+ "c" => :foo,
33
+ "g" => :baz
34
+ }
35
+ }
36
+ }
37
+
38
+ tree.compress.to_h.must_equal expected
39
+ end
40
+
41
+ it "should filter the tree to include only paths to specific leaf values" do
42
+ tree = UKPostcode::Tree.new
43
+ tree.insert %w[a b c d e], :foo
44
+ tree.insert %w[a b c d f], :bar
45
+ tree.insert %w[a b g h i], :baz
46
+
47
+ expected = {
48
+ "a" => {
49
+ "b" => {
50
+ "c" => { "d" => { "e" => :foo } }
51
+ }
52
+ }
53
+ }
54
+
55
+ tree.filter(:foo).to_h.must_equal expected
56
+ end
57
+
58
+ it "should generate a minimal regular expression for the tree" do
59
+ tree = UKPostcode::Tree.new
60
+ tree.insert %w[a b c d e], :foo
61
+ tree.insert %w[a b c d f], :foo
62
+ tree.insert %w[a b g h i], :foo
63
+ tree.insert %w[j k], :foo
64
+
65
+ expected = /^(?:ab(?:cd[ef]|ghi)|jk)/
66
+
67
+ tree.regexp.must_equal expected
68
+ end
69
+
70
+ it "should generate a working regular expression for the tree" do
71
+ tree = UKPostcode::Tree.new
72
+ tree.insert %w[a b c d e], :foo
73
+ tree.insert %w[a b c d f], :foo
74
+ tree.insert %w[a b g h i], :foo
75
+ tree.insert %w[j k], :foo
76
+
77
+ regexp = tree.regexp
78
+ regexp.must_be :match, 'abcde'
79
+ regexp.must_be :match, 'abcdf'
80
+ regexp.must_be :match, 'abghi'
81
+ regexp.must_be :match, 'jk'
82
+ regexp.wont_be :match, 'abcdg'
83
+ end
84
+ end
@@ -2,277 +2,29 @@ require_relative "./test_helper"
2
2
  require "uk_postcode"
3
3
 
4
4
  describe UKPostcode do
5
-
6
- VALID_SAMPLES = [
7
- %w[A 9 9 AA], %w[A 99 9 AA], %w[AA 9 9 AA], %w[AA 99 9 AA], %w[A 9A 9 AA],
8
- %w[AA 9A 9 AA], %w[SW 1A 0 AA], %w[SW 1A 0 PW], %w[SW 1A 1 AA], %w[SW 1A 2 HQ],
9
- %w[W 1A 1 AA], %w[W 1A 1 AB], %w[N 81 1 ER], %w[EH 99 1 SP], %w[CV 1 1 FL],
10
- %w[EX 1 1 AE], %w[TQ 1 1 AG]
11
- ]
12
-
13
- { "full samples with spaces" => lambda{ |(a,b,c,d)| [[a, b, " ", c, d].join, [a, b, c, d]] },
14
- "full samples without spaces" => lambda{ |(a,b,c,d)| [[a, b, c, d].join, [a, b, c, d]] },
15
- }.each do |desc, mapping|
16
- describe desc do
17
- let(:samples) {
18
- VALID_SAMPLES.map(&mapping)
19
- }
20
-
21
- it "should all be valid" do
22
- samples.each do |sample, _|
23
- UKPostcode.new(sample).must_be :valid?
24
- end
25
- end
26
-
27
- it "should all be full" do
28
- samples.each do |sample, _|
29
- UKPostcode.new(sample).must_be :full?
30
- end
31
- end
32
-
33
- it "should extract outcodes" do
34
- samples.each do |sample, parts|
35
- UKPostcode.new(sample).outcode.must_equal parts[0] + parts[1]
36
- end
37
- end
38
-
39
- it "should extract incodes" do
40
- samples.each do |sample, parts|
41
- UKPostcode.new(sample).incode.must_equal parts[2] + parts[3]
42
- end
43
- end
44
-
45
- it "should extract area" do
46
- samples.each do |sample, parts|
47
- UKPostcode.new(sample).area.must_equal parts[0]
48
- end
49
- end
50
-
51
- it "should extract district" do
52
- samples.each do |sample, parts|
53
- UKPostcode.new(sample).district.must_equal parts[1]
54
- end
55
- end
56
-
57
- it "should extract sector" do
58
- samples.each do |sample, parts|
59
- UKPostcode.new(sample).sector.must_equal parts[2]
60
- end
61
- end
62
-
63
- it "should extract unit" do
64
- samples.each do |sample, parts|
65
- UKPostcode.new(sample).unit.must_equal parts[3]
66
- end
67
- end
68
-
69
- it "should be the same when normalised" do
70
- samples.each do |sample, parts|
71
- expected = [parts[0], parts[1], " ", parts[2], parts[3]].join
72
- UKPostcode.new(sample).norm.must_equal expected
73
- end
74
- end
75
- end
76
- end
77
-
78
- describe "outcode samples" do
79
- let(:samples) {
80
- VALID_SAMPLES.map{ |a,b,c,d| [a, b].join }
81
- }
82
-
83
- it "should all be valid" do
84
- samples.each do |sample|
85
- UKPostcode.new(sample).must_be :valid?
86
- end
87
- end
88
-
89
- it "should not be full" do
90
- samples.each do |sample|
91
- UKPostcode.new(sample).wont_be :full?
92
- end
93
- end
94
-
95
- it "should keep outcode unchanged" do
96
- samples.each do |sample|
97
- UKPostcode.new(sample).outcode.must_equal sample
98
- end
5
+ describe "parse" do
6
+ it "should return a Giro postcode" do
7
+ pc = UKPostcode.parse("GIR 0AA")
8
+ pc.must_be_instance_of UKPostcode::GiroPostcode
9
+ pc.to_s.must_equal "GIR 0AA"
99
10
  end
100
11
 
101
- it "should have nil incode" do
102
- samples.each do |sample|
103
- UKPostcode.new(sample).incode.must_be_nil
104
- end
105
- end
106
-
107
- it "should be the same when normalised" do
108
- samples.each do |sample|
109
- UKPostcode.new(sample).norm.must_equal sample
110
- end
111
- end
112
- end
113
-
114
- describe "when the postcode is supplied in lower case" do
115
- let(:postcode) {
116
- UKPostcode.new("w1a 1aa")
117
- }
118
-
119
- it "should extract outcode in upper case" do
120
- postcode.outcode.must_equal "W1A"
121
- end
122
-
123
- it "should extract incode in upper case" do
124
- postcode.incode.must_equal "1AA"
125
- end
126
-
127
- it "should be valid" do
128
- postcode.must_be :valid?
129
- end
130
- end
131
-
132
- { "when the postcode is blank" => "",
133
- "when the incode is truncated" => "W1A 1A",
134
- "when the outcode is truncated" => "W",
135
- "when the postcode is invalid" => "ABC DEFG"
136
- }.each do |desc, sample|
137
- describe desc do
138
- let(:postcode) {
139
- UKPostcode.new(sample)
140
- }
141
-
142
- it "should not be valid" do
143
- refute postcode.valid?
144
- end
145
-
146
- it "should not be full" do
147
- refute postcode.full?
148
- end
149
-
150
- it "should return an empty string when normalised" do
151
- postcode.norm.must_equal ""
152
- end
153
-
154
- it "should return nil for outcode" do
155
- postcode.outcode.must_be_nil
156
- end
157
-
158
- it "should return nil for incode" do
159
- postcode.incode.must_be_nil
160
- end
12
+ it "should return a geographic postcode" do
13
+ pc = UKPostcode.parse("EH8 8DX")
14
+ pc.must_be_instance_of UKPostcode::GeographicPostcode
15
+ pc.to_s.must_equal "EH8 8DX"
161
16
  end
162
- end
163
-
164
- describe "when used as a string" do
165
- it "should normalise spacing when no spacing has been used in the input" do
166
- UKPostcode.new("W1A1AA").norm.must_equal "W1A 1AA"
167
- end
168
-
169
- it "should normalise spacing when too much spacing has been used in the input" do
170
- UKPostcode.new("W1A 1AA").norm.must_equal "W1A 1AA"
171
- end
172
-
173
- it "should convert case" do
174
- UKPostcode.new("w1a 1aa").norm.must_equal "W1A 1AA"
175
- end
176
-
177
- it "should ignore a missing incode" do
178
- UKPostcode.new("W1A").norm.must_equal "W1A"
179
- end
180
-
181
- it "should trim whitespace from start and end of the string" do
182
- UKPostcode.new(" W1A 1AA ").norm.must_equal "W1A 1AA"
183
- end
184
- end
185
-
186
- it "should return original input for to_s" do
187
- ["W1A1AA", "w1a 1aa", "W1A"].each do |s|
188
- UKPostcode.new(s).to_s.must_equal s
189
- end
190
- end
191
-
192
- it "should return original input for to_str" do
193
- ["W1A1AA", "w1a 1aa", "W1A"].each do |s|
194
- UKPostcode.new(s).to_str.must_equal s
195
- end
196
- end
197
-
198
- describe "when letters are used instead of digits" do
199
- describe "in a full postcode" do
200
- let(:postcode) {
201
- UKPostcode.new("SWIA OPW")
202
- }
203
-
204
- it "should be valid" do
205
- postcode.must_be :valid?
206
- end
207
-
208
- it "should be full" do
209
- postcode.must_be :full?
210
- end
211
-
212
- it "should normalise to digits" do
213
- postcode.norm.must_equal "SW1A 0PW"
214
- end
215
- end
216
- end
217
-
218
- describe "when digits are used instead of letters" do
219
- describe "in a full postcode" do
220
- let(:postcode) {
221
- UKPostcode.new("0X1 0AB")
222
- }
223
-
224
- it "should be valid" do
225
- postcode.must_be :valid?
226
- end
227
-
228
- it "should be full" do
229
- postcode.must_be :full?
230
- end
231
-
232
- it "should normalise to letters" do
233
- postcode.norm.must_equal "OX1 0AB"
234
- end
235
- end
236
- end
237
-
238
- describe "single-letter area code" do
239
- %w[B E G L M N S W].each do |area|
240
- describe area do
241
- it "should not convert 1 to I in two-digit outcode" do
242
- outcode = area + "11"
243
- postcode = UKPostcode.new(outcode)
244
- postcode.area.must_equal area
245
- postcode.outcode.must_equal outcode
246
- end
247
-
248
- it "should not convert 1 to I in full postcode with space" do
249
- outcode = area + "13"
250
- postcode = UKPostcode.new(outcode + " 1AA")
251
- postcode.area.must_equal area
252
- postcode.outcode.must_equal outcode
253
- end
254
-
255
- it "should not convert 1 to I in full postcode without space" do
256
- outcode = area + "13"
257
- postcode = UKPostcode.new(outcode + "2AA")
258
- postcode.area.must_equal area
259
- postcode.outcode.must_equal outcode
260
- end
261
- end
262
- end
263
- end
264
-
265
- describe "GIR 0AA" do
266
- let(:norm) {
267
- "GIR 0AA"
268
- }
269
17
 
270
- it "should stay as is if already normalised" do
271
- UKPostcode.new(norm).norm.must_equal norm
18
+ it "should return invalid instance for a blank postcode" do
19
+ pc = UKPostcode.parse("")
20
+ pc.must_be_instance_of UKPostcode::InvalidPostcode
21
+ pc.to_s.must_equal ""
272
22
  end
273
23
 
274
- it "should be normalised" do
275
- UKPostcode.new("G1ROAA").norm.must_equal norm
24
+ it "should return invalid instance for an invalid postcode" do
25
+ pc = UKPostcode.parse("ABC DEF")
26
+ pc.must_be_instance_of UKPostcode::InvalidPostcode
27
+ pc.to_s.must_equal "ABC DEF"
276
28
  end
277
29
  end
278
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uk_postcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0.alpha
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Battley
@@ -14,29 +14,40 @@ dependencies:
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description:
27
+ description: Parse, validate, and extract sub-fields from UK postcodes
28
28
  email: pbattley@gmail.com
29
29
  executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
+ - COPYING.txt
33
34
  - README.md
34
- - Rakefile
35
35
  - lib/uk_postcode.rb
36
+ - lib/uk_postcode/country_finder.rb
37
+ - lib/uk_postcode/country_lookup.rb
38
+ - lib/uk_postcode/geographic_postcode.rb
39
+ - lib/uk_postcode/giro_postcode.rb
40
+ - lib/uk_postcode/invalid_postcode.rb
41
+ - lib/uk_postcode/tree.rb
36
42
  - lib/uk_postcode/version.rb
37
- - test/samples/wikipedia.list
38
- - test/samples_test.rb
43
+ - test/country_finder_test.rb
44
+ - test/full_postcode_test.rb
45
+ - test/geographic_postcode_test.rb
46
+ - test/giro_postcode_test.rb
47
+ - test/invalid_postcode_test.rb
48
+ - test/special_postcode_test.rb
39
49
  - test/test_helper.rb
50
+ - test/tree_test.rb
40
51
  - test/uk_postcode_test.rb
41
52
  homepage: http://github.com/threedaymonk/uk_postcode
42
53
  licenses:
@@ -53,9 +64,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
64
  version: '0'
54
65
  required_rubygems_version: !ruby/object:Gem::Requirement
55
66
  requirements:
56
- - - ">="
67
+ - - ">"
57
68
  - !ruby/object:Gem::Version
58
- version: '0'
69
+ version: 1.3.1
59
70
  requirements: []
60
71
  rubyforge_project:
61
72
  rubygems_version: 2.2.2
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require "rake/testtask"
2
-
3
- Rake::TestTask.new("test") do |t|
4
- t.pattern = "test/**/*_test.rb"
5
- t.verbose = true
6
- end
7
-
8
- task :default => :test
@@ -1,35 +0,0 @@
1
- # Special postcodes listed in http://en.wikipedia.org/wiki/UK_postcode
2
-
3
- SW1A0AA
4
- SW1A1AA
5
- SW1A2AA
6
- BS981TL
7
- BX1 1LT
8
- BX5 5AT
9
- CF991NA
10
- DH991NS
11
- E16 1XL
12
- E98 1NW
13
- E98 1SN
14
- E98 1ST
15
- E98 1TT
16
- EC4Y0HQ
17
- EH991SP
18
- EN8 9SL
19
- G58 1SB
20
- GIR 0AA
21
- L30 4GB
22
- LS981FD
23
- M2 5BE
24
- N81 1ER
25
- S2 4SU
26
- S6 1SW
27
- SE1 8UJ
28
- SE9 2UG
29
- SN381NW
30
- SW1A0PW
31
- SW1A2HQ
32
- SW1W0DT
33
- TS1 3BA
34
- W1A 1AA
35
- W1F 9DJ
data/test/samples_test.rb DELETED
@@ -1,18 +0,0 @@
1
- require_relative "./test_helper"
2
- require "uk_postcode"
3
-
4
- describe "Sample files" do
5
- Dir[File.expand_path("../samples/**/*.list", __FILE__)].each do |path|
6
- it "should be valid for each line in #{File.basename(path, ".list")}" do
7
- open(path).each_line do |line|
8
- next if line =~ /^#|^$/
9
- outcode = line[0,4].strip
10
- incode = line[4,3].strip
11
- postcode = UKPostcode.new(outcode + incode)
12
- postcode.must_be :valid?
13
- postcode.outcode.must_equal outcode
14
- postcode.incode.must_equal incode
15
- end
16
- end
17
- end
18
- end