Skip to content

Instantly share code, notes, and snippets.

@FredrikWendt
Created October 16, 2014 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FredrikWendt/0844a76af178a6750fbf to your computer and use it in GitHub Desktop.
Save FredrikWendt/0844a76af178a6750fbf to your computer and use it in GitHub Desktop.
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