Sha256: 4868ed94ed9cf7ea221b5423fa8ec30af1b7fc834f316c8c3eb3b274a6c769e9

Contents?: true

Size: 1.94 KB

Versions: 9

Compression:

Stored size: 1.94 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

require 'spec_helper'

include TwitterCldr::Shared

describe PostalCodes do
  describe "#territories" do
    let(:territories) { PostalCodes.territories }

    it 'returns an array' do
      expect(territories).to be_instance_of(Array)
    end

    it 'returns symbols' do
      territories.each { |territory| expect(territory).to be_instance_of(Symbol) }
    end

    it 'returns supported territories' do
      expect(territories).to include(:br, :fr, :jp)
    end
  end

  describe "#new" do
    it "should raise an error if the territory isn't supported" do
      lambda { PostalCodes.for_territory(:xx) }.should raise_error(InvalidTerritoryError)
    end

    it 'accepts strings' do
      PostalCodes.for_territory("us").should be_a(PostalCodes)
    end

    it 'accepts upper-case strings' do
      PostalCodes.for_territory("US").should be_a(PostalCodes)
    end
  end

  context "with a PostalCodes instance" do
    let(:postal_code) { PostalCodes.for_territory(:us) }

    describe '#regexp' do
      it 'returns postal code regex for a given territory' do
        postal_code.regexp.should be_a(Regexp)
      end
    end

    describe '#valid?' do
      it 'returns true if a given postal code satisfies the regexp' do
        postal_code.valid?('12345-6789').should be_true
      end

      it "returns false if a given postal code doesn't satisfy the regexp" do
        postal_code.valid?('postal-code').should be_false
      end
    end

    describe "#sample" do
      PostalCodes.territories.each do |territory|
        postal_code = PostalCodes.for_territory(territory)

        it "returns samples that match #{territory}" do
          postal_code.sample(10).each do |sample|
            result = postal_code.valid?(sample)
            puts "Failed with example #{sample}" unless result
            result.should be_true
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
twitter_cldr-3.0.10 spec/shared/postal_codes_spec.rb
twitter_cldr-3.0.9 spec/shared/postal_codes_spec.rb
twitter_cldr-3.0.8 spec/shared/postal_codes_spec.rb
twitter_cldr-3.0.7 spec/shared/postal_codes_spec.rb
twitter_cldr-3.0.6 spec/shared/postal_codes_spec.rb
twitter_cldr-3.0.5 spec/shared/postal_codes_spec.rb
twitter_cldr-3.0.4 spec/shared/postal_codes_spec.rb
twitter_cldr-3.0.3 spec/shared/postal_codes_spec.rb
twitter_cldr-3.0.2 spec/shared/postal_codes_spec.rb