-
# frozen_string_literal: true
-
-
1
require_relative '../structures/stack'
-
1
require_relative '../custom_exceptions/stack_is_empty'
-
-
1
RSpec.describe Stack do
-
8
let(:stack) { Stack.new }
-
4
let(:rand_element) { Faker::Number.number(digits: 5) }
-
-
1
shared_examples 'when stack is empty' do |method|
-
2
it "raise StackIsEmpty exception on #{method}" do
-
4
expect{stack.public_send(method)}.to raise_exception(StackIsEmpty)
-
end
-
end
-
-
1
describe 'methods' do
-
1
describe 'push' do
-
1
it 'should push an element onto the top' do
-
1
stack.push(rand_element)
-
-
1
expect(stack.peak).to eq(rand_element)
-
end
-
end
-
-
1
describe 'pop' do
-
1
it 'return the top element (and removing it) when the stack has element' do
-
1
stack.push(rand_element)
-
-
1
expect(stack.pop).to eq(rand_element)
-
end
-
-
1
it_behaves_like 'when stack is empty', :pop
-
end
-
-
1
describe 'peak' do
-
1
it 'return the top element (without removing it) when the stack has element' do
-
1
stack.push(rand_element)
-
-
1
expect(stack.peak).to eq(rand_element)
-
1
expect(stack.empty?).to be_falsey
-
end
-
-
1
it_behaves_like 'when stack is empty', :peak
-
end
-
-
1
describe 'empty?' do
-
1
it 'return false if the stack has element' do
-
1
stack.push(1)
-
-
1
expect(stack.empty?).to be_falsey
-
end
-
-
1
it 'return true if the stack is empty' do
-
1
expect(stack.empty?).to be_truthy
-
end
-
end
-
end
-
end