summaryrefslogtreecommitdiff
path: root/spec/history_array_spec.rb
blob: 041cda4dca55dac9c989165ecf24e75256f9bb7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require 'helper'

describe Pry::HistoryArray do
  before do
    @array = Pry::HistoryArray.new 10
    @populated = @array.dup << 1 << 2 << 3 << 4
  end

  it 'should have a maximum size specifed at creation time' do
    @array.max_size.should == 10
  end

  it 'should be able to be added objects to' do
    @populated.size.should == 4
    @populated.to_a.should == [1, 2, 3, 4]
  end

  it 'should be able to access single elements' do
    @populated[2].should == 3
  end

  it 'should be able to access negative indices' do
    @populated[-1].should == 4
  end

  it 'should be able to access ranges' do
    @populated[1..2].should == [2, 3]
  end

  it 'should be able to access ranges starting from a negative index' do
    @populated[-2..3].should == [3, 4]
  end

  it 'should be able to access ranges ending at a negative index' do
    @populated[2..-1].should == [3, 4]
  end

  it 'should be able to access ranges using only negative indices' do
    @populated[-2..-1].should == [3, 4]
  end

  it 'should be able to use range where end is excluded' do
    @populated[-2...-1].should == [3]
  end

  it 'should be able to access slices using a size' do
    @populated[-3, 2].should == [2, 3]
  end

  it 'should remove older entries' do
    11.times { |n| @array << n }

    @array[0].should  == nil
    @array[1].should  == 1
    @array[10].should == 10
  end

  it 'should not be larger than specified maximum size' do
    12.times { |n| @array << n }
    @array.entries.compact.size.should == 10
  end

  it 'should pop!' do
    @populated.pop!
    @populated.to_a.should == [1, 2, 3]
  end

  it 'should return an indexed hash' do
    @populated.to_h[0].should == @populated[0]
  end
end