Skip to content

Commit

Permalink
bugfix: handled acessing first 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 f4f8c9a commit d67fdf6
Show file tree
Hide file tree
Showing 2 changed files with 12 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 @@ -17,6 +17,10 @@ class RoundLinkedQueue {
}

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

return this._first.data;
}

Expand Down
8 changes: 8 additions & 0 deletions round-linked-queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,12 @@ describe("Round-Queue", () => {
expect(() => queue.remove()).to.throw("Cannot remove element from an empty queue");
});
});

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

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

0 comments on commit d67fdf6

Please sign in to comment.