Created
April 5, 2017 19:08
-
-
Save moonmaster9000/ec0226935da6b870766b4ff6fbf62a09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function RPS(){ | |
this.play = function(p1, p2, ui){ | |
new PlayUseCase(p1, p2, ui).execute() | |
} | |
} | |
function PlayUseCase(p1, p2, ui){ | |
this.execute = function(){ | |
if (inputInvalid()){ | |
ui.invalid() | |
} else if (tie() ){ | |
ui.tie() | |
} else if (p1Wins()){ | |
ui.winner("p1") | |
} else { | |
ui.winner("p2") | |
} | |
} | |
const validInput = ["rock", "paper", "scissors"] | |
function invalid(playerThrow) { | |
return !validInput.includes(playerThrow) | |
} | |
function inputInvalid() { | |
return invalid(p1) || invalid(p2) | |
} | |
function tie() { | |
return p1 === p2 | |
} | |
function p1Wins() { | |
return p1 === "paper" && p2 === "rock" || | |
p1 === "rock" && p2 === "scissors" || | |
p1 === "scissors" && p2 === "paper" | |
} | |
} | |
describe("play", function () { | |
let rps, ui | |
beforeEach(function () { | |
rps = new RPS() | |
}) | |
describe("win scenarios", function () { | |
beforeEach(function () { | |
ui = jasmine.createSpyObj("ui", ["winner"]) | |
}) | |
it("rock v. paper", function () { | |
rps.play("rock", "paper", ui) | |
expect(ui.winner).toHaveBeenCalledWith("p2") | |
}) | |
it("paper v. rock", function () { | |
rps.play("paper", "rock", ui) | |
expect(ui.winner).toHaveBeenCalledWith("p1") | |
}) | |
it("scissors v. rock", function () { | |
rps.play("scissors", "rock", ui) | |
expect(ui.winner).toHaveBeenCalledWith("p2") | |
}) | |
it("rock v. scissors", function () { | |
rps.play("rock", "scissors", ui) | |
expect(ui.winner).toHaveBeenCalledWith("p1") | |
}) | |
it("paper v. scissors", function () { | |
rps.play("paper", "scissors", ui) | |
expect(ui.winner).toHaveBeenCalledWith("p2") | |
}) | |
it("scissors v. paper", function () { | |
rps.play("scissors", "paper", ui) | |
expect(ui.winner).toHaveBeenCalledWith("p1") | |
}) | |
}) | |
describe("ties", function () { | |
let ui | |
beforeEach(function () { | |
ui = jasmine.createSpyObj("ui", ["tie"]) | |
}) | |
it("rock v. rock", function () { | |
rps.play("rock", "rock", ui) | |
expect(ui.tie).toHaveBeenCalled() | |
}) | |
it("paper v. paper", function () { | |
rps.play("paper", "paper", ui) | |
expect(ui.tie).toHaveBeenCalled() | |
}) | |
it("scissors v. scissors", function () { | |
rps.play("scissors", "scissors", ui) | |
expect(ui.tie).toHaveBeenCalled() | |
}) | |
}) | |
describe("invalid scenarios", function () { | |
beforeEach(function () { | |
ui = jasmine.createSpyObj("ui", ["invalid"]) | |
}) | |
it("rock v. sailboat", function () { | |
rps.play("rock", "sailboat", ui) | |
expect(ui.invalid).toHaveBeenCalled() | |
}) | |
it("sailboat v. sailboat", function () { | |
rps.play("sailboat", "sailboat", ui) | |
expect(ui.invalid).toHaveBeenCalled() | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment