| 1 |
'use strict'; |
| 2 |
|
| 3 |
const LEVEL = Symbol.for('level'); |
| 4 |
|
| 5 |
// |
| 6 |
// Returns a function which logs a specified amount |
| 7 |
// of times before calling the provided callback |
| 8 |
// |
| 9 |
exports.logFor = function logFor(count, done) { |
| 10 |
const infos = []; |
| 11 |
return function log(info, next) { |
| 12 |
infos.push(info); |
| 13 |
if (next) { next(); } |
| 14 |
if (!--count) { return done && done(null, infos); } |
| 15 |
if (count < 0) { |
| 16 |
throw new Error('Invoked more log messages than requested'); |
| 17 |
} |
| 18 |
}; |
| 19 |
}; |
| 20 |
|
| 21 |
// |
| 22 |
// Helper function that creates `opts.count` infos repeating over the |
| 23 |
// `opts.levels` in order. |
| 24 |
// |
| 25 |
exports.infosFor = function infosFor(opts) { |
| 26 |
const { count, levels } = opts; |
| 27 |
const infos = []; |
| 28 |
|
| 29 |
for (var i = 0; i < count; i++) { |
| 30 |
infos.push.apply(infos, levels.map(function (level) { |
| 31 |
return { |
| 32 |
[LEVEL]: level, |
| 33 |
message: `Testing message for level: ${level}`, |
| 34 |
index: i, |
| 35 |
level |
| 36 |
}; |
| 37 |
})); |
| 38 |
} |
| 39 |
|
| 40 |
return infos; |
| 41 |
}; |
| 42 |
|
| 43 |
// |
| 44 |
// Helper function for generating a set of messages |
| 45 |
// one per level. |
| 46 |
// |
| 47 |
exports.levelAndMessage = function levelAndMessage (level) { |
| 48 |
return { |
| 49 |
[LEVEL]: level, |
| 50 |
message: `Testing message for level: ${level}`, |
| 51 |
level |
| 52 |
}; |
| 53 |
}; |
| 54 |
|
| 55 |
// |
| 56 |
// Adds the { exception: true } to the info created |
| 57 |
// (or passed in) with the target `level`. |
| 58 |
// |
| 59 |
exports.toException = function toException(level) { |
| 60 |
const info = typeof level === 'string' |
| 61 |
? exports.levelAndMessage(level) |
| 62 |
: level; |
| 63 |
|
| 64 |
info.exception = true; |
| 65 |
if (!info[LEVEL]) { |
| 66 |
info[LEVEL] = info.level; |
| 67 |
} |
| 68 |
|
| 69 |
return info; |
| 70 |
}; |
| 71 |
|
| 72 |
// |
| 73 |
// Transforms into a _mock_ internal Node.js WriteReq. |
| 74 |
// |
| 75 |
exports.toWriteReq = function (obj) { |
| 76 |
return { |
| 77 |
chunk: obj, |
| 78 |
enc: 'utf8', |
| 79 |
callback: function () {} |
| 80 |
}; |
| 81 |
}; |
| 82 |
|
| 83 |
// |
| 84 |
// Inspects two arrays |
| 85 |
// |
| 86 |
exports.inspectLoggedResults = function inspectLoggedResults(actual, expected) { |
| 87 |
const len = actual.length > expected.length |
| 88 |
? actual.length |
| 89 |
: expected.length; |
| 90 |
|
| 91 |
console.log(`Length: { actual: ${actual.length}, expected: ${expected.length}`); |
| 92 |
|
| 93 |
for (let i = 0; i < len; i++) { |
| 94 |
if (actual[i] || expected[i]) { |
| 95 |
console.log(`actual[${i}]: %j`, actual[i]); |
| 96 |
console.log(`expected[${i}]: %j`, expected[i]); |
| 97 |
} |
| 98 |
} |
| 99 |
}; |