-
Notifications
You must be signed in to change notification settings - Fork 24
/
quiz-tester.js
147 lines (116 loc) · 4.65 KB
/
quiz-tester.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*global isGameOver, whoWon, playTurn, restart, currentQuestion, numberOfQuestions, numberOfAnswers, correctAnswer*/
/* quiz-tester.js
This script will test the game logic of your multiple choice quiz.
To use it you will need to include it in your html file after you main quiz script.
You will need to declare the following functions in the global scope:
# numberOfQuestions()
It should return an integer that is the number of questions in a game
# currentQuestion()
It should return an integer that is the zero-based index of the current question in the quiz
# correctAnswer()
It should return an integer that is the zero-based index the correct answer for the currrent question
# numberOfAnswers()
It should return an integer that is the number of choices for the current question
# playTurn(choice)
It should take a single integer, which specifies which choice the current player wants to make.
It should return a boolean true/false if the answer is correct.
# isGameOver()
It should return a true or false if the quiz is over.
# whoWon()
It should return 0 if the game is not yet finished.
Else it should return either 1 or 2 depending on which player won.
It should return 3 if the game is a draw.
# restart()
It should restart the game so it can be played again.
ASSUMPTIONS
It is assumed that the turns of the player will be automatically changed after each turn.
The application will console log all the passed or failed test */
// CONFIG
var showPasses = true
var playerOne = 1
var playerTwo = 2
var playerDraw = 3
var testsRun = 0
var testsPassed = 0
var testsFailed = 0
// TESTS TO RUN
console.log('==================================')
console.log('RUNNING QUIZ TESTER')
console.log('==================================')
simulateGame('P1 Wins', playerOne)
simulateGame('P2 Wins', playerTwo)
if (numberOfQuestions() % 2 === 0) {
simulateGame('Draw Game', playerDraw)
}
console.log('==================================')
console.log(testsRun + ' TESTS RUN')
if (testsPassed > 0) console.log('%c>' + testsPassed + ' TESTS PASSED', 'color: green')
if (testsFailed > 0) console.log('%c>' + testsFailed + ' TESTS FAILED', 'color: red')
// restart the game so it can run normally
restart()
function simulateGame (testTitle, winner) {
console.log('-------------------------------')
console.log('Testing: ' + testTitle)
console.log('-------------------------------')
restart()
expect('isGameOver should return false at start of game', isGameOver(), false)
expect('whoWon should return 0 at start of the game', whoWon(), 0)
var questionCount = numberOfQuestions()
expect('numberOfQuestions should be greater than 0', questionCount, 0, '>')
for (var i = 0; i < questionCount; ++i) {
expect('currentQuestion should be equal to ' + i, currentQuestion(), i)
// if these tests fails don't continue as the logic below depends on them
var answerCount = numberOfAnswers()
var passed = expect('numberOfAnswers should be greater than 1', answerCount, 1, '>')
if (!passed) {
return
}
var answer = correctAnswer()
passed = expect('correctAnswer should be greater than or equal to 0', answer, 0, '>=')
if (!passed) {
return
}
var shouldGuessCorrect = false
if (winner === 3) {
shouldGuessCorrect = true
} else if (i % 2 === 0) {
shouldGuessCorrect = (winner === 1)
} else {
shouldGuessCorrect = (winner === 2)
}
if (!shouldGuessCorrect) {
var incorrectAnswer = 0
while (incorrectAnswer === answer) {
incorrectAnswer++
}
}
// then guess correct
if (shouldGuessCorrect) expect('playTurn should return true if correctAnswer', playTurn(answer), true)
else expect('playTurn should return false if incorrect answer', playTurn(incorrectAnswer), false)
}
playTurn(0)
console.log(currentQuestion() + ' ' + questionCount)
expect('playTurn should not be allow after gameover, currentTurn should not advance', currentQuestion(), questionCount)
expect('whoWon should return ' + winner + ' at end of the game', whoWon(), winner)
}
function expect (expectationMessage, testFunctionResult, returnValue, operator) {
++testsRun
var passed = false
if (operator && operator !== '===') {
switch (operator) {
case '>' : passed = (testFunctionResult > returnValue)
break
case '>=' : passed = (testFunctionResult >= returnValue)
}
} else {
passed = (testFunctionResult === returnValue)
}
if (passed) {
++testsPassed
if (showPasses) console.log('%c[PASS] ' + expectationMessage, 'color: green')
return true
}
++testsFailed
console.log('%c[FAIL] ' + expectationMessage + ' but it returned ' + testFunctionResult, 'color: red')
return false
}