forked from frank-deng/bulls-cows-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
93 lines (93 loc) · 2.25 KB
/
test.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
console.log();
var bulls_cows_solver = require('./bulls-cows-solver.js');
QUnit.test("Test", function(assert) {
assert.deepEqual(bulls_cows_solver([
{guess:'1234', result:'1A3B'},
]),
[ '1342', '1423', '2314', '2431', '3124', '3241', '4132', '4213' ]
);
assert.strictEqual(bulls_cows_solver([]), undefined);
});
QUnit.test("Invalid Input Detection", function(assert) {
assert.throws(function(){bulls_cows_solver();}, /Invalid input./, "Detect invalid input.");
assert.throws(function(){bulls_cows_solver("1324");}, /Invalid input./, "Detect invalid input.");
assert.throws(function(){bulls_cows_solver(23432);}, /Invalid input./, "Detect invalid input.");
assert.throws(
function(){
bulls_cows_solver([
{guess:'1234', result:'1A3B'},
{guess:'32a9',},
{guess:'3234', result:'1A3B'},
]);
},
/Input 2 is invalid./,
"Detect abnormal record."
);
assert.throws(
function(){
bulls_cows_solver([
{guess:'1234', result:'1A3B'},
{guess:'32a9', result:'0A0B'},
{guess:'3234', result:'1A3B'},
]);
},
/Input 2 is invalid./,
"Detect non-number input."
);
assert.throws(
function(){
bulls_cows_solver([
{guess:'1234', result:'1A3B'},
{guess:'678', result:'0A0B'},
{guess:'3234', result:'1A3B'},
]);
},
/Input 2 is invalid./,
"Detect number shorter than 4."
);
assert.throws(
function(){
bulls_cows_solver([
{guess:'1234', result:'1A3B'},
{guess:'67890', result:'0A0B'},
{guess:'3234', result:'1A3B'},
]);
},
/Input 2 is invalid./,
"Detect number longer than 4."
);
assert.throws(
function(){
bulls_cows_solver([
{guess:'1234', result:'1A3B'},
{guess:'5678', result:'0A0B'},
{guess:'3234', result:'1A3B'},
{guess:'1234', result:'1A3B'},
]);
},
/Input 3 is invalid./,
"Number with duplicated digit."
);
assert.throws(
function(){
bulls_cows_solver([
{guess:'1234', result:'1A3B'},
{guess:'1234', result:'1A4B'},
{guess:'1234', result:'2A1B'},
]);
},
/Input 2 is invalid./,
"Invalid guess result."
);
assert.throws(
function(){
bulls_cows_solver([
{guess:'1234', result:'1A3B'},
{guess:'4234', result:'1A4B'},
{guess:'1234', result:'2A1B'},
]);
},
/Input 2 is invalid./,
"Invalid number and guess result."
);
});