-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
126 lines (90 loc) · 1.88 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
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
'use strict';
var test = require('tape');
var HealthPlugin = require('./');
function FakeGame() { }
test('hurt', function(t) {
var p = HealthPlugin(new FakeGame(), {});
t.equals(p.value, 10);
p.hurt(1);
t.equal(p.value, 9);
p.hurt(6);
t.equal(p.value, 3);
p.hurt(1);
t.equal(p.value, 2);
p.hurt(1);
t.equal(p.value, 1);
p.hurt(1);
t.equal(p.value, 0);
t.end();
});
test('die', function(t) {
var p = HealthPlugin(new FakeGame(), {});
var self = this;
p.on('die', function() {
self.died = true;
});
p.hurt(10);
t.equal(self.died, true);
t.end();
});
test('clamped hurt', function(t) {
var p = HealthPlugin(new FakeGame(), {});
p.hurt(9999);
t.equal(p.value, 0); // not negative
t.end();
});
test('clamped heal', function(t) {
var p = HealthPlugin(new FakeGame(), {});
p.heal(9999);
t.equal(p.value, 10);
t.end();
});
test('max health', function(t) {
var p = HealthPlugin(new FakeGame(), {maxHealth: 20});
t.equal(p.value, p.maxHealth);
t.equal(p.value, 20);
p.hurt(9);
t.equal(p.value, 11);
p.heal(9);
t.equal(p.value, 20);
p.hurt(9);
t.equal(p.value, 11);
p.heal(10);
t.equal(p.value, 20);
t.end();
});
test('bad health', function(t) {
var p = HealthPlugin(new FakeGame(), {maxHealth: 20});
t.throws(function() {
p.heal(undefined);
});
t.throws(function() {
p.hurt(undefined);
});
t.throws(function() {
p.heal('abc');
});
t.throws(function() {
p.hurt('abc');
});
t.throws(function() {
p.heal(NaN);
});
t.throws(function() {
p.hurt(NaN);
});
t.end();
});
test('infinite health', function(t) {
var p = HealthPlugin(new FakeGame(), {maxHealth: 20});
p.hurt(10);
p.heal(Infinity);
t.equal(p.value, 20);
p.heal(-Infinity);
t.equal(p.value, 0);
p.hurt(-Infinity);
t.equal(p.value, 20);
p.heal(-Infinity)
t.equal(p.value, 0);
t.end();
});