Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lisa Bart and Millhouse, not members of Disney
var Child = function(name) {
this.nextChild = null;
this.name = name;
}
var DisneyClub = function() {
this.toppen = null;
this.shift = function(newTop) {
newTop.nextChild = this.toppen;
this.toppen = newTop;
};
this.pop = function() {
if (this.toppen === null) {
throw "DisneyClub has no children to pop";
}
var oldTop = this.toppen;
this.toppen = oldTop.nextChild;
return oldTop;
};
}
var assertEquals = function(expected, actual) {
if (expected !== actual) {
throw "Expected " + expected + " but was " + actual;
}
console.log(".");
}
var test_adding_lisa_returns_lisa = function() {
var club = new DisneyClub();
var lisa = new Child("Lisa");
club.shift(lisa);
var topChild = club.pop();
assertEquals(lisa.name, topChild.name);
}();
var test_adding_lisa_and_then_bart_returns_bart_then_lisa = function() {
var club = new DisneyClub();
var lisa = new Child("Lisa");
var bart = new Child("Bart");
club.shift(lisa);
club.shift(bart);
assertEquals(bart.name, club.pop().name);
assertEquals(lisa.name, club.pop().name);
}();
var test_adding_lisa_millhouse_and_bart_returns_bart_then_lisa = function() {
var club = new DisneyClub();
var lisa = new Child("Lisa");
var millhouse = new Child("Millhouse");
var bart = new Child("Bart");
club.shift(lisa);
club.shift(millhouse);
club.shift(bart);
assertEquals(bart.name, club.pop().name);
assertEquals(millhouse.name, club.pop().name);
assertEquals(lisa.name, club.pop().name);
}();
var fail = function(errorMessage) {
throw errorMessage;
}
var test_getting_top_on_empty_queue = function() {
var club = new DisneyClub();
try {
club.pop();
fail("Calling pop on empty queue should throw exception");
} catch (errorCaught) {
assertEquals("DisneyClub has no children to pop", errorCaught);
}
}();
console.log("All tests run");
ceda@wendt-mbair:~/utbildning/yh-gbg/clean_code$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.