Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
239 lines (216 loc) · 5.31 KB
/
Copy pathtest.js
File metadata and controls
239 lines (216 loc) · 5.31 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
var assert = require('assert');
var Image = require("../index.js");
var imageDiff = require('image-diff');
var fs = require("fs");
var CreateImage = function() {
var buf = fs.createReadStream("test/image.png");
var image = new Image();
return image.setData(buf);
}
var TestManipulation = function(manipulation, resultFile, comparisonFile) {
return CreateImage().then(function(image){
return manipulation(image);
}).then(function(image){
assert(typeof image != 'undefined');
return image.data()
}).then(function(data){
assert(typeof data != 'undefined');
fs.writeFileSync(resultFile, data);
return new Promise((resolve, reject) => {
imageDiff({
actualImage: resultFile,
expectedImage: comparisonFile
}, function(err, res){
assert(res == true);
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
});
}
function pad() {
return TestManipulation(function(image){
return image.pad({
left: 5,
top: 10,
right: 20,
bottom: 40,
color: "#F00"
})
},"./result-pad.png", "./test/result-pad.png");
}
function padTo(width, height) {
return TestManipulation(function(image){
return image.pad({
width: width,
height: height,
color: "#F00"
})
},"./result-pad-"+width+"-"+height+".png", "./test/result-pad-"+width+"-"+height+".png");
}
function crop() {
return TestManipulation(function(image){
return image.crop({
left: 5,
top: 10,
right: 20,
bottom: 40
})
},"./result-crop.png", "./test/result-crop.png");
}
function cropTo(width, height) {
return TestManipulation(function(image){
return image.crop({
width: width,
height: height
})
},"./result-crop-"+width+"-"+height+".png", "./test/result-crop-"+width+"-"+height+".png");
}
function resize() {
return TestManipulation(function(image){
return image.resize({
width: 200,
height: 200,
ignoreAspectRatio: true
})
},"./result-resize.png", "./test/result-resize.png");
}
function quality(quality) {
return TestManipulation(function(image) {
return image.quality(quality);
},"./result-quality.png", "./test/result-quality.png");
}
function scale() {
return TestManipulation(function(image){
return image.scale({
ratio: 0.5
})
},"./result-scale.png", "./test/result-scale.png");
}
function scaleWith(width, height) {
return TestManipulation(function(image){
return image.scale({
width: width,
height: height
})
},"./result-scale-"+width+"-"+height+".png", "./test/result-scale-"+width+"-"+height+".png");
}
describe('Image', function() {
describe('#pad()', function() {
it('should pad the image without error', function(done) {
pad().then(function(){
done();
}).catch(function(err){
assert(err == null);
done();
})
});
});
describe('#pad()', function() {
it('should pad the image without error', function(done) {
padTo(400, 500).then(function(){
done();
}).catch(function(err){
assert(err == null);
done();
})
});
});
describe('#crop()', function() {
it('should crop the image without error', function(done) {
crop().then(function(){
done();
}).catch(function(err){
assert(err == null);
done();
})
});
});
describe('#crop()', function() {
it('should crop the image without error', function(done) {
cropTo(100, 200).then(function(){
done();
}).catch(function(err){
assert(err == null);
done();
})
});
});
describe('#quality()', function() {
it('should change quality without error', function(done) {
quality(50).then(function() {
done();
}).catch(function(err) {
assert(err == null);
done();
});
});
});
describe('#scale()', function() {
it('should scale the image at a ratio', function(done) {
scale().then(function(){
done();
}).catch(function(err){
assert(err == null);
done();
})
});
});
describe('#scale()', function() {
it('should scale the image at a width and height', function(done) {
scaleWith(200, 100).then(function(){
done();
}).catch(function(err){
assert(err == null);
done();
})
});
});
describe('#resize()', function() {
it('should resize the image to correct width and height', function(done) {
resize().then(function(){
done();
}).catch(function(err){
assert(err == null);
done();
})
});
});
describe('#setData()', function() {
it('should fail setting wrong data', function(done) {
var image = new Image();
var buf = Buffer.from("abc", 'utf-8');
var errHit = 0;
image.setData(buf, {
sucess: function(){
errHit++;
},
error: function(err){
errHit++;
}
}).then(function(){
assert(false, "Should not succeed");
done();
}, function(err){
assert.equal(1, err.code);
assert.equal(1, errHit);
done();
})
});
});
describe('#setFormat()', function() {
it('should set the format to JPEG', function(done) {
CreateImage().then(function(image){
return image.setFormat("jpeg");
}).then(function(image){
return image.format();
}).then(function(format){
assert.equal("JPEG", format);
done();
})
});
});
});