-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest_switch.js
84 lines (68 loc) · 1.19 KB
/
test_switch.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
// test_switch.js
// --------------
var assert = console.assert;
var test = function(name, f) {
f();
};
var counter = 0;
test('matching case', function() {
var x = 42;
switch (x) {
case 12:
assert(false);
break;
case 42:
counter++;
break;
default:
assert(false);
break;
}
assert(counter === 1);
});
test('default case', function() {
var y = 'not handled';
switch (y) {
case 'a':
assert(false);
break;
case 'b':
assert(false);
break;
case 'c':
assert(false);
break;
default:
counter++;
break;
}
assert(counter === 2);
});
test('fall-through cases and missing breaks', function() {
var z = 'cat';
switch (z) {
case 'cat':
case 'dog':
counter++;
case 'lizard':
counter++;
}
assert(counter === 4);
});
test('cases that follow default are checked first', function() {
var z = 'cat';
switch (z) {
case 'dog':
assert(false);
break;
case 'mouse':
assert(false);
break;
default:
assert(false); // This should NOT be executed
break;
case 'cat':
counter++;
}
assert(counter === 5);
});