Comparing version 0.9.15 to 0.9.16
@@ -23,3 +23,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var color = require('ansi-color').set; | ||
var hex = require('hexer'); | ||
@@ -29,2 +28,3 @@ var TypedError = require('error/typed'); | ||
var Result = require('./result'); | ||
var errorHighlighter = require('./error_highlighter'); | ||
@@ -159,44 +159,10 @@ var ShortReadError = TypedError({ | ||
options = options || {}; | ||
var markColor = options.markColor || 'red+bold'; | ||
var errColor = typeof markColor === 'function' ? markColor : function(str) { | ||
return color(str, markColor); | ||
}; | ||
var hasOffset = !(err.offset === undefined || err.offset === null); | ||
var hasEnd = !(err.endOffset === undefined || err.endOffset === null); | ||
var within = false; | ||
var opts = options.hexerOptions ? Object.create(options.hexerOptions) : {}; | ||
if (hasOffset) { | ||
if (hasEnd) { | ||
opts.decorateHexen = decorateRangedError; | ||
opts.decorateHuman = decorateRangedError; | ||
} else { | ||
opts.decorateHexen = decorateError; | ||
opts.decorateHuman = decorateError; | ||
} | ||
if (opts.colored === undefined) { | ||
opts.colored = true; | ||
} | ||
var highlight = errorHighlighter(err, options); | ||
opts.decorateHexen = highlight; | ||
opts.decorateHuman = highlight; | ||
return hex(err.buffer, opts); | ||
function decorateRangedError(totalOffset, screenOffset, str) { | ||
if (totalOffset === err.offset) { | ||
within = totalOffset !== err.endOffset-1; | ||
return errColor(str); | ||
} else if (totalOffset === err.endOffset-1) { | ||
within = false; | ||
return errColor(str); | ||
} else if (within) { | ||
return errColor(str); | ||
} else { | ||
return str; | ||
} | ||
} | ||
function decorateError(totalOffset, screenOffset, str) { | ||
if (totalOffset === err.offset) { | ||
return errColor(str); | ||
} else { | ||
return str; | ||
} | ||
} | ||
} | ||
@@ -203,0 +169,0 @@ |
{ | ||
"name": "bufrw", | ||
"version": "0.9.15", | ||
"version": "0.9.16", | ||
"description": "Buffer Reading and Writing", | ||
@@ -22,4 +22,5 @@ "keywords": [], | ||
"error": "^5.1.1", | ||
"hexer": "^1.3.5", | ||
"readable-stream": "1.0.33" | ||
"hexer": "^1.4.2", | ||
"readable-stream": "1.0.33", | ||
"xtend": "^4.0.0" | ||
}, | ||
@@ -26,0 +27,0 @@ "devDependencies": { |
121
test_rw.js
@@ -23,8 +23,9 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var color = require('ansi-color').set; | ||
var hex = require('hexer'); | ||
var hexdiff = require('hexer/diff'); | ||
var util = require('util'); | ||
var intoBufferResult = require('./interface').intoBufferResult; | ||
var fromBufferResult = require('./interface').fromBufferResult; | ||
var formatError = require('./interface').formatError; | ||
var AnnotatedBuffer = require('./annotated_buffer'); | ||
var errorHighlighter = require('./error_highlighter'); | ||
@@ -35,5 +36,2 @@ module.exports.cases = testCases; | ||
var self = function runTestCases(assert, done) { | ||
self = Object.create(self); | ||
self.assert = assert; | ||
self.rw = rw; | ||
for (var i = 0; i < cases.length; i++) { | ||
@@ -63,15 +61,6 @@ var testCase; | ||
} | ||
if (testCase.lengthTest) lengthTest(self, testCase.lengthTest); | ||
if (testCase.writeTest) writeTest(self, testCase.writeTest); | ||
if (testCase.readTest) readTest(self, testCase.readTest); | ||
new RWTestCase(assert, rw, testCase).run(); | ||
} | ||
(done || assert.end)(); | ||
}; | ||
self.hexdumpStream = process.stdout; | ||
self.hexdump = function hexdumpBuffer(desc, err) { | ||
self.hexdumpStream.write(util.format('%s: %s', | ||
desc, formatError(err, {color: true}))); | ||
}; | ||
self.assert = null; | ||
@@ -82,3 +71,19 @@ self.rw = null; | ||
function lengthTest(self, testCase) { | ||
function RWTestCase(assert, rw, testCase) { | ||
var self = this; | ||
self.assert = assert; | ||
self.rw = rw; | ||
self.testCase = testCase; | ||
} | ||
RWTestCase.prototype.run = function run() { | ||
var self = this; | ||
if (self.testCase.lengthTest) self.runLengthTest(); | ||
if (self.testCase.writeTest) self.runWriteTest(); | ||
if (self.testCase.readTest) self.runReadTest(); | ||
}; | ||
RWTestCase.prototype.runLengthTest = function runLengthTest() { | ||
var self = this; | ||
var testCase = self.testCase.lengthTest; | ||
var val = testCase.value; | ||
@@ -99,5 +104,7 @@ var res = self.rw.byteLength(val); | ||
} | ||
} | ||
}; | ||
function writeTest(self, testCase) { | ||
RWTestCase.prototype.runWriteTest = function runWriteTest() { | ||
var self = this; | ||
var testCase = self.testCase.writeTest; | ||
var val = testCase.value; | ||
@@ -114,4 +121,5 @@ var got = Buffer(testCase.bytes ? testCase.bytes.length : testCase.length || 0); | ||
} else { | ||
self.hexdump('write error', err); | ||
self.assert.ifError(err, 'no write error'); | ||
// istanbul ignore else | ||
if (err) self.dumpError('write', err); | ||
} | ||
@@ -125,3 +133,4 @@ } else if (testCase.error) { | ||
if (got.toString('hex') !== buf.toString('hex')) { | ||
self.assert.comment('expected v actual:\n' + hexdiff(buf, got)); | ||
self.assert.comment('expected v actual:\n' + | ||
hexdiff(buf, got, {colored: true})); | ||
self.assert.fail(desc); | ||
@@ -132,37 +141,9 @@ } else { | ||
} | ||
}; | ||
} | ||
// istanbul ignore next | ||
function hexdiff(a, b) { | ||
var adump = hex(a, { | ||
emptyHuman: ' ', | ||
decorateHexen: highlightA | ||
}).split('\n'); | ||
var bdump = hex(b, { | ||
emptyHuman: ' ', | ||
decorateHexen: highlightB | ||
}).split('\n'); | ||
return adump.map(function each(aline, i) { | ||
var bline = bdump[i]; | ||
var sep = aline === bline ? ' ' : color('|', 'blue+bold'); | ||
return aline + ' ' + sep + ' ' + bline; | ||
}).join('\n'); | ||
function highlightA(i, j, str) { | ||
if (a[i] !== b[i]) { | ||
str = color(str, 'red+bold'); | ||
} | ||
return str; | ||
} | ||
function highlightB(i, j, str) { | ||
if (a[i] !== b[i]) { | ||
str = color(str, 'green+bold'); | ||
} | ||
return str; | ||
} | ||
} | ||
function readTest(self, testCase) { | ||
RWTestCase.prototype.runReadTest = function runReadTest() { | ||
var self = this; | ||
var testCase = self.testCase.readTest; | ||
var buffer = Buffer(testCase.bytes); | ||
buffer = new AnnotatedBuffer(buffer); | ||
var res = fromBufferResult(self.rw, buffer); | ||
@@ -177,7 +158,5 @@ var err = res.error; | ||
} else { | ||
self.assert.ifError(err, 'no read error'); | ||
// istanbul ignore else | ||
if (!got && err.buffer) got = err.buffer; | ||
// istanbul ignore else | ||
if (Buffer.isBuffer(got)) self.hexdump('read error', err); | ||
self.assert.ifError(err, 'no read error'); | ||
if (err) self.dumpError('read', err); | ||
} | ||
@@ -196,4 +175,30 @@ } else if (testCase.error) { | ||
} | ||
} | ||
}; | ||
RWTestCase.prototype.dumpError = function dumpError(kind, err) { | ||
var self = this; | ||
var highlight = errorHighlighter(err); | ||
// istanbul ignore next | ||
var errName = err.name || err.constructor.name; | ||
var dump = util.format('%s error %s: %s\n', | ||
kind, errName, err.message); | ||
if (err.buffer && err.buffer.hexdump) { | ||
dump += err.buffer.hexdump({ | ||
colored: true, | ||
boldStart: false, | ||
highlight: highlight | ||
}); | ||
// istanbul ignore else | ||
} else if (err.buffer) { | ||
dump += hex(err.buffer, { | ||
colored: true, | ||
decorateHexen: highlight, | ||
decorateHuman: highlight | ||
}); | ||
} | ||
dump.split(/\n/).forEach(function each(line) { | ||
self.assert.comment(line); | ||
}); | ||
}; | ||
function copyErr(err, tmpl) { | ||
@@ -200,0 +205,0 @@ var out = {}; |
@@ -25,3 +25,2 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
var TypedError = require('error/typed'); | ||
var PassThrough = require('readable-stream').PassThrough; | ||
var test = require('tape'); | ||
@@ -73,15 +72,21 @@ | ||
]); | ||
rwTest.hexdumpStream = PassThrough({encoding: 'utf8'}); | ||
runMockedTest(rwTest, function done(results) { | ||
assert.equal(results[0].name, 'no length error', 'expected "no length error"'); | ||
assert.equal(results[0].actual.message, 'boom', 'expected actual "boom" error'); | ||
assert.equal(results[1].name, 'no write error', 'expected "no write error"'); | ||
assert.equal(results[1].actual.message, 'bang', 'expected actual "bang" error'); | ||
assert.equal(results[2].name, 'no read error', 'expected "no read error"'); | ||
assert.equal(results[2].actual.message, 'bork', 'expected actual "bork" error'); | ||
assert.equal(rwTest.hexdumpStream.read(), | ||
'write error: BangError: bang\n' + | ||
'00: \x1b[31m\x1b[1m00\x1b[0m \x1b[31m\x1b[1m.\x1b[0m\n' + | ||
'read error: Error: bork\n' + | ||
'00: \x1b[31m\x1b[1m00\x1b[0m \x1b[31m\x1b[1m.\x1b[0m\n'); | ||
assert.equal(results[2], | ||
'write error BangError: bang'); | ||
assert.equal(results[3], | ||
'00\x1b[36m:\x1b[0m \x1b[31m\x1b[1m00\x1b[0m \x1b[31m\x1b[1m.\x1b[0m'); | ||
assert.equal(results[4].name, 'no read error', 'expected "no read error"'); | ||
assert.equal(results[4].actual.message, 'bork', 'expected actual "bork" error'); | ||
assert.equal(results[5], | ||
'read error Error: bork'); | ||
assert.equal(results[6], | ||
'00\x1b[36m:\x1b[0m 00 \x1b[30m\x1b[1m.\x1b[0m'); | ||
assert.end(); | ||
@@ -109,3 +114,2 @@ }); | ||
]); | ||
rwTest.hexdumpStream = PassThrough({encoding: 'utf8'}); | ||
runMockedTest(rwTest, function done(results) { | ||
@@ -115,3 +119,2 @@ assert.equal(results[0].name, 'expected length error', 'expected "expected length error"'); | ||
assert.equal(results[2].name, 'expected read error', 'expected "expected read error"'); | ||
assert.equal(rwTest.hexdumpStream.read(), null); | ||
assert.end(); | ||
@@ -118,0 +121,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
156094
49
3896
5
+ Addedxtend@^4.0.0
Updatedhexer@^1.4.2