-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest.js
172 lines (144 loc) · 4.45 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
165
166
167
168
169
170
171
172
var assert = require('assert'),
BinaryFormat = require('./');
describe('Tiny Binary Format', function() {
var Tile;
beforeEach(function() {
Tile = new BinaryFormat([
{ length: 8, name: 'type' },
{ length: 8, name: 'height' },
{ length: 1, name: 'veg' }
]);
});
it('should be a function', function() {
assert.equal(typeof BinaryFormat, 'function');
});
it('should return an object', function() {
assert.equal(typeof Tile, 'object');
});
it('the object should have the correct prototype', function() {
assert.equal(Tile instanceof BinaryFormat, true);
});
it('should throw an error if we create a large field', function() {
assert.throws(function() {
new BinaryFormat([
{ length: 40, name: 'too_big' }
]);
}, Error);
});
describe('#pack', function() {
var testCases;
beforeEach(function() {
testCases = [
{ input: [14, 31, 1], expected: 7231 },
{ input: [0, 79, 0], expected: 158 },
{ input: [8, 60, 1], expected: 4217 },
{ input: [46, 18, 0], expected: 23588 },
{ input: [18, 32, 1], expected: 9281 }
];
});
it('should be a function', function() {
assert.equal(typeof Tile.pack, 'function');
});
it('should return a number', function() {
testCases.forEach(function(test) {
assert.equal(typeof Tile.pack.apply(Tile, test.input), 'number');
});
});
it('should return the correct numbers', function() {
testCases.forEach(function(test) {
assert.equal(Tile.pack.apply(Tile, test.input), test.expected);
});
});
});
describe('#unpack', function() {
var testCases;
beforeEach(function() {
testCases = [
{ input: [26, 2, 0] },
{ input: [125, 5, 1] },
{ input: [3, 99, 1] },
{ input: [88, 99, 1] }
];
});
it('should be a function', function() {
assert.equal(typeof Tile.unpack, 'function');
});
it('should return an object', function() {
testCases.forEach(function(test) {
var packed = Tile.pack.apply(Tile, test.input);
var unpacked = Tile.unpack(packed);
assert.equal(typeof unpacked, 'object');
assert.equal(unpacked instanceof Array, false);
});
});
it('should be reversible', function() {
testCases.forEach(function(test) {
var packed = Tile.pack.apply(Tile, test.input);
assert.deepEqual(Tile.unpack(packed), {
type: test.input[0],
height: test.input[1],
veg: test.input[2]
});
});
});
});
describe('#unpackArray', function() {
var testCases;
beforeEach(function() {
testCases = [
{ input: [53, 49, 1] },
{ input: [111, 31, 0] },
{ input: [9, 5, 1] },
{ input: [16, 9, 0] }
];
});
it('should be a function', function() {
assert.equal(typeof Tile.unpackArray, 'function');
});
it('should return an array', function() {
testCases.forEach(function(test) {
var packed = Tile.pack.apply(Tile, test.input);
var unpacked = Tile.unpackArray(packed);
assert.equal(unpacked instanceof Array, true);
});
});
it('should be reversible', function() {
testCases.forEach(function(test) {
var packed = Tile.pack.apply(Tile, test.input);
assert.deepEqual(Tile.unpackArray(packed), test.input);
});
});
});
describe('#unpackField', function() {
var testCases;
beforeEach(function() {
testCases = [
{ input: [26, 2, 0] },
{ input: [125, 5, 1] },
{ input: [3, 99, 1] },
{ input: [88, 99, 1] }
];
});
it('should be a function', function() {
assert.equal(typeof Tile.unpackField, 'function');
});
it('should return an integer', function() {
testCases.forEach(function(test) {
var packed = Tile.pack.apply(Tile, test.input);
Tile.fields.forEach(function(field) {
var unpackedField = Tile.unpackField(packed, field.name);
assert.equal(typeof unpackedField, 'number');
});
});
});
it('should be reversible', function() {
testCases.forEach(function(test) {
var packed = Tile.pack.apply(Tile, test.input);
Tile.fields.forEach(function(field, i) {
var unpackedField = Tile.unpackField(packed, field.name);
assert.equal(unpackedField, test.input[i]);
});
});
});
});
});