Skip to content

Instantly share code, notes, and snippets.

@thomaslombart
Last active November 27, 2018 15:46
Show Gist options
  • Save thomaslombart/4832c20ba7ed42276d68ecef0b80c50a to your computer and use it in GitHub Desktop.
Save thomaslombart/4832c20ba7ed42276d68ecef0b80c50a to your computer and use it in GitHub Desktop.
Example of a stack implemented with test-driven development
class Stack {
constructor (capacity) {
this.items = []
this.capacity = capacity
}
isEmpty () {
return this.items.length === 0
}
isFull () {
return this.items.length === this.capacity
}
push (element) {
if (this.isFull()) {
return 'Full'
}
this.items.push(element)
return element
}
pop () {
return this.isEmpty() ? 'Empty' : this.items.pop()
}
peek () {
return this.isEmpty() ? 'Empty' : this.items[this.items.length - 1]
}
}
module.exports = Stack
const Stack = require('./stack')
describe('Stack', () => {
let stack
beforeEach(() => {
stack = new Stack(3)
})
it('Should construct the stack with a given capacity', () => {
expect(stack.items).toEqual([])
expect(stack.capacity).toBe(3)
})
it('Should have an isEmpty function that returns true if the stack is empty and false otherwise', () => {
expect(stack.isEmpty()).toBe(true)
stack.items.push(2)
expect(stack.isEmpty()).toBe(false)
})
it('Should have an isFull function that returns true if the stack is full and false otherwise', () => {
expect(stack.isFull()).toBe(false)
stack.items = [4, 5, 6]
expect(stack.isFull()).toBe(true)
})
describe('Push', () => {
it('Should add a new element on top of the stack', () => {
stack.push(2)
expect(stack.items[stack.items.length - 1]).toBe(2)
})
it('Should return the new element pushed at the top of the stack', () => {
let elementPushed = stack.push(2)
expect(elementPushed).toBe(2)
})
it('Should return full if one tries to push at the top of the stack while it is full', () => {
stack.items = [1, 2, 3]
let element = stack.push(4)
expect(stack.items[stack.items.length - 1]).toBe(3)
expect(element).toBe('Full')
})
})
describe('Pop', () => {
it('Should removes the last element at the top of a stack', () => {
stack.items = [1, 2, 3]
stack.pop()
expect(stack.items).toEqual([1, 2])
})
it('Should returns the element that have been just removed', () => {
stack.items = [1, 2, 3]
let element = stack.pop()
expect(element).toBe(3)
})
it('Should return Empty if one tries to pop an empty stack', () => {
// By default, the stack is empty
expect(stack.pop()).toBe('Empty')
})
})
describe('Peek', () => {
it('Should returns the element at the top of the stack', () => {
stack.items = [1, 2, 3]
let element = stack.peek()
expect(element).toBe(3)
})
it('Should return Empty if one tries to peek an empty stack', () => {
// By default, the stack is empty
expect(stack.peek()).toBe('Empty')
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment