Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Move tests to RSpec
Mini[Tt]est is frustratingly inconsistent between versions of Ruby.
- Loading branch information
1 parent
e193b1f
commit cddd264
Showing
19 changed files
with
599 additions
and
616 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
*.gem | ||
Gemfile.lock | ||
/data/ | ||
/test/data/ | ||
/spec/data/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--color | ||
--require spec_helper | ||
-I lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
require "rake/testtask" | ||
require "rspec/core/rake_task" | ||
|
||
Rake::TestTask.new("test") do |t| | ||
t.pattern = "test/**/*_test.rb" | ||
t.verbose = true | ||
desc "Run the specs." | ||
RSpec::Core::RakeTask.new do |t| | ||
t.pattern = "spec/**/*_spec.rb" | ||
t.verbose = false | ||
end | ||
|
||
task default: :test | ||
task :default => [:spec] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require "uk_postcode" | ||
|
||
describe UKPostcode do | ||
it "finds the country of a full postcode in England" do | ||
expect(described_class.parse("W1A 1AA").country).to eq(:england) | ||
end | ||
|
||
it "finds the country of a full postcode in Scotland" do | ||
expect(described_class.parse("EH8 8DX").country).to eq(:scotland) | ||
end | ||
|
||
it "finds the country of a full postcode in Wales" do | ||
expect(described_class.parse("CF99 1NA").country).to eq(:wales) | ||
end | ||
|
||
it "finds the country of a full postcode in Northern Ireland" do | ||
expect(described_class.parse("BT4 3XX").country).to eq(:northern_ireland) | ||
end | ||
|
||
it "finds the country of a postcode in a border region" do | ||
expect(described_class.parse("CA6 5HS").country).to eq(:scotland) | ||
expect(described_class.parse("CA6 5HT").country).to eq(:england) | ||
end | ||
|
||
it "finds the country of an unambiguous partial postcode" do | ||
expect(described_class.parse("BT4").country).to eq(:northern_ireland) | ||
end | ||
|
||
it "returns :unknown for an ambiguous partial postcode" do | ||
expect(described_class.parse("DG16").country).to eq(:unknown) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
require "uk_postcode/geographic_postcode" | ||
|
||
describe UKPostcode::GeographicPostcode do | ||
describe "parse" do | ||
it "parses a full postcode" do | ||
pc = described_class.parse("W1A 1AA") | ||
expect(pc).to be_instance_of(described_class) | ||
expect(pc.area).to eq("W") | ||
expect(pc.district).to eq("1A") | ||
expect(pc.sector).to eq("1") | ||
expect(pc.unit).to eq("AA") | ||
end | ||
|
||
it "parses a postcode with no unit" do | ||
pc = described_class.parse("W1A 1") | ||
expect(pc).to be_instance_of(described_class) | ||
expect(pc.area).to eq("W") | ||
expect(pc.district).to eq("1A") | ||
expect(pc.sector).to eq("1") | ||
expect(pc.unit).to be_nil | ||
end | ||
|
||
it "parses an outcode" do | ||
pc = described_class.parse("W1A") | ||
expect(pc).to be_instance_of(described_class) | ||
expect(pc.area).to eq("W") | ||
expect(pc.district).to eq("1A") | ||
expect(pc.sector).to be_nil | ||
expect(pc.unit).to be_nil | ||
end | ||
|
||
it "parses an area" do | ||
pc = described_class.parse("W") | ||
expect(pc).to be_instance_of(described_class) | ||
expect(pc.area).to eq("W") | ||
expect(pc.district).to be_nil | ||
expect(pc.sector).to be_nil | ||
expect(pc.unit).to be_nil | ||
end | ||
|
||
it "handles extra spaces" do | ||
pc = described_class.parse(" W1A 1AA ") | ||
expect(pc).to be_instance_of(described_class) | ||
expect(pc.to_s).to eq("W1A 1AA") | ||
end | ||
|
||
it "handles no spaces" do | ||
pc = described_class.parse("W1A1AA") | ||
expect(pc).to be_instance_of(described_class) | ||
expect(pc.to_s).to eq("W1A 1AA") | ||
end | ||
|
||
it "is case-insensitive" do | ||
pc = described_class.parse("w1a 1aa") | ||
expect(pc).to be_instance_of(described_class) | ||
expect(pc.to_s).to eq("W1A 1AA") | ||
end | ||
|
||
it "returns nil if unable to parse" do | ||
pc = described_class.parse("Can't parse this") | ||
expect(pc).to be_nil | ||
end | ||
|
||
describe "single-letter area" do | ||
it "extracts area without trailing I from outcode" do | ||
pc = described_class.parse("B11") | ||
expect(pc.area).to eq("B") | ||
expect(pc.district).to eq("11") | ||
end | ||
|
||
it "extracts area without trailing I from full postcode with space" do | ||
pc = described_class.parse("E17 1AA") | ||
expect(pc.area).to eq("E") | ||
expect(pc.district).to eq("17") | ||
end | ||
|
||
it "extracts area without trailing I from full postcode without space" do | ||
pc = described_class.parse("E171AA") | ||
expect(pc.area).to eq("E") | ||
expect(pc.district).to eq("17") | ||
end | ||
end | ||
|
||
describe "trailing O in area" do | ||
it "extracts area with trailing O from outcode" do | ||
pc = described_class.parse("CO1") | ||
expect(pc.area).to eq("CO") | ||
expect(pc.district).to eq("1") | ||
end | ||
|
||
it "extracts area with trailing O from full postcode with space" do | ||
pc = described_class.parse("CO1 1BT") | ||
expect(pc.area).to eq("CO") | ||
expect(pc.district).to eq("1") | ||
end | ||
|
||
it "extracts area with trailing O from full postcode without space" do | ||
pc = described_class.parse("CO11BT") | ||
expect(pc.area).to eq("CO") | ||
expect(pc.district).to eq("1") | ||
end | ||
end | ||
|
||
describe "tricky postcodes" do | ||
it "parses B11 1LL" do | ||
pc = described_class.parse("B111LL") | ||
expect(pc.area).to eq("B") | ||
expect(pc.district).to eq("11") | ||
expect(pc.sector).to eq("1") | ||
expect(pc.unit).to eq("LL") | ||
end | ||
|
||
it "parses BII ILL" do | ||
pc = described_class.parse("BIIILL") | ||
expect(pc.area).to eq("B") | ||
expect(pc.district).to eq("11") | ||
expect(pc.sector).to eq("1") | ||
expect(pc.unit).to eq("LL") | ||
end | ||
|
||
it "parses BB11 1DJ" do | ||
pc = described_class.parse("BB111DJ") | ||
expect(pc.area).to eq("BB") | ||
expect(pc.district).to eq("11") | ||
expect(pc.sector).to eq("1") | ||
expect(pc.unit).to eq("DJ") | ||
end | ||
|
||
it "parses BBII IDJ" do | ||
pc = described_class.parse("BBIIIDJ") | ||
expect(pc.area).to eq("BB") | ||
expect(pc.district).to eq("11") | ||
expect(pc.sector).to eq("1") | ||
expect(pc.unit).to eq("DJ") | ||
end | ||
|
||
it "parses B10 0JP" do | ||
pc = described_class.parse("B100JP") | ||
expect(pc.area).to eq("B") | ||
expect(pc.district).to eq("10") | ||
expect(pc.sector).to eq("0") | ||
expect(pc.unit).to eq("JP") | ||
end | ||
|
||
it "parses BIO OJP" do | ||
pc = described_class.parse("BIOOJP") | ||
expect(pc.area).to eq("B") | ||
expect(pc.district).to eq("10") | ||
expect(pc.sector).to eq("0") | ||
expect(pc.unit).to eq("JP") | ||
end | ||
end | ||
end | ||
|
||
describe "area" do | ||
it "is capitalised" do | ||
expect(described_class.new("w", "1a", "1", "aa").area).to eq("W") | ||
end | ||
|
||
it "corrects 0 to O" do | ||
expect(described_class.new("0X", "1", "0", "AB").area).to eq("OX") | ||
end | ||
|
||
it "corrects 1 to I" do | ||
expect(described_class.new("1G", "1", "1", "AA").area).to eq("IG") | ||
end | ||
end | ||
|
||
describe "district" do | ||
it "is capitalised" do | ||
expect(described_class.new("w", "1a", "1", "aa").district).to eq("1A") | ||
end | ||
|
||
it "corrects O to 0" do | ||
expect(described_class.new("B", "2O", "2", "XB").district).to eq("20") | ||
end | ||
|
||
it "corrects I to 1" do | ||
expect(described_class.new("B", "I", "I", "DF").district).to eq("1") | ||
end | ||
end | ||
|
||
describe "sector" do | ||
it "corrects O to 0" do | ||
expect(described_class.new("AB", "1", "O", "DN").sector).to eq("0") | ||
end | ||
|
||
it "corrects I to 1" do | ||
expect(described_class.new("W", "1A", "I", "AA").sector).to eq("1") | ||
end | ||
end | ||
|
||
describe "unit" do | ||
it "is capitalised" do | ||
expect(described_class.new("w", "1a", "1", "aa").unit).to eq("AA") | ||
end | ||
|
||
# Note: neither I nor O appear in units | ||
end | ||
|
||
describe "outcode" do | ||
it "is generated from area and district" do | ||
expect(described_class.new("W", "1A").outcode).to eq("W1A") | ||
end | ||
|
||
it "is nil if missing district" do | ||
expect(described_class.new("W").outcode).to be_nil | ||
end | ||
end | ||
|
||
describe "incode" do | ||
it "is generated from sector and unit" do | ||
expect(described_class.new("W", "1A", "1", "AA").incode).to eq("1AA") | ||
end | ||
|
||
it "is nil if missing sector" do | ||
expect(described_class.new("W", "1A").incode).to be_nil | ||
end | ||
|
||
it "is nil if missing unit" do | ||
expect(described_class.new("W", "1A", "1").incode).to be_nil | ||
end | ||
end | ||
|
||
describe "to_s" do | ||
it "generates a full postcode" do | ||
expect(described_class.new("W", "1A", "1", "AA").to_s).to eq("W1A 1AA") | ||
end | ||
|
||
it "generates an outcode" do | ||
expect(described_class.new("W", "1A").to_s).to eq("W1A") | ||
end | ||
|
||
it "generates a postcode with no unit" do | ||
expect(described_class.new("W", "1A", "1").to_s).to eq("W1A 1") | ||
end | ||
|
||
it "generates an area alone" do | ||
expect(described_class.new("W").to_s).to eq("W") | ||
end | ||
end | ||
|
||
describe "full?" do | ||
it "is true if outcode and incode are given" do | ||
expect(described_class.new("W", "1A", "1", "AA")).to be_full | ||
end | ||
|
||
it "is not true if something is missing" do | ||
expect(described_class.new("W", "1A", "1")).not_to be_full | ||
end | ||
end | ||
|
||
describe "valid?" do | ||
it "is true" do | ||
expect(described_class.new("W", "1A", "1", "AA")).to be_valid | ||
end | ||
end | ||
|
||
describe "country" do | ||
it "looks up the country of a full postcode" do | ||
expect(described_class.new("EH", "8", "8", "DX").country).to eq(:scotland) | ||
end | ||
|
||
it "looks up the country of a partial postcode" do | ||
expect(described_class.new("W", "1A").country).to eq(:england) | ||
end | ||
end | ||
end |
Oops, something went wrong.