uk_postcode 2.0.0.beta → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8fa8f457586bb74272addb1cc2680a90718688c4
4
- data.tar.gz: 232794c36025c24b7d9c7051c7bb37d20daa3ba2
3
+ metadata.gz: 0a919ea202a84289397d9ee28a3cfeb13296c96f
4
+ data.tar.gz: 61ae6cb187e332087b477335996e91a5f57a6bc5
5
5
  SHA512:
6
- metadata.gz: 5512f266cbb5ecd1de324af09bb07b0eb0600214a75fbc04c4467eb3887a5fc9363479dde70722161e4f6c9667940bf2baf357ef6001f46e2f97e783e83460ab
7
- data.tar.gz: 5631dca91102f422254fad3672836cb92ef54834284b32346eecaf90d90f38fb88ab807b9e84e7d2125d0a58aaf037a8b21b2639a766ae3f643b84207eee179e
6
+ metadata.gz: aa9f41d0d1b1b9b2793cb6d5075e533f0f7364707b275ec34550ed3f401073de66158f08715ed55a5ac96bde1b12ddd0c9c7e5dd29a4054e0bec885e49f33c6f
7
+ data.tar.gz: 96ba39a7b24beab30c4a80cbd37ff50f95a6ed2ab6bad84049fc864c8279608140accb6b579cca1084f8e1ab13414e463a7a00389449cc692716449d7244b4ef
data/README.md CHANGED
@@ -100,6 +100,38 @@ pc.to_s # => "Not valid"
100
100
  pc.country # => :unknown
101
101
  ```
102
102
 
103
+ ## Tips for Rails
104
+
105
+ You can normalise postcodes on assignment by overriding a setter method (this
106
+ assumes that you have a `postcode` field on the model):
107
+
108
+ ```ruby
109
+ def postcode=(str)
110
+ super UKPostcode.parse(str).to_s
111
+ end
112
+ ```
113
+
114
+ Invalid postcodes are parsed to instances of `InvalidPostcode`, whose `#to_s`
115
+ method gives the original input, so an invalid postcode will be presented back
116
+ to the user as originally entered.
117
+
118
+ To validate, use something like this:
119
+
120
+ ```ruby
121
+ class PostcodeValidator < ActiveModel::EachValidator
122
+ def validate_each(record, attribute, value)
123
+ ukpc = UKPostcode.parse(value)
124
+ unless ukpc.valid? && ukpc.full?
125
+ record.errors[attribute] << "not recognised as a UK postcode"
126
+ end
127
+ end
128
+ end
129
+
130
+ class Address
131
+ validates :postcode, presence: true, postcode: true
132
+ end
133
+ ```
134
+
103
135
  ## For users of version 1.x
104
136
 
105
137
  The interface has changed significantly, so code that worked with version 1.x
@@ -22,11 +22,11 @@ module UKPostcode
22
22
  end
23
23
 
24
24
  def full?
25
- false
25
+ raise NotImplemented
26
26
  end
27
27
 
28
28
  def valid?
29
- false
29
+ raise NotImplemented
30
30
  end
31
31
 
32
32
  def country
@@ -2,8 +2,10 @@ require "uk_postcode/abstract_postcode"
2
2
 
3
3
  module UKPostcode
4
4
 
5
- # InvalidPostcode is a singleton null object returned by UKPostcode.parse
6
- # when it is unable to parse the supplied postcode.
5
+ # An InvalidPostcode is returned by UKPostcode.parse when it is unable to
6
+ # parse the supplied postcode. As it returns the input verbatim via #to_s,
7
+ # it's possible to do UKPostcode.parse(s).to_s and get either a normalised
8
+ # postcode (if possible) or the original user input.
7
9
  #
8
10
  # The sub-fields of the postcode (outcode, area, etc.) are all nil.
9
11
  #
@@ -22,5 +24,14 @@ module UKPostcode
22
24
  def to_s
23
25
  @input
24
26
  end
27
+
28
+ def full?
29
+ false
30
+ end
31
+
32
+ def valid?
33
+ false
34
+ end
35
+
25
36
  end
26
37
  end
