Sha256: 686d6c1ff8864da9669de29a76f8be76719f14b71c166bb38c895efd9731028b

Contents?: true

Size: 1.95 KB

Versions: 21

Compression:

Stored size: 1.95 KB

Contents

# encoding: UTF-8

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

require 'spec_helper'

include TwitterCldr::Shared
include TwitterCldr::Utils

describe PostalCodeGenerator do
  let(:generator) do
    PostalCodeGenerator.new(
      RegexpAst::Root.new([
        RegexpAst::Digit.new([], RegexpAst::Quantifier.new(3, 3)),
        RegexpAst::Literal.new([], nil, '- '),
        RegexpAst::Word.new([], RegexpAst::Quantifier.new(3, 3))
      ], nil)
    )
  end

  let(:blank_generator) do
    PostalCodeGenerator.new(
      RegexpAst::Root.new([
        RegexpAst::Alternation.new([
          RegexpAst::Literal.new([], nil, ''),
          RegexpAst::Word.new([], nil)
        ], nil)
      ], nil)
    )
  end

  let(:limited_generator) do
    PostalCodeGenerator.new(
      RegexpAst::Root.new([
        RegexpAst::CharacterSet.new([], nil, ['A-C'], false),
      ], nil)
    )
  end

  describe "#generate" do
    it "should generate correctly and not allow dashes to be followed by spaces" do
      10.times do
        generator.generate.should match(/\d{3}-\w{3}/)
      end
    end
  end

  describe "#sample" do
    it "should generate the given number of unique samples" do
      samples = generator.sample(10)
      samples.size.should == 10
      samples.uniq.should == samples
      samples.each do |sample|
        sample.should match(/\d{3}-\w{3}/)
      end
    end

    it "should not return blank samples" do
      10.times do
        blank_generator.sample(10).all? do |sample|
          sample.empty?
        end.should be_false
      end
    end

    it "shouldn't loop infinitely if the number of possible samples is less than requested" do
      limited_generator.sample(10).size.should < 10
    end

    it "should attempt to generate more samples if the set doesn't contain enough (but shouldn't infinite loop)" do
      mock.proxy(limited_generator).generate.at_least(10)
      limited_generator.sample(10).size.should < 10
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
twitter_cldr-3.6.0 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.5.0 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.4.0 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.3.0 spec/shared/postal_code_generator_spec.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/twitter_cldr-3.2.1/spec/shared/postal_code_generator_spec.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/twitter_cldr-3.2.1/spec/shared/postal_code_generator_spec.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/twitter_cldr-3.1.2/spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.2.1 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.2.0 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.1.2 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.1.1 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.1.0 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.0.10 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.0.9 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.0.8 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.0.7 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.0.6 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.0.5 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.0.4 spec/shared/postal_code_generator_spec.rb
twitter_cldr-3.0.3 spec/shared/postal_code_generator_spec.rb