forked from urfu-2016/javascript-task-8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow.spec.js
106 lines (84 loc) · 3.14 KB
/
flow.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
/* eslint-env mocha */
/* eslint-disable no-shadow */
'use strict';
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var flow = require('./flow');
var directory = './data/';
describe('flow', function () {
it('должен правильно посчитать суммарную стоимость', function (done) {
var jsonParseAsync = flow.makeAsync(JSON.parse);
flow.serial([
function (next) {
fs.readdir(directory, next);
},
flow.makeAsync(function (files) {
return files.map(function (dir) {
return path.join(directory, dir);
});
}),
function (files, next) {
flow.filter(files, function (file, next) {
fs.stat(file, function (err, stat) {
if (err) {
return next(err);
}
// Первый аргумент соответствует ошибке
next(null, stat.size > 0);
});
}, next);
},
function (files, next) {
flow.map(files, fs.readFile, next);
},
function (files, next) {
flow.map(files, jsonParseAsync, next);
}
], function (error, contents) {
assert.ifError(error);
var total = contents.reduce(function (sum, content) {
return sum + content.price;
}, 0);
assert.strictEqual(total, 111000);
done();
});
});
if (flow.isStar) {
it('должен правильно посчитать суммарную стоимость [*]', function (done) {
var jsonParseAsync = flow.makeAsync(JSON.parse);
flow.serial([
function (next) {
fs.readdir(directory, next);
},
flow.makeAsync(function (files) {
return files.map(function (dir) {
return path.join(directory, dir);
});
}),
function (files, next) {
flow.filterLimit(files, 2, function (file, next) {
fs.stat(file, function (err, stat) {
next(err, stat && stat.size > 0);
});
}, next);
},
function (files, next) {
flow.mapLimit(files, 2, fs.readFile, next);
},
function (files, next) {
flow.map(files, jsonParseAsync, next);
},
flow.makeAsync(function (contents) {
return contents.reduce(function (sum, content) {
return sum + content.price;
}, 0);
})
], function (error, total) {
assert.ifError(error);
assert.strictEqual(total, 111000);
done();
});
});
}
});