@@ -1,3 +1,3 @@
1
1
  module UKPostcode
2
- VERSION = "2.0.0.beta"
2
+ VERSION = "2.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uk_postcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Battley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-30 00:00:00.000000000 Z
11
+ date: 2015-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3'
27
41
  description: Parse, validate, and extract sub-fields from UK postcodes
28
42
  email: pbattley@gmail.com
29
43
  executables: []
@@ -42,15 +56,6 @@ files:
42
56
  - lib/uk_postcode/parser_chain.rb
43
57
  - lib/uk_postcode/tree.rb
44
58
  - lib/uk_postcode/version.rb
45
- - test/country_finder_test.rb
46
- - test/full_postcode_test.rb
47
- - test/geographic_postcode_test.rb
48
- - test/giro_postcode_test.rb
49
- - test/invalid_postcode_test.rb
50
- - test/special_postcode_test.rb
51
- - test/test_helper.rb
52
- - test/tree_test.rb
53
- - test/uk_postcode_test.rb
54
59
  homepage: http://github.com/threedaymonk/uk_postcode
55
60
  licenses:
56
61
  - MIT
@@ -63,12 +68,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
63
68
  requirements:
64
69
  - - ">="
65
70
  - !ruby/object:Gem::Version
66
- version: '0'
71
+ version: 1.9.2
67
72
  required_rubygems_version: !ruby/object:Gem::Requirement
68
73
  requirements:
69
- - - ">"
74
+ - - ">="
70
75
  - !ruby/object:Gem::Version
71
- version: 1.3.1
76
+ version: '0'
72
77
  requirements: []
73
78
  rubyforge_project:
74
79
  rubygems_version: 2.2.2
