Sha256: b2d134a80f840f748c5f9ccd344dff040f8f6f888767b80f15f9316b4864b8ce

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

require 'naturally'

describe Naturally do
  describe '#sort' do
    it 'sorts an array of strings nicely as if they were legal numbers' do
      a = %w[676 676.1 676.11 676.12 676.2 676.3 676.9 676.10]
      b = %w[676 676.1 676.2 676.3 676.9 676.10 676.11 676.12]
      Naturally.sort(a).should == b
    end
    
    it 'sorts a smaller array of strings nicely as if they were legal numbers' do
      a = %w[676 676.1 676.11 676.12 676.2 676.3 676.9]
      b = %w[676 676.1 676.2 676.3 676.9 676.11 676.12]
      Naturally.sort(a).should == b
    end
    
    it 'sorts a more complex list of strings' do
      a = %w[350 351 352 352.1 352.5 353.1 354 354.3 354.4 354.45 354.5]
      b = %w[350 351 352 352.1 352.5 353.1 354 354.3 354.4 354.5 354.45]
      Naturally.sort(a).should == b
    end

    it 'sorts when numbers have letters in them' do
      a = %w[335 335.1 336a 336 337 337a 337.1 337.15 337.2]
      b = %w[335 335.1 336 336a 337 337.1 337.2 337.15 337a]
      Naturally.sort(a).should == b
    end
    
    it 'sorts double digits with letters correctly' do
      a = %w[12a 12b 12c 13a 13b 2 3 4 5 10 11 12]
      b = %w[2 3 4 5 10 11 12 12a 12b 12c 13a 13b]
      Naturally.sort(a).should == b
    end
  end
  
  describe '#normalize' do
    it 'enables sorting objects by one particular attribute' do
      Thing = Struct.new(:number, :name)
      objects = [
        Thing.new('1.1', 'color'),
        Thing.new('1.2', 'size'),
        Thing.new('1.1.1', 'opacity'),
        Thing.new('1.1.2', 'lightness'),
        Thing.new('1.10', 'hardness'),
        Thing.new('2.1', 'weight'),
        Thing.new('1.3', 'shape')
        ]
      sorted = objects.sort_by{ |o| Naturally.normalize(o.number) }
      sorted.map{|o| o.name}.should == %w[
        color
        opacity
        lightness
        size
        shape
        hardness
        weight
        ]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
naturally-1.0.4 spec/naturally_spec.rb
naturally-1.0.3 spec/naturally_spec.rb