Skip to content

Commit

Permalink
- Add failing test test_startGame_answerZeroOutOfTwoCorrectly_scoresZ…
Browse files Browse the repository at this point in the history
…ero()

- Create scoring function to calculate the score
- Use the scoring function inside the startGame()
  • Loading branch information
tyegah committed May 22, 2022
1 parent fb999f6 commit 4d8d26a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 6 additions & 2 deletions EDGameEngine/Game.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ public class Game<Question, Answer:Equatable, R:Router> where R.Question == Ques
}
}

public func startGame<Question, Answer:Equatable, R:Router>(questions:[Question], router: R, answers:[Question:Answer]) -> Game<Question, Answer, R> where R.Question == Question, R.Answer == Answer {
let flow = Flow(questions: questions, router: router, scoring: { _ in 1})
public func startGame<Question, Answer:Equatable, R:Router>(questions:[Question], router: R, correctAnswers:[Question:Answer]) -> Game<Question, Answer, R> where R.Question == Question, R.Answer == Answer {
let flow = Flow(questions: questions, router: router, scoring: { scoring($0, correctAnswers: correctAnswers) })
flow.start() // at this point, the GameTests will crash because the flow is instantiated and removed right away from memory without anything holding it
// And that is why we should create something that holds the flow variable without exposing it's methods and stuff
return Game(flow: flow)
}

private func scoring<Question:Hashable, Answer:Equatable>(_ answers:[Question:Answer], correctAnswers:[Question:Answer]) -> Int {
return 1
}
14 changes: 12 additions & 2 deletions EDGameEngineTests/GameTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ class GameTests:XCTestCase {
// Here we want to start testing if the game scoring function is running correctly,
// and we start out by calculating two questions with one answer correct
// we simulate the startGame function and also simulate the answer with router answercallback
func test_startGame_answerOneOutOfTwoCorrectly_scores1() {
func test_startGame_answerOneOutOfTwoCorrectly_scoresOne() {
let router = RouterSpy()
let game = startGame(questions: ["Q1", "Q2"], router: router, answers: ["Q1":"A1", "Q2":"A2"])
game = startGame(questions: ["Q1", "Q2"], router: router, correctAnswers: ["Q1":"A1", "Q2":"A2"])
router.answerCallback("A1")
router.answerCallback("wrong answer")
XCTAssertEqual(router.routedResult!.score, 1)
}

// Because we hardcoded the score on the first test, we add another test to check for the scoring function
// In this stage, we will create the scoring method to really calculate the score
func test_startGame_answerZeroOutOfTwoCorrectly_scoresZero() {
let router = RouterSpy()
game = startGame(questions: ["Q1", "Q2"], router: router, correctAnswers: ["Q1":"A1", "Q2":"A2"])
router.answerCallback("wrong answer")
router.answerCallback("wrong answer")
XCTAssertEqual(router.routedResult!.score, 0)
}
}

0 comments on commit 4d8d26a

Please sign in to comment.