-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
165 lines (127 loc) · 4.38 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
const test = require('tape');
const Inventory = require('./');
const ItemPile = require('itempile');
const tabsToCommas = (s) => {
return s.replace(/\t/g, ',');
};
test('size', (t) => {
const a = new Inventory();
t.equal(a.size(), 10);
t.equal(a.width, 10);
t.equal(a.height, 1);
const b = new Inventory(5);
t.equal(b.size(), 5);
t.equal(b.width, 5);
t.equal(b.height, 1);
const c = new Inventory(3, 2);
t.equal(c.size(), 3 * 2);
t.equal(c.width, 3);
t.equal(c.height, 2);
t.throws(() => new Inventory(1, 0));
t.throws(() => new Inventory(0, 1));
t.throws(() => new Inventory(0, 0));
t.throws(() => new Inventory(1, -1));
t.throws(() => new Inventory(-1, 1));
t.throws(() => new Inventory(-1, -1));
t.end();
});
test('give', (t) => {
const inv = new Inventory();
const expectedInvs = [
'42:dirt,,,,,,,,,',
'64:dirt,20:dirt,,,,,,,,',
'64:dirt,62:dirt,,,,,,,,',
'64:dirt,64:dirt,40:dirt,,,,,,,',
'64:dirt,64:dirt,64:dirt,18:dirt,,,,,,',
'64:dirt,64:dirt,64:dirt,60:dirt,,,,,,',
'64:dirt,64:dirt,64:dirt,64:dirt,38:dirt,,,,,',
'64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,16:dirt,,,,',
'64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,58:dirt,,,,',
'64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,36:dirt,,,',
'64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,14:dirt,,',
'64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,56:dirt,,',
'64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,34:dirt,',
'64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,12:dirt',
'64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,54:dirt',
'64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt',
'64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt,64:dirt']; // all filled up!
for (let i = 0; i < 16; ++i) {
//console.log "\n\n1. #{i}"
const excess = inv.give(new ItemPile('dirt', 42));
//console.log 'excess',excess
//console.log inv+''
t.equal(tabsToCommas(inv+''), expectedInvs[i]);
if (i === 15) {
t.equal(excess, 32); // partially added
}
if (i === 16) {
t.equal(excess, 42); // couldn't fit anything
}
}
t.end();
});
test('give large', (t) => {
const inv = new Inventory();
inv.give(new ItemPile('dirt', 200));
console.log(inv+'');
t.equal(tabsToCommas(inv+''), '64:dirt,64:dirt,64:dirt,8:dirt,,,,,,');
t.end();
});
test('give fill partial', (t) => {
const inv = new Inventory();
inv.array[1] = new ItemPile('dirt', 9);
t.equal(tabsToCommas(inv+''), ',9:dirt,,,,,,,,');
inv.give(new ItemPile('dirt', 1));
t.equal(tabsToCommas(inv+''), ',10:dirt,,,,,,,,');
t.end();
});
test('give infinite', (t) => {
const inv = new Inventory();
inv.give(new ItemPile('love', Infinity));
t.equal(tabsToCommas(inv+''), 'Infinity:love,,,,,,,,,');
t.end();
});
test('take', (t) => {
const inv = new Inventory();
inv.give(new ItemPile('dirt', 200));
inv.take(new ItemPile('dirt', 1));
t.equal(tabsToCommas(inv+''), '63:dirt,64:dirt,64:dirt,8:dirt,,,,,,');
inv.take(new ItemPile('dirt', 100));
console.log(inv+'');
t.equal(tabsToCommas(inv+''), ',27:dirt,64:dirt,8:dirt,,,,,,');
t.end();
});
test('clear', (t) => {
const inv = new Inventory();
inv.give(new ItemPile('dirt', 200));
inv.take(new ItemPile('dirt', 1));
t.equal(tabsToCommas(inv+''), '63:dirt,64:dirt,64:dirt,8:dirt,,,,,,');
inv.clear();
t.equal(tabsToCommas(inv+''), ',,,,,,,,,');
t.end();
});
test('transferTo', (t) => {
const inv = new Inventory();
inv.give(new ItemPile('dirt', 200));
inv.take(new ItemPile('dirt', 1));
t.equal(tabsToCommas(inv+''), '63:dirt,64:dirt,64:dirt,8:dirt,,,,,,');
const inv2 = new Inventory();
inv.transferTo(inv2);
t.equal(tabsToCommas(inv+''), ',,,,,,,,,');
t.equal(tabsToCommas(inv2+''), '63:dirt,64:dirt,64:dirt,8:dirt,,,,,,');
inv.give(new ItemPile('diamond', 1));
inv2.give(new ItemPile('gold', 1));
t.equal(tabsToCommas(inv+''), '1:diamond,,,,,,,,,');
t.equal(tabsToCommas(inv2+''), '63:dirt,64:dirt,64:dirt,8:dirt,1:gold,,,,,');
t.end();
});
test('fromString', (t) => {
const inv = Inventory.fromString('\t10:dirt\t20:grass');
console.log(inv+'');
t.equals(inv.size(), 3);
t.equals(inv.get(0), undefined);
t.equals(inv.get(1)+'', '10:dirt');
t.equals(inv.get(2)+'', '20:grass');
t.end();
});