Skip to content

Commit ae65fc8

Browse files
unknownunknown
authored andcommitted
added tests
1 parent dab5835 commit ae65fc8

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

02week/tests.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"use strict";
2+
3+
const assert = require("assert");
4+
const readline = require("readline");
5+
const rl = readline.createInterface({
6+
input: process.stdin,
7+
output: process.stdout
8+
});
9+
10+
const rockPaperScissors = (hand1, hand2) => {
11+
hand1 = hand1.trim().toLowerCase();
12+
hand2 = hand2.trim().toLowerCase();
13+
14+
if (typeof hand1 !== "string" || typeof hand2 !== "string") {
15+
return `Must enter "rock", "paper" or "scissors"`;
16+
} else if (
17+
(hand1 !== "rock" && hand1 !== "paper" && hand1 !== "scissors") ||
18+
(hand2 !== "rock" && hand2 !== "paper" && hand2 !== "scissors")
19+
) {
20+
return `Must enter "rock", "paper" or "scissors"`;
21+
} else if (hand1 === hand2) {
22+
return "It's a tie!";
23+
} else if (hand1 === "rock" && hand2 === "scissors") {
24+
return `Hand 1 wins!`;
25+
} else if (hand1 === "paper" && hand2 === "rock") {
26+
return `Hand 1 wins!`;
27+
} else if (hand1 === "scissors" && hand2 === "paper") {
28+
return `Hand 1 wins!`;
29+
} else if (hand1 === "scissors" && hand2 === "rock") {
30+
return `Hand 2 wins!`;
31+
} else if (hand1 === "rock" && hand2 === "paper") {
32+
return `Hand 2 wins!`;
33+
} else if (hand1 === "paper" && hand2 === "scissors") {
34+
return `Hand 2 wins!`;
35+
}
36+
};
37+
38+
function getPrompt() {
39+
rl.question("hand1: ", answer1 => {
40+
rl.question("hand2: ", answer2 => {
41+
console.log(rockPaperScissors(answer1, answer2));
42+
getPrompt();
43+
});
44+
});
45+
}
46+
47+
// Tests
48+
49+
if (typeof describe === "function") {
50+
describe("#rockPaperScissors()", () => {
51+
it("should detect a tie", () => {
52+
assert.equal(rockPaperScissors("rock", "rock"), "It's a tie!");
53+
assert.equal(rockPaperScissors("paper", "paper"), "It's a tie!");
54+
assert.equal(rockPaperScissors("scissors", "scissors"), "It's a tie!");
55+
});
56+
it("should detect which hand won", () => {
57+
assert.equal(rockPaperScissors("rock", "paper"), `Hand 2 wins!`);
58+
assert.equal(rockPaperScissors("paper", "scissors"), `Hand 2 wins!`);
59+
assert.equal(rockPaperScissors("rock", "scissors"), `Hand 1 wins!`);
60+
});
61+
it('should scrub input to ensure lowercase with "trim"ed whitepace', () => {
62+
assert.equal(rockPaperScissors("rOcK", " paper "), `Hand 2 wins!`);
63+
assert.equal(rockPaperScissors("Paper", "SCISSORS"), `Hand 2 wins!`);
64+
assert.equal(rockPaperScissors("rock ", "sCiSsOrs"), `Hand 1 wins!`);
65+
});
66+
it("should scrub input to only accept rock paper or scissors", () => {
67+
assert.equal(
68+
rockPaperScissors("hammer", " paper "),
69+
`Must enter "rock", "paper" or "scissors"`
70+
);
71+
assert.equal(
72+
rockPaperScissors("Paper", "brick"),
73+
`Must enter "rock", "paper" or "scissors"`
74+
);
75+
assert.equal(
76+
rockPaperScissors("ball ", "sCiSsOrs"),
77+
`Must enter "rock", "paper" or "scissors"`
78+
);
79+
});
80+
it("must enter string with no numbers", () => {
81+
assert.equal(
82+
rockPaperScissors("9776", " paper "),
83+
`Must enter "rock", "paper" or "scissors"`
84+
);
85+
assert.equal(
86+
rockPaperScissors("Paper", "99999"),
87+
`Must enter "rock", "paper" or "scissors"`
88+
);
89+
assert.equal(
90+
rockPaperScissors("4355", "sCiSsOrs"),
91+
`Must enter "rock", "paper" or "scissors"`
92+
);
93+
});
94+
});
95+
} else {
96+
getPrompt();
97+
}

0 commit comments

Comments
 (0)