2014-03-14 00:31:24 -04:00
|
|
|
require_relative 'helper'
|
2011-05-15 05:29:14 -04:00
|
|
|
|
|
|
|
describe Pry::HistoryArray do
|
|
|
|
before do
|
|
|
|
@array = Pry::HistoryArray.new 10
|
2012-09-17 02:52:53 -04:00
|
|
|
@populated = @array.dup << 1 << 2 << 3 << 4
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should have a maximum size specifed at creation time' do
|
2015-01-22 16:52:20 -05:00
|
|
|
@array.max_size.should eq 10
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to be added objects to' do
|
2015-01-22 16:52:20 -05:00
|
|
|
@populated.size.should eq 4
|
|
|
|
@populated.to_a.should eq [1, 2, 3, 4]
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to access single elements' do
|
2015-01-22 16:52:20 -05:00
|
|
|
@populated[2].should eq 3
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to access negative indices' do
|
2015-01-22 16:52:20 -05:00
|
|
|
@populated[-1].should eq 4
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to access ranges' do
|
2015-01-22 16:52:20 -05:00
|
|
|
@populated[1..2].should eq [2, 3]
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to access ranges starting from a negative index' do
|
2015-01-22 16:52:20 -05:00
|
|
|
@populated[-2..3].should eq [3, 4]
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to access ranges ending at a negative index' do
|
2015-01-22 16:52:20 -05:00
|
|
|
@populated[2..-1].should eq [3, 4]
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to access ranges using only negative indices' do
|
2015-01-22 16:52:20 -05:00
|
|
|
@populated[-2..-1].should eq [3, 4]
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to use range where end is excluded' do
|
2015-01-22 16:52:20 -05:00
|
|
|
@populated[-2...-1].should eq [3]
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to access slices using a size' do
|
2015-01-22 16:52:20 -05:00
|
|
|
@populated[-3, 2].should eq [2, 3]
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should remove older entries' do
|
|
|
|
11.times { |n| @array << n }
|
|
|
|
|
2015-01-22 16:52:20 -05:00
|
|
|
@array[0].should eq nil
|
|
|
|
@array[1].should eq 1
|
|
|
|
@array[10].should eq 10
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|
2012-08-20 17:24:57 -04:00
|
|
|
|
|
|
|
it 'should not be larger than specified maximum size' do
|
|
|
|
12.times { |n| @array << n }
|
2015-01-22 16:52:20 -05:00
|
|
|
@array.entries.compact.size.should eq 10
|
2012-08-20 17:24:57 -04:00
|
|
|
end
|
2012-09-17 03:20:52 -04:00
|
|
|
|
|
|
|
it 'should pop!' do
|
|
|
|
@populated.pop!
|
2015-01-22 16:52:20 -05:00
|
|
|
@populated.to_a.should eq [1, 2, 3]
|
2012-09-17 03:20:52 -04:00
|
|
|
end
|
2013-12-07 07:09:42 -05:00
|
|
|
|
|
|
|
it 'should return an indexed hash' do
|
2015-01-22 16:52:20 -05:00
|
|
|
@populated.to_h[0].should eq @populated[0]
|
2013-12-07 07:09:42 -05:00
|
|
|
end
|
2011-05-15 05:29:14 -04:00
|
|
|
end
|