/
valid.spec.js
154 lines (140 loc) · 4.36 KB
/
valid.spec.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
'use strict';
var fs = require('fs');
var expect = require('expect.js');
var glob = require('glob');
var path = require('path');
var imageSize = require('..');
var detector = require('../lib/detector');
var sizes = {
'default': {
'width': 123,
'height': 456
},
'specs/images/valid/cur/sample.cur': {
'width': 32, 'height': 32
},
'specs/images/valid/ico/sample.ico': {
'width': 32, 'height': 32
},
'specs/images/valid/ico/sample-compressed.ico': {
'width': 32, 'height': 32
},
'specs/images/valid/ico/sample-256.ico': {
'width': 256, 'height': 256
},
'specs/images/valid/ico/sample-256-compressed.ico': {
'width': 256, 'height': 256
},
'specs/images/valid/icns/sample.icns': {
'width': 16,
'height': 16,
'images': [
{ 'width': 16, 'height': 16, 'type': 'is32' },
{ 'width': 16, 'height': 16, 'type': 's8mk' },
{ 'width': 32, 'height': 32, 'type': 'il32' },
{ 'width': 32, 'height': 32, 'type': 'l8mk' },
{ 'width': 48, 'height': 48, 'type': 'ih32' },
{ 'width': 48, 'height': 48, 'type': 'h8mk' },
{ 'width': 128, 'height': 128, 'type': 'it32' },
{ 'width': 128, 'height': 128, 'type': 't8mk' }
],
'type': 'icns'
},
'specs/images/valid/ico/multi-size.ico': {
'width': 256,
'height': 256,
'images': [
{'width': 256, 'height': 256},
{'width': 128, 'height': 128},
{'width': 96, 'height': 96},
{'width': 72, 'height': 72},
{'width': 64, 'height': 64},
{'width': 48, 'height': 48},
{'width': 32, 'height': 32},
{'width': 24, 'height': 24},
{'width': 16, 'height': 16}
]
},
'specs/images/valid/ico/multi-size-compressed.ico': {
'width': 256,
'height': 256,
'images': [
{'width': 256, 'height': 256},
{'width': 128, 'height': 128},
{'width': 96, 'height': 96},
{'width': 72, 'height': 72},
{'width': 64, 'height': 64},
{'width': 48, 'height': 48},
{'width': 32, 'height': 32},
{'width': 24, 'height': 24},
{'width': 16, 'height': 16}
]
},
'specs/images/valid/jpg/large.jpg': {
'width': 1600,
'height': 1200
},
'specs/images/valid/jpg/very-large.jpg': {
'width': 4800,
'height': 3600
},
'specs/images/valid/png/sample_fried.png': {
'width': 128,
'height': 68
}
};
// Test all valid files
describe('Valid images', function () {
var validFiles = glob
.sync('specs/images/valid/**/*.*')
.filter(function (file) {
return path.extname(file) !== '.md';
});
validFiles.forEach(function (file) {
describe(file, function() {
var type, bufferDimensions, asyncDimensions;
var bufferSize = 8192;
beforeEach(function (done) {
var buffer = Buffer.alloc(bufferSize);
var filepath = path.resolve(file);
var descriptor = fs.openSync(filepath, 'r');
fs.readSync(descriptor, buffer, 0, bufferSize, 0);
type = detector(buffer);
// tiff cannot support buffers, unless the buffer contains the entire file
if (type !== 'tiff') {
bufferDimensions = imageSize(buffer);
}
imageSize(file, function (err, _dim) {
asyncDimensions = _dim;
done();
});
});
it('should return correct size for ' + file, function() {
var expected = sizes[file] || sizes.default;
expect(asyncDimensions.width).to.be(expected.width);
expect(asyncDimensions.height).to.be(expected.height);
if (asyncDimensions.images) {
asyncDimensions.images.forEach(function (item, index) {
var expectedItem = expected.images[index];
expect(item.width).to.be(expectedItem.width);
expect(item.height).to.be(expectedItem.height);
if (expectedItem.type) {
expect(item.type).to.be(expectedItem.type);
}
});
}
if (type !== 'tiff') {
expect(bufferDimensions.width).to.be(expected.width);
expect(bufferDimensions.height).to.be(expected.height);
if (bufferDimensions.images) {
bufferDimensions.images.forEach(function (item, index) {
var expectedItem = expected.images[index];
expect(item.width).to.be(expectedItem.width);
expect(item.height).to.be(expectedItem.height);
});
}
}
});
});
});
});