Sha256: fffd49e281e58a2edb278435d7e730af2abaa4a4ebf4d40aad4f039bea4a2795

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

require "spec_helper"
require "hamster/set"

describe Hamster::Set do
  [
    [:sort, ->(left, right) { left.length <=> right.length }],
    [:sort_by, ->(item) { item.length }],
  ].each do |method, comparator|
    describe "##{method}" do
      [
        [[], []],
        [["A"], ["A"]],
        [%w[Ichi Ni San], %w[Ni San Ichi]],
      ].each do |values, expected|
        describe "on #{values.inspect}" do
          let(:set) { S[*values] }

          describe "with a block" do
            let(:result) { set.send(method, &comparator) }

            it "returns #{expected.inspect}" do
              result.should eql(SS.new(expected, &comparator))
              result.to_a.should == expected
            end

            it "doesn't change the original Set" do
              result
              set.should eql(S.new(values))
            end
          end

          describe "without a block" do
            let(:result) { set.send(method) }

            it "returns #{expected.sort.inspect}" do
              result.should eql(SS[*expected])
              result.to_a.should == expected.sort
            end

            it "doesn't change the original Set" do
              result
              set.should eql(S.new(values))
            end
          end
        end
      end
    end
  end

  describe "#sort_by" do
    it "only calls the passed block once for each item" do
      count = 0
      fn    = lambda { |x| count += 1; -x }
      items = 100.times.collect { rand(10000) }.uniq

      S[*items].sort_by(&fn).to_a.should == items.sort.reverse
      count.should == items.length
    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/set/sorting_spec.rb
hamster-3.0.0 spec/lib/hamster/set/sorting_spec.rb
hamster-2.0.0 spec/lib/hamster/set/sorting_spec.rb