Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re implement traversal #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions test/6_vampireWithName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const chai = require('chai');
const expect = chai.expect;

const Vampire = require('../vampire.js');

describe("Vampire", function() {

let rootVampire;
beforeEach( function() {
rootVampire = new Vampire("root");
});

describe("vampireWithName", function() {
let offspring1, offspring2, offspring3, offspring4, offspring5;
beforeEach(() => {
offspring1 = new Vampire("andrew");
offspring2 = new Vampire("sarah");
offspring3 = new Vampire("c");
offspring4 = new Vampire("d");
offspring5 = new Vampire("e");
rootVampire.addOffspring(offspring1);
offspring1.addOffspring(offspring2);
rootVampire.addOffspring(offspring3);
offspring3.addOffspring(offspring4);
offspring4.addOffspring(offspring5);
});

context("when searching with a name that exists in the tree", () => {
it("should return the vampire with that name", () => {
expect(rootVampire.vampireWithName(rootVampire.name).name).to.equal(rootVampire.name);
expect(rootVampire.vampireWithName(offspring1.name).name).to.equal(offspring1.name);
expect(rootVampire.vampireWithName(offspring2.name).name).to.equal(offspring2.name);
expect(rootVampire.vampireWithName(offspring5.name).name).to.equal(offspring5.name);
expect(offspring3.vampireWithName(offspring5.name).name).to.equal(offspring5.name);
});
});

context("when searching with a name that does not exist in the tree", () => {
it("should return null", () => {
expect(rootVampire.vampireWithName("")).to.equal(null);
expect(offspring2.vampireWithName(offspring5.name)).to.equal(null);
});
});
});
});
42 changes: 42 additions & 0 deletions test/7_totalDescendents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const chai = require('chai');
const expect = chai.expect;

const Vampire = require('../vampire.js');

describe("Vampire", function() {

let rootVampire;
beforeEach( function() {
rootVampire = new Vampire("root");
});

describe("totalDescendents", function() {
let offspring1, offspring2, offspring3, offspring4, offspring5, offspring6, offspring7, offspring8;
beforeEach(() => {
offspring1 = new Vampire("a");
offspring2 = new Vampire("b");
offspring3 = new Vampire("c");
offspring4 = new Vampire("d");
offspring5 = new Vampire("e");
offspring6 = new Vampire("f");
offspring7 = new Vampire("g");
offspring8 = new Vampire("h");

rootVampire.addOffspring(offspring1);
rootVampire.addOffspring(offspring2);
rootVampire.addOffspring(offspring3);
offspring3.addOffspring(offspring4);
offspring3.addOffspring(offspring5);
offspring5.addOffspring(offspring6);
offspring6.addOffspring(offspring7);
offspring2.addOffspring(offspring8);
});

it("should give the total descendents underneath a specific vampire", () => {
expect(rootVampire.totalDescendents).to.equal(8);
expect(offspring1.totalDescendents).to.equal(0);
expect(offspring2.totalDescendents).to.equal(1);
expect(offspring3.totalDescendents).to.equal(4);
});
});
});
43 changes: 43 additions & 0 deletions test/8_allMillennialVampires.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const chai = require('chai');
const expect = chai.expect;

const Vampire = require('../vampire.js');

describe("Vampire", function() {

let rootVampire;
beforeEach( function() {
rootVampire = new Vampire("root");
});

describe("allMillennialVampires", function() {
let offspring1, offspring2, offspring3, offspring4, offspring5, offspring6, offspring7, offspring8;
beforeEach(() => {
offspring1 = new Vampire("a", 1000);
offspring2 = new Vampire("b", 900);
offspring3 = new Vampire("c", 1400);
offspring4 = new Vampire("d", 1890);
offspring5 = new Vampire("e", 1990);
offspring6 = new Vampire("f", 2000);
offspring7 = new Vampire("g", 2010);
offspring8 = new Vampire("h", 2017);

rootVampire.addOffspring(offspring1);
rootVampire.addOffspring(offspring2);
rootVampire.addOffspring(offspring3);
offspring3.addOffspring(offspring4);
offspring3.addOffspring(offspring5);
offspring5.addOffspring(offspring6);
offspring6.addOffspring(offspring7);
offspring2.addOffspring(offspring8);
});

it("should return an array of all vampires converted after 1980", () => {
expect(rootVampire.allMillennialVampires.length).to.equal(4); //[offspring5, offspring6, offspring7, offspring8]
expect(rootVampire.allMillennialVampires).to.include(offspring5);
expect(rootVampire.allMillennialVampires).to.include(offspring6);
expect(rootVampire.allMillennialVampires).to.include(offspring7);
expect(rootVampire.allMillennialVampires).to.include(offspring8);
});
});
});
17 changes: 17 additions & 0 deletions vampire.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ class Vampire {

}

/** Tree traversal methods **/

// Returns the vampire object with that name, or null if no vampire exists with that name
vampireWithName(name) {

}

// Returns the total number of vampires that exist
get totalDescendents() {

}

// Returns an array of all the vampires that were converted after 1980
get allMillennialVampires() {

}

/** Stretch **/

// Returns the closest common ancestor of two vampires.
Expand Down