<!DOCTYPE html>
<html>
<head>
<link data-require="mocha@*" data-semver="1.13.0" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/mocha/1.13.0/mocha.css" />
<script data-require="mocha@*" data-semver="1.13.0" src="//cdnjs.cloudflare.com/ajax/libs/mocha/1.13.0/mocha.js"></script>
<script data-require="mocha-chai@1.9.0" data-semver="1.9.0" src="//cdnjs.cloudflare.com/ajax/libs/chai/1.9.0/chai.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="stack.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>mocha.setup('bdd')</script>
<script src="stack-spec.js"></script>
<script>mocha.run()</script>
</body>
</html>
// Code goes here
function Stack(){
this.data = [];
this.push = function(element) {
this.data.push(element);
}
this.pop = function() {
var index = this.data.length-1
var top = this.data[index];
this.data.splice(index, 1);
return top;
}
}
/* Styles go here */
Testing a Stack structure with Mocha and Chai
var expect = chai.expect
describe('Stack', function() {
it('creates a Stack', function() {
expect(new Stack()).to.exist
});
it('has a data array', function() {
var stack = new Stack();
expect(stack.data).to.exist;
expect(stack.data).to.be.an('array');
});
it('push an element to the stack', function() {
var stack = new Stack()
stack.push(1);
expect(stack.data).to.have.lengthOf(1);
});
it('pop an element from the stack and return it', function() {
var stack = new Stack();
stack.push(1);
var element = stack.pop();
expect(element).to.be.equal(1);
expect(stack.data).to.have.lengthOf(0);
});
it('push and pop from the top', function() {
var stack = new Stack();
stack.push(1);
stack.push(2);
expect(stack.pop()).to.be.equal(2);
expect(stack.pop()).to.be.equal(1);
});
});