Skip to content

Commit

Permalink
bugfix: handled acessing last element of an empty queue
Browse files Browse the repository at this point in the history
  • Loading branch information
hbarcelos committed Feb 5, 2020
1 parent d67fdf6 commit b478170
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions round-linked-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class RoundLinkedQueue {
}

get last() {
if (!this._last) {
throw new Error("Cannot access the last element of an empty queue");
}

return this._last.data;
}

Expand Down
6 changes: 6 additions & 0 deletions round-linked-queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,11 @@ describe("Round-Queue", () => {

expect(() => queue.first).to.throw("Cannot access the first element of an empty queue");
});

it("Should throw a proper error when acessing the last element of an empty queue", () => {
const queue = new RoundQueue(3);

expect(() => queue.last).to.throw("Cannot access the last element of an empty queue");
});
});
});

0 comments on commit b478170

Please sign in to comment.