1
|
|
|
/** |
2
|
|
|
* Test for class gamebrain. |
3
|
|
|
*/ |
4
|
|
|
"use strict"; |
5
|
|
|
|
6
|
|
|
/* global describe it */ |
7
|
|
|
|
8
|
|
|
var assert = require("assert"); |
9
|
|
|
const Gamebrain = require("../../lib/Memory/gamebrain"); |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Check if card is set correctly |
14
|
|
|
*/ |
15
|
|
|
function checkSetCardValue(player, colorclass, cardvalue, expectedcardvalues) { |
16
|
|
|
let gamebrain = new Gamebrain(); |
17
|
|
|
let res; |
18
|
|
|
|
19
|
|
|
gamebrain.setCardValue(player, colorclass, cardvalue); |
20
|
|
|
res = gamebrain.cardvalues; |
21
|
|
|
assert.deepEqual(res, expectedcardvalues); |
22
|
|
|
|
23
|
|
|
gamebrain.setCardValue(player, colorclass, cardvalue); |
24
|
|
|
res = gamebrain.paircheck; |
25
|
|
|
assert.equal(res, colorclass); |
26
|
|
|
|
27
|
|
|
res = gamebrain.numberofplayermoves; |
28
|
|
|
assert.equal(res, 0); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Check if nickname is uniqified correctly |
33
|
|
|
*/ |
34
|
|
|
function checkUniqifining(nickone, nicktwo, nickthree, expected) { |
35
|
|
|
let gamebrain = new Gamebrain(); |
36
|
|
|
let res; |
37
|
|
|
|
38
|
|
|
gamebrain.addPlayer(nickone, "blackplayer"); |
39
|
|
|
if (nickthree != "") { |
40
|
|
|
gamebrain.addPlayer(nickthree, "blueplayer"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
res = gamebrain.uniquifyname(nicktwo); |
44
|
|
|
|
45
|
|
|
assert.equal(res, expected); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Check setting of active player |
50
|
|
|
*/ |
51
|
|
|
function checkSetActivePlayer(players) { |
52
|
|
|
let gamebrain = new Gamebrain(); |
53
|
|
|
let firstplayer; |
54
|
|
|
let res; |
55
|
|
|
|
56
|
|
|
for (let i = 0; i < players.length; i++) { |
57
|
|
|
gamebrain.addPlayer(players[i], "blackplayer"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
firstplayer = gamebrain.setActivePlayer(true); |
61
|
|
|
gamebrain.numberofplayermoves = 0; |
62
|
|
|
res = gamebrain.setActivePlayer(); |
63
|
|
|
|
64
|
|
|
assert.equal(res, firstplayer); |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
gamebrain.numberofplayermoves = 2; |
68
|
|
|
res = gamebrain.setActivePlayer(); |
69
|
|
|
|
70
|
|
|
assert.notEqual(res, firstplayer); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check setting of player color |
75
|
|
|
*/ |
76
|
|
|
function checkSetPlayerColor(players) { |
77
|
|
|
let gamebrain = new Gamebrain(); |
78
|
|
|
let colors = []; |
79
|
|
|
var sortedcolors; |
80
|
|
|
var res = []; |
81
|
|
|
|
82
|
|
|
for (let i = 0; i < players.length; i++) { |
83
|
|
|
colors.push(gamebrain.setPlayerColor(players[i])); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
sortedcolors = colors.slice().sort(); |
87
|
|
|
|
88
|
|
|
for (var i = 0; i < sortedcolors.length - 1; i++) { |
89
|
|
|
if (sortedcolors[i + 1] == sortedcolors[i]) { |
90
|
|
|
res.push(sortedcolors[i]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
assert.equal(res.length, 0); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Testsuite |
99
|
|
|
*/ |
100
|
|
|
describe("Check gamebrain", function() { |
101
|
|
|
var cardvalues = [ |
102
|
|
|
{player: "Magnus", colorclass: "blueplayer", cardvalue: "alpaca.png", expectedcardvalues: ["alpaca.png"]}, |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
var uniqifynames = [ |
106
|
|
|
{nicknameone: "Magnus", nicknametwo: "Magnus", nicknamethree: "", expected: "Magnus2"}, |
107
|
|
|
{nicknameone: "Magnus", nicknametwo: "Magnus", nicknamethree: "Magnus2", expected: "Magnus3"}, |
108
|
|
|
{nicknameone: "Magnus", nicknametwo: "Roger", nicknamethree: "", expected: "Roger"} |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
var setactive = [ |
112
|
|
|
{players: ["a", "b", "c"]} |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
var setcolor = [ |
116
|
|
|
{players: ["a", "b", "c"]} |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
// var cardids = [ |
120
|
|
|
// {value: 0, expected: [-1, -1]}, |
121
|
|
|
// {value: 300, expected: [-1, -1]}, |
122
|
|
|
// {value: 1, expected: [0, 1]}, |
123
|
|
|
// {value: 2, expected: [2, 3]}, |
124
|
|
|
// {value: 3, expected: [4, 5]} |
125
|
|
|
// ]; |
126
|
|
|
|
127
|
|
|
cardvalues.forEach(function(test) { |
128
|
|
|
describe("Set card values in gamebrain", function() { |
129
|
|
|
it("Checking values", function () { |
130
|
|
|
checkSetCardValue(test.player, test.colorclass, test.cardvalue, test.expectedcardvalues); |
131
|
|
|
}); |
132
|
|
|
}); |
133
|
|
|
}); |
134
|
|
|
|
135
|
|
|
uniqifynames.forEach(function(test) { |
136
|
|
|
describe("Check uniqifying names " + test.nicknameone +" with " + test.nicknametwo, function() { |
137
|
|
|
it("Second nickname should be " + test.expected, function () { |
138
|
|
|
checkUniqifining(test.nicknameone, test.nicknametwo, test.nicknamethree, test.expected); |
139
|
|
|
}); |
140
|
|
|
}); |
141
|
|
|
}); |
142
|
|
|
|
143
|
|
|
setactive.forEach(function(test) { |
144
|
|
|
describe("Check set active player", function() { |
145
|
|
|
it("Checking next player", function () { |
146
|
|
|
checkSetActivePlayer(test.players); |
147
|
|
|
}); |
148
|
|
|
}); |
149
|
|
|
}); |
150
|
|
|
|
151
|
|
|
setcolor.forEach(function(test) { |
152
|
|
|
describe("Check set color for player", function() { |
153
|
|
|
it("Checking color", function () { |
154
|
|
|
checkSetPlayerColor(test.players); |
155
|
|
|
}); |
156
|
|
|
}); |
157
|
|
|
}); |
158
|
|
|
}); |
159
|
|
|
|