@@ -1,33 +0,0 @@
1
- require_relative "./test_helper"
2
- require "uk_postcode"
3
-
4
- describe UKPostcode do
5
- it 'should find the country of a full postcode in England' do
6
- UKPostcode.parse('W1A 1AA').country.must_equal :england
7
- end
8
-
9
- it 'should find the country of a full postcode in Scotland' do
10
- UKPostcode.parse('EH8 8DX').country.must_equal :scotland
11
- end
12
-
13
- it 'should find the country of a full postcode in Wales' do
14
- UKPostcode.parse('CF99 1NA').country.must_equal :wales
15
- end
16
-
17
- it 'should find the country of a full postcode in Northern Ireland' do
18
- UKPostcode.parse('BT4 3XX').country.must_equal :northern_ireland
19
- end
20
-
21
- it 'should find the country of a postcode in a border region' do
22
- UKPostcode.parse('CA6 5HS').country.must_equal :scotland
23
- UKPostcode.parse('CA6 5HT').country.must_equal :england
24
- end
25
-
26
- it 'should find the country of an unambiguous partial postcode' do
27
- UKPostcode.parse('BT4').country.must_equal :northern_ireland
28
- end
29
-
30
- it 'should return :unknown for an ambiguous partial postcode' do
31
- UKPostcode.parse('DG16').country.must_equal :unknown
32
- end
33
- end
@@ -1,34 +0,0 @@
1
- require_relative "./test_helper"
2
- require "csv"
3
- require "uk_postcode"
4
-
5
- describe "Full set of postcodes" do
6
- CSV_PATH = File.expand_path("../data/postcodes.csv", __FILE__)
7
-
8
- COUNTRIES = {
9
- 'E92000001' => :england,
10
- 'N92000002' => :northern_ireland,
11
- 'S92000003' => :scotland,
12
- 'W92000004' => :wales,
13
- 'L93000001' => :channel_islands,
14
- 'M83000003' => :isle_of_man
15
- }
16
-
17
- it "should correctly parse and find the country of every extant postcode" do
18
- skip "Skipping because SKIP_FULL_TEST was set" if ENV['SKIP_FULL_TEST']
19
- skip "Skipping because #{CSV_PATH} does not exist" unless File.exist?(CSV_PATH)
20
-
21
- CSV.foreach(CSV_PATH, headers: [:postcode, :country]) do |row|
22
- outcode = row[:postcode][0, 4].strip
23
- incode = row[:postcode][4, 3].strip
24
- country = COUNTRIES.fetch(row[:country])
25
-
26
- postcode = UKPostcode.parse(outcode + incode)
27
-
28
- postcode.wont_be_nil
29
- postcode.outcode.must_equal outcode
30
- postcode.incode.must_equal incode
31
- postcode.country.must_equal country
32
- end
33
- end
34
- end
@@ -1,271 +0,0 @@
1
- require_relative "./test_helper"
2
- require "uk_postcode/geographic_postcode"
3
-
4
- describe UKPostcode::GeographicPostcode do
5
- described_class = UKPostcode::GeographicPostcode
6
-
7
- describe "parse" do
8
- it "should parse a full postcode" do
9
- pc = described_class.parse("W1A 1AA")
10
- pc.must_be_instance_of described_class
11
- pc.area.must_equal "W"
12
- pc.district.must_equal "1A"
13
- pc.sector.must_equal "1"
14
- pc.unit.must_equal "AA"
15
- end
16
-
17
- it "should parse a postcode with no unit" do
18
- pc = described_class.parse("W1A 1")
19
- pc.must_be_instance_of described_class
20
- pc.area.must_equal "W"
21
- pc.district.must_equal "1A"
22
- pc.sector.must_equal "1"
23
- pc.unit.must_be_nil
24
- end
25
-
26
- it "should parse an outcode" do
27
- pc = described_class.parse("W1A")
28
- pc.must_be_instance_of described_class
29
- pc.area.must_equal "W"
30
- pc.district.must_equal "1A"
31
- pc.sector.must_be_nil
32
- pc.unit.must_be_nil
33
- end
34
-
35
- it "should parse an area" do
36
- pc = described_class.parse("W")
37
- pc.must_be_instance_of described_class
38
- pc.area.must_equal "W"
39
- pc.district.must_be_nil
40
- pc.sector.must_be_nil
41
- pc.unit.must_be_nil
42
- end
43
-
44
- it "should handle extra spaces" do
45
- pc = described_class.parse(" W1A 1AA ")
46
- pc.must_be_instance_of described_class
47
- pc.to_s.must_equal "W1A 1AA"
48
- end
49
-
50
- it "should handle no spaces" do
51
- pc = described_class.parse("W1A1AA")
52
- pc.must_be_instance_of described_class
53
- pc.to_s.must_equal "W1A 1AA"
54
- end
55
-
56
- it "should be case-insensitive" do
57
- pc = described_class.parse("w1a 1aa")
58
- pc.must_be_instance_of described_class
59
- pc.to_s.must_equal "W1A 1AA"
60
- end
61
-
62
- it "should return nil if unable to parse" do
63
- pc = described_class.parse("Can't parse this")
64
- pc.must_be_nil
65
- end
66
-
67
- describe "single-letter area" do
68
- it "should extract area without trailing I from outcode" do
69
- pc = described_class.parse("B11")
70
- pc.area.must_equal "B"
71
- pc.district.must_equal "11"
72
- end
73
-
74
- it "should extract area without trailing I from full postcode with space" do
75
- pc = described_class.parse("E17 1AA")
76
- pc.area.must_equal "E"
77
- pc.district.must_equal "17"
78
- end
79
-
80
- it "should extract area without trailing I from full postcode without space" do
81
- pc = described_class.parse("E171AA")
82
- pc.area.must_equal "E"
83
- pc.district.must_equal "17"
84
- end
85
- end
86
-
87
- describe "trailing O in area" do
88
- it "should extract area with trailing O from outcode" do
89
- pc = described_class.parse("CO1")
90
- pc.area.must_equal "CO"
91
- pc.district.must_equal "1"
92
- end
93
-
94
- it "should extract area with trailing O from full postcode with space" do
95
- pc = described_class.parse("CO1 1BT")
96
- pc.area.must_equal "CO"
97
- pc.district.must_equal "1"
98
- end
99
-
100
- it "should extract area with trailing O from full postcode without space" do
101
- pc = described_class.parse("CO11BT")
102
- pc.area.must_equal "CO"
103
- pc.district.must_equal "1"
104
- end
105
- end
106
-
107
- describe "tricky postcodes" do
108
- it "should parse B11 1LL" do
109
- pc = described_class.parse("B111LL")
110
- pc.area.must_equal "B"
111
- pc.district.must_equal "11"
112
- pc.sector.must_equal "1"
113
- pc.unit.must_equal "LL"
114
- end
115
-
116
- it "should parse BII ILL" do
117
- pc = described_class.parse("BIIILL")
118
- pc.area.must_equal "B"
119
- pc.district.must_equal "11"
120
- pc.sector.must_equal "1"
121
- pc.unit.must_equal "LL"
122
- end
123
-
124
- it "should parse BB11 1DJ" do
125
- pc = described_class.parse("BB111DJ")
126
- pc.area.must_equal "BB"
127
- pc.district.must_equal "11"
128
- pc.sector.must_equal "1"
129
- pc.unit.must_equal "DJ"
130
- end
131
-
132
- it "should parse BBII IDJ" do
133
- pc = described_class.parse("BBIIIDJ")
134
- pc.area.must_equal "BB"
135
- pc.district.must_equal "11"
136
- pc.sector.must_equal "1"
137
- pc.unit.must_equal "DJ"
138
- end
139
-
140
- it "should parse B10 0JP" do
141
- pc = described_class.parse("B100JP")
142
- pc.area.must_equal "B"
143
- pc.district.must_equal "10"
144
- pc.sector.must_equal "0"
145
- pc.unit.must_equal "JP"
146
- end
147
-
148
- it "should parse BIO OJP" do
149
- pc = described_class.parse("BIOOJP")
150
- pc.area.must_equal "B"
151
- pc.district.must_equal "10"
152
- pc.sector.must_equal "0"
153
- pc.unit.must_equal "JP"
154
- end
155
- end
156
- end
157
-
158
- describe "area" do
159
- it "should be capitalised" do
160
- described_class.new("w", "1a", "1", "aa").area.must_equal "W"
161
- end
162
-
163
- it "should correct 0 to O" do
164
- described_class.new("0X", "1", "0", "AB").area.must_equal "OX"
165
- end
166
-
167
- it "should correct 1 to I" do
168
- described_class.new("1G", "1", "1", "AA").area.must_equal "IG"
169
- end
170
- end
171
-
172
- describe "district" do
173
- it "should be capitalised" do
174
- described_class.new("w", "1a", "1", "aa").district.must_equal "1A"
175
- end
176
-
177
- it "should correct O to 0" do
178
- described_class.new("B", "2O", "2", "XB").district.must_equal "20"
179
- end
180
-
181
- it "should correct I to 1" do
182
- described_class.new("B", "I", "I", "DF").district.must_equal "1"
183
- end
184
- end
185
-
186
- describe "sector" do
187
- it "should correct O to 0" do
188
- described_class.new("AB", "1", "O", "DN").sector.must_equal "0"
189
- end
190
-
191
- it "should correct I to 1" do
192
- described_class.new("W", "1A", "I", "AA").sector.must_equal "1"
193
- end
194
- end
195
-
196
- describe "unit" do
197
- it "should be capitalised" do
198
- described_class.new("w", "1a", "1", "aa").unit.must_equal "AA"
199
- end
200
-
201
- # Note: neither I nor O appear in units
202
- end
203
-
204
- describe "outcode" do
205
- it "should be generated from area and district" do
206
- described_class.new("W", "1A").outcode.must_equal "W1A"
207
- end
208
-
209
- it "should be nil if missing district" do
210
- described_class.new("W").outcode.must_be_nil
211
- end
212
- end
213
-
214
- describe "incode" do
215
- it "should be generated from sector and unit" do
216
- described_class.new("W", "1A", "1", "AA").incode.must_equal "1AA"
217
- end
218
-
219
- it "should be nil if missing sector" do
220
- described_class.new("W", "1A").incode.must_be_nil
221
- end
222
-
223
- it "should be nil if missing unit" do
224
- described_class.new("W", "1A", "1").incode.must_be_nil
225
- end
226
- end
227
-
228
- describe "to_s" do
229
- it "should generate a full postcode" do
230
- described_class.new("W", "1A", "1", "AA").to_s.must_equal "W1A 1AA"
231
- end
232
-
233
- it "should generate an outcode" do
234
- described_class.new("W", "1A").to_s.must_equal "W1A"
235
- end
236
-
237
- it "should generate a postcode with no unit" do
238
- described_class.new("W", "1A", "1").to_s.must_equal "W1A 1"
239
- end
240
-
241
- it "should generate an area alone" do
242
- described_class.new("W").to_s.must_equal "W"
243
- end
244
- end
245
-
246
- describe "full?" do
247
- it "should be true if outcode and incode are given" do
248
- described_class.new("W", "1A", "1", "AA").must_be :full?
249
- end
250
-
251
- it "should not be true if something is missing" do
252
- described_class.new("W", "1A", "1").wont_be :full?
253
- end
254
- end
255
-
256
- describe "valid?" do
257
- it "should be true" do
258
- described_class.new("W", "1A", "1", "AA").must_be :valid?
259
- end
260
- end
261
-
262
- describe "country" do
263
- it "should look up the country of a full postcode" do
264
- described_class.new("EH", "8", "8", "DX").country.must_equal :scotland
265
- end
266
-
267
- it "should look up the country of a partial postcode" do
268
- described_class.new("W", "1A").country.must_equal :england
269
- end
270
- end
271
- end
@@ -1,100 +0,0 @@
1
- require_relative "./test_helper"
2
- require "uk_postcode/giro_postcode"
3
-
4
- describe UKPostcode::GiroPostcode do
5
- described_class = UKPostcode::GiroPostcode
6
-
7
- let(:subject) { described_class.instance }
8
-
9
- describe "parse" do
10
- it "should parse the canonical form" do
11
- pc = described_class.parse("GIR 0AA")
12
- pc.must_be_instance_of described_class
13
- end
14
-
15
- it "should parse transcribed 0/O and 1/I" do
16
- pc = described_class.parse("G1R OAA")
17
- pc.must_be_instance_of described_class
18
- end
19
-
20
- it "should handle extra spaces" do
21
- pc = described_class.parse(" GIR 0AA ")
22
- pc.must_be_instance_of described_class
23
- end
24
-
25
- it "should handle no spaces" do
26
- pc = described_class.parse("GIR0AA")
27
- pc.must_be_instance_of described_class
28
- end
29
-
30
- it "should be case-insensitive" do
31
- pc = described_class.parse("gir 0aa")
32
- pc.must_be_instance_of described_class
33
- end
34
-
35
- it "should return nil if unable to parse" do
36
- pc = described_class.parse("Can't parse this")
37
- pc.must_be_nil
38
- end
39
- end
40
-
41
- describe "area" do
42
- it "should be nil" do
43
- subject.area.must_be_nil
44
- end
45
- end
46
-
47
- describe "district" do
48
- it "should be nil" do
49
- subject.district.must_be_nil
50
- end
51
- end
52
-
53
- describe "sector" do
54
- it "should be nil" do
55
- subject.sector.must_be_nil
56
- end
57
- end
58
-
59
- describe "unit" do
60
- it "should be nil" do
61
- subject.unit.must_be_nil
62
- end
63
- end
64
-
65
- describe "outcode" do
66
- it "should be GIR" do
67
- subject.outcode.must_equal("GIR")
68
- end
69
- end
70
-
71
- describe "incode" do
72
- it "should be 0AA" do
73
- subject.incode.must_equal("0AA")
74
- end
75
- end
76
-
77
- describe "to_s" do
78
- it "should be the canonical form" do
79
- subject.to_s.must_equal "GIR 0AA"
80
- end
81
- end
82
-
83
- describe "full?" do
84
- it "should be true" do
85
- subject.must_be :full?
86
- end
87
- end
88
-
89
- describe "valid?" do
90
- it "should be true" do
91
- subject.must_be :valid?
92
- end
93
- end
94
-
95
- describe "country" do
96
- it "should be England" do
97
- subject.country.must_equal :england
98
- end
99
- end
100
- end
@@ -1,75 +0,0 @@
1
- require_relative "./test_helper"
2
- require "uk_postcode/invalid_postcode"
3
-
4
- describe UKPostcode::InvalidPostcode do
5
- described_class = UKPostcode::InvalidPostcode
6
-
7
- let(:subject) { described_class.new("anything") }
8
-
9
- describe "parse" do
10
- it "should parse anything" do
11
- pc = described_class.parse("Any old junk")
12
- pc.must_be_instance_of described_class
13
- end
14
- end
15
-
16
- describe "area" do
17
- it "should be nil" do
18
- subject.area.must_be_nil
19
- end
20
- end
21
-
22
- describe "district" do
23
- it "should be nil" do
24
- subject.district.must_be_nil
25
- end
26
- end
27
-
28
- describe "sector" do
29
- it "should be nil" do
30
- subject.sector.must_be_nil
31
- end
32
- end
33
-
34
- describe "unit" do
35
- it "should be nil" do
36
- subject.unit.must_be_nil
37
- end
38
- end
39
-
40
- describe "outcode" do
41
- it "should be nil" do
42
- subject.outcode.must_be_nil
43
- end
44
- end
45
-
46
- describe "incode" do
47
- it "should be nil" do
48
- subject.incode.must_be_nil
49
- end
50
- end
51
-
52
- describe "to_s" do
53
- it "should return the initialisation string" do
54
- subject.to_s.must_equal "anything"
55
- end
56
- end
57
-
58
- describe "full?" do
59
- it "should be false" do
60
- subject.wont_be :full?
61
- end
62
- end
63
-
64
- describe "valid?" do
65
- it "should be false" do
66
- subject.wont_be :valid?
67
- end
68
- end
69
-
70
- describe "country" do
71
- it "should be unknown" do
72
- subject.country.must_equal :unknown
73
- end
74
- end
75
- end
@@ -1,50 +0,0 @@
1
- require_relative "./test_helper"
2
- require "uk_postcode"
3
-
4
- describe "Special postcodes" do
5
- # Special postcodes listed in http://en.wikipedia.org/wiki/UK_postcode
6
- SPECIAL = %w[
7
- SW1A 0AA
8
- SW1A 1AA
9
- SW1A 2AA
10
- BS98 1TL
11
- BX1 1LT
12
- BX5 5AT
13
- CF99 1NA
14
- DH99 1NS
15
- E16 1XL
16
- E98 1NW
17
- E98 1SN
18
- E98 1ST
19
- E98 1TT
20
- EC4Y 0HQ
21
- EH99 1SP
22
- EN8 9SL
23
- G58 1SB
24
- GIR 0AA
25
- L30 4GB
26
- LS98 1FD
27
- M2 5BE
28
- N81 1ER
29
- S2 4SU
30
- S6 1SW
31
- SE1 8UJ
32
- SE9 2UG
33
- SN38 1NW
34
- SW1A 0PW
35
- SW1A 2HQ
36
- SW1W 0DT
37
- TS1 3BA
38
- W1A 1AA
39
- W1F 9DJ
40
- ]
41
-
42
- SPECIAL.each_slice(2) do |outcode, incode|
43
- it "should correctly handle special postcode #{outcode} #{incode}" do
44
- postcode = UKPostcode.parse(outcode + incode)
45
- postcode.wont_be_nil
46
- postcode.outcode.must_equal outcode
47
- postcode.incode.must_equal incode
48
- end
49
- end
50
- end
data/test/test_helper.rb DELETED
@@ -1,4 +0,0 @@
1
- lib = File.expand_path("../../lib", __FILE__)
2
- $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
3
- require "minitest/spec"
4
- require "minitest/autorun"
data/test/tree_test.rb DELETED
@@ -1,84 +0,0 @@
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
@@ -1,30 +0,0 @@
1
- require_relative "./test_helper"
2
- require "uk_postcode"
3
-
4
- describe UKPostcode do
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"
10
- end
11
-
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"
16
- end
17
-
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 ""
22
- end
23
-
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"
28
- end
29
- end
30
- end