Make Snapshot#crop return the snapshot of exact requested height

This commit is contained in:
Marcin Kulik 2013-08-05 18:18:49 +02:00
parent 7c9a9842af
commit 4767442875
2 changed files with 9 additions and 4 deletions

View File

@ -18,8 +18,13 @@ class Snapshot
end end
def crop(width, height) def crop(width, height)
height = [height, lines.size].min min_height = [height, lines.size].min
new_lines = lines.drop(lines.size - height).map { |line| line.crop(width) } new_lines = lines.drop(lines.size - min_height).map { |line| line.crop(width) }
while new_lines.size < height
new_lines << []
end
self.class.new(new_lines) self.class.new(new_lines)
end end

View File

@ -98,9 +98,9 @@ describe Snapshot do
context "when height is greater than lines count" do context "when height is greater than lines count" do
let(:height) { 4 } let(:height) { 4 }
it 'returns a new Snapshot with all lines cropped' do it 'returns a new Snapshot with all lines cropped and missing lines added' do
expect(subject).to eq(Snapshot.new([cropped_line_1, cropped_line_2, expect(subject).to eq(Snapshot.new([cropped_line_1, cropped_line_2,
cropped_line_3])) cropped_line_3, []]))
end end
end end
end end