Sha256: 15175a54f3066fedb29a3b703761e97a9e3cbdc8306aa4ccbde41a0bc2dfe263

Contents?: true

Size: 748 Bytes

Versions: 3

Compression:

Stored size: 748 Bytes

Contents

require "spec_helper"
require "hamster/hash"

describe Hamster::Hash do
  let(:hash) { H[a: 3, b: 2, c: 1] }

  describe "#sort" do
    it "returns a Vector of sorted key/val pairs" do
      hash.sort.should eql(V[[:a, 3], [:b, 2], [:c, 1]])
    end

    it "works on large hashes" do
      array = (1..1000).map { |n| [n,n] }
      H.new(array.shuffle).sort.should eql(V.new(array))
    end

    it "uses block as comparator to sort if passed a block" do
      hash.sort { |a,b| b <=> a }.should eql(V[[:c, 1], [:b, 2], [:a, 3]])
    end
  end

  describe "#sort_by" do
    it "returns a Vector of key/val pairs, sorted using the block as a key function" do
      hash.sort_by { |k,v| v }.should eql(V[[:c, 1], [:b, 2], [:a, 3]])
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/hamster-3.0.0/spec/lib/hamster/hash/sort_spec.rb
hamster-3.0.0 spec/lib/hamster/hash/sort_spec.rb
hamster-2.0.0 spec/lib/hamster/hash/sort_spec.rb