Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

power-assert-formatter

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

power-assert-formatter - npm Package Compare versions

Comparing version 0.6.1 to 0.7.0

test/types_and_values_test.js

52

bower.json
{
"name": "power-assert-formatter",
"version": "0.6.1",
"main": "lib/power-assert-formatter.js",
"ignore": [
"**/.*",
"Gruntfile.js",
"node_modules",
"package.json",
"coverage",
"test",
"test_helper.js"
],
"dependencies": {
"estraverse": "~1.5.0"
},
"devDependencies": {
"empower": "~0.6.0",
"espower": "~0.6.0",
"esprima": "~1.0.4",
"escodegen": "1.3.0",
"mocha": "~1.18.2",
"requirejs": "~2.1.11",
"assert": "Jxck/assert"
}
"name": "power-assert-formatter",
"version": "0.7.0",
"main": "lib/power-assert-formatter.js",
"ignore": [
"**/.*",
"Gruntfile.js",
"node_modules",
"package.json",
"coverage",
"test",
"test_helper.js"
],
"dependencies": {
"esprima": "~1.2.1",
"estraverse": "~1.5.0",
"google-diff-match-patch-js": "~1.0.0"
},
"devDependencies": {
"empower": "~0.7.1",
"espower": "~0.7.0",
"escodegen": "1.3.0",
"mocha": "~1.18.2",
"requirejs": "~2.1.11",
"assert": "Jxck/assert"
},
"resolutions": {
"esprima": "~1.2.1"
}
}

@@ -17,12 +17,13 @@ /**

'use strict';
/*jshint camelcase: false */
// using returnExports UMD pattern
if (typeof define === 'function' && define.amd) {
define(['estraverse'], factory);
define(['estraverse', 'esprima', 'diff_match_patch'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('estraverse'));
module.exports = factory(require('estraverse'), require('esprima'), require('googlediff'));
} else {
root.powerAssertFormatter = factory(root.estraverse);
root.powerAssertFormatter = factory(root.estraverse, root.esprima, root.diff_match_patch);
}
}(this, function (estraverse) {
}(this, function (estraverse, esprima, DiffMatchPatch) {
'use strict';

@@ -34,5 +35,5 @@

return {
lineSeparator: '\n',
dump: jsonDump,
widthOf: multibyteStringWidthOf
lineDiffThreshold: 5,
stringifyDepth: 2,
lineSeparator: '\n'
};

@@ -42,19 +43,21 @@ }

function PowerAssertContextRenderer (dump, widthOf, context) {
this.dump = dump;
this.widthOf = widthOf;
function PowerAssertContextRenderer (config) {
this.config = config;
this.stringify = config.stringify;
this.compare = config.compare;
this.widthOf = config.widthOf;
this.initialVertivalBarLength = 1;
this.initWithContext(context);
}
PowerAssertContextRenderer.prototype.initWithContext = function (context) {
var i, events = collectEvents(context);
// console.log(JSON.stringify(context, null, 2));
events.sort(rightToLeft);
this.events = events;
PowerAssertContextRenderer.prototype.init = function (context) {
this.context = context;
this.assertionLine = context.source.content;
this.filepath = context.source.filepath;
this.lineNumber = firstLineNumberFor(context);
this.lineNumber = context.source.line;
this.initializeRows();
};
PowerAssertContextRenderer.prototype.initializeRows = function () {
this.rows = [];
for (i = 0; i <= this.initialVertivalBarLength; i += 1) {
for (var i = 0; i <= this.initialVertivalBarLength; i += 1) {
this.addOneMoreRow();

@@ -98,3 +101,3 @@ }

capturedEvents.forEach(function (captured) {
var dumpedValue = that.dump(captured.value);
var dumpedValue = that.stringify(captured.value);
if (that.isOverlapped(prevCaptured, captured, dumpedValue)) {

@@ -110,18 +113,31 @@ that.addOneMoreRow();

PowerAssertContextRenderer.prototype.startColumnFor = function (captured) {
return this.widthOf(this.assertionLine.slice(0, captured.location.start.column));
return this.widthOf(this.assertionLine.slice(0, captured.loc.start.column));
};
PowerAssertContextRenderer.prototype.renderLines = function () {
var lines = [], lineNum = this.lineNumber;
this.constructRows(this.events);
var that = this,
lines = [],
events = [],
pairs = [];
if (this.filepath) {
lines.push('# ' + [this.filepath, lineNum].join(':'));
lines.push('# ' + [this.filepath, this.lineNumber].join(':'));
} else {
lines.push('# at line: ' + lineNum);
lines.push('# at line: ' + this.lineNumber);
}
lines.push('');
lines.push(this.assertionLine);
traverseContext(this.context, events, pairs);
events.sort(rightToLeft);
this.constructRows(events);
this.rows.forEach(function (columns) {
lines.push(columns.join(''));
});
pairs.forEach(function (pair) {
that.compare(pair, lines);
});
lines.push('');

@@ -132,43 +148,114 @@ return lines;

function firstLineNumberFor(context) {
var lineNums = context.args.map(function (arg) {
return arg.meta.tree.loc.start.line;
});
lineNums.sort(function (a, b) {
return b - a;
});
return lineNums[0];
function EsNode (path, currentNode, parentNode, espathToValue, jsCode, jsAST) {
if (path) {
this.espath = path.join('/');
this.parentEspath = path.slice(0, path.length - 1).join('/');
this.currentProp = path[path.length - 1];
} else {
this.espath = '';
this.parentEspath = '';
this.currentProp = null;
}
this.currentNode = currentNode;
this.parentNode = parentNode;
this.parentEsNode = null;
this.espathToValue = espathToValue;
this.jsCode = jsCode;
this.jsAST = jsAST;
}
EsNode.prototype.setParentEsNode = function (parentEsNode) {
this.parentEsNode = parentEsNode;
};
EsNode.prototype.getParentEsNode = function () {
return this.parentEsNode;
};
EsNode.prototype.code = function () {
return this.jsCode.slice(this.currentNode.loc.start.column, this.currentNode.loc.end.column);
};
EsNode.prototype.value = function () {
if (this.currentNode.type === syntax.Literal) {
return this.currentNode.value;
}
return this.espathToValue[this.espath];
};
EsNode.prototype.isCaptured = function () {
return this.espathToValue.hasOwnProperty(this.espath);
};
EsNode.prototype.location = function () {
return locationOf(this.currentNode, this.jsAST.tokens);
};
function collectEvents(context) {
var events = [];
function traverseContext (context, events, pairs) {
context.args.forEach(function (arg) {
var tokens = arg.meta.tokens,
espathList = arg.events.map(function (ev) {
return ev.espath;
}),
espathToValue = arg.events.reduce(function (accum, ev) {
accum[ev.espath] = ev.value;
return accum;
}, {});
// console.log(JSON.stringify(espathToValue, null, 2));
// console.log(JSON.stringify(espathList, null, 2));
// console.log(JSON.stringify(arg.meta.tree, null, 2));
estraverse.traverse(arg.meta.tree, {
enter: function (currentNode, parentNode) {
var controller = this,
path = controller.path(),
espath = path ? path.join('/') : '';
if ((typeof espathToValue[espath] === 'undefined') && (espathList.indexOf(espath) === -1)) {
return;
var espathToPair = {};
onEachEsNode(arg, context.source.content, function (esNode) {
var pair;
if (!esNode.isCaptured()) {
if (isTargetBinaryExpression(esNode.getParentEsNode()) && esNode.currentNode.type === syntax.Literal) {
espathToPair[esNode.parentEspath][esNode.currentProp] = {code: esNode.code(), value: esNode.value()};
}
events.push({value: espathToValue[espath], espath: espath, location: locationOf(currentNode, tokens)});
return;
}
events.push({value: esNode.value(), espath: esNode.espath, loc: esNode.location()});
if (isTargetBinaryExpression(esNode.getParentEsNode())) {
espathToPair[esNode.parentEspath][esNode.currentProp] = {code: esNode.code(), value: esNode.value()};
}
if (isTargetBinaryExpression(esNode)) {
pair = {
operator: esNode.currentNode.operator,
value: esNode.value()
};
espathToPair[esNode.espath] = pair;
}
});
Object.keys(espathToPair).forEach(function (espath) {
var pair = espathToPair[espath];
if (pair.left && pair.right) {
pairs.push(pair);
}
});
});
return events;
}
function isTargetBinaryExpression (esNode) {
return esNode &&
esNode.currentNode.type === syntax.BinaryExpression &&
(esNode.currentNode.operator === '===' || esNode.currentNode.operator === '==') &&
esNode.isCaptured() &&
!(esNode.value());
}
function onEachEsNode(arg, jsCode, callback) {
var jsAST = esprima.parse(jsCode, {tolerant: true, loc: true, tokens: true, raw: true}),
espathToValue = arg.events.reduce(function (accum, ev) {
accum[ev.espath] = ev.value;
return accum;
}, {}),
nodeStack = [];
estraverse.traverse(extractExpressionFrom(jsAST), {
enter: function (currentNode, parentNode) {
var esNode = new EsNode(this.path(), currentNode, parentNode, espathToValue, jsCode, jsAST);
if (1 < nodeStack.length) {
esNode.setParentEsNode(nodeStack[nodeStack.length - 1]);
}
nodeStack.push(esNode);
callback(esNode);
},
leave: function (currentNode, parentNode) {
nodeStack.pop();
}
});
}
function extractExpressionFrom (tree) {
var expressionStatement = tree.body[0],
expression = expressionStatement.expression;
return expression;
}
function locationOf(currentNode, tokens) {

@@ -194,25 +281,24 @@ switch(currentNode.type) {

function searchToken(tokens, fromLine, toLine, predicate) {
var i, token, found;
for(i = 0; i < tokens.length; i += 1) {
token = tokens[i];
if (token.loc.start.line < fromLine) {
continue;
}
if (toLine < token.loc.end.line) {
break;
}
found = predicate(token, i);
if (found) {
return found;
}
function propertyLocationOf(memberExpression, tokens) {
var prop = memberExpression.property,
token;
if (!memberExpression.computed) {
return prop.loc;
}
return undefined;
token = findLeftBracketTokenOf(memberExpression, tokens);
return token ? token.loc : prop.loc;
}
// calculate location of infix operator for BinaryExpression, AssignmentExpression and LogicalExpression.
function infixOperatorLocationOf (expression, tokens) {
var token = findOperatorTokenOf(expression, tokens);
return token ? token.loc : expression.left.loc;
}
function findLeftBracketTokenOf(expression, tokens) {
var fromColumn = expression.property.loc.start.column,
fromLine = expression.loc.start.line,
toLine = expression.property.loc.start.line;
var fromLine = expression.loc.start.line,
toLine = expression.property.loc.start.line,
fromColumn = expression.property.loc.start.column;
return searchToken(tokens, fromLine, toLine, function (token, index) {

@@ -232,6 +318,6 @@ var prevToken;

function findOperatorTokenOf(expression, tokens) {
var fromColumn = expression.left.loc.end.column,
toColumn = expression.right.loc.start.column,
fromLine = expression.left.loc.end.line,
toLine = expression.right.loc.start.line;
var fromLine = expression.left.loc.end.line,
toLine = expression.right.loc.start.line,
fromColumn = expression.left.loc.end.column,
toColumn = expression.right.loc.start.column;
return searchToken(tokens, fromLine, toLine, function (token, index) {

@@ -249,23 +335,18 @@ if (fromColumn < token.loc.start.column &&

// calculate location of infix operator for BinaryExpression, AssignmentExpression and LogicalExpression.
function infixOperatorLocationOf (expression, tokens) {
var token = findOperatorTokenOf(expression, tokens);
if (token) {
// console.log('TOKEN INDEX: ' + token.loc.start.column);
return token.loc;
}
return expression.left.loc;
}
function propertyLocationOf(memberExpression, tokens) {
var prop = memberExpression.property,
token;
if (memberExpression.computed) {
token = findLeftBracketTokenOf(memberExpression, tokens);
if (token) {
return token.loc;
function searchToken(tokens, fromLine, toLine, predicate) {
var i, token, found;
for(i = 0; i < tokens.length; i += 1) {
token = tokens[i];
if (token.loc.start.line < fromLine) {
continue;
}
if (toLine < token.loc.end.line) {
break;
}
found = predicate(token, i);
if (found) {
return found;
}
}
return prop.loc;
return undefined;
}

@@ -284,3 +365,3 @@

function rightToLeft (a, b) {
return b.location.start.column - a.location.start.column;
return b.loc.start.column - a.loc.start.column;
}

@@ -303,21 +384,45 @@

function jsonDump (obj) {
var seen = [],
replacer = function(key, val) {
if (typeof val === 'object' && val) {
if (seen.indexOf(val) !== -1) {
return '#Circular#';
}
seen.push(val);
}
return val;
},
str = JSON.stringify(obj, replacer);
if (typeof str === 'undefined') {
function constructorNameOf (obj) {
var ctor = obj.constructor,
cname = '';
if (typeof(ctor) === 'function') {
cname = ctor.name ? ctor.name : Object.prototype.toString.call(obj).slice(8, -1);
}
return cname ? cname : 'Object';
}
function typeNameOf (val) {
var type = typeof(val);
if (val === null) {
return 'null';
}
if (typeof val === 'undefined') {
return 'undefined';
}
return str;
if (type === 'object') {
if (Array.isArray(val)) {
return 'Array';
}
return constructorNameOf(val);
}
return type;
}
function isComparedByValue (obj) {
if (obj === null) {
return true;
}
switch(typeof obj) {
case 'string':
case 'number':
case 'boolean':
case 'undefined':
return true;
}
return false;
}
// borrowed from qunit.js

@@ -339,6 +444,165 @@ function extend (a, b) {

function defaultStringifier (config) {
var globalConstructors = [
Boolean,
Date,
Number,
RegExp,
String
];
function stringify(obj, depth) {
if (typeof depth !== 'number') {
depth = config.stringifyDepth;
}
return stringifyAny(obj, depth);
}
function stringifyAny(obj, depth) {
switch(typeof obj) {
case 'string':
case 'boolean':
return JSON.stringify(obj);
case 'number':
return stringifyNumber(obj);
case 'function':
return '#function#';
case 'undefined':
return 'undefined';
case 'object':
if (obj === null) {
return 'null';
} else if (Array.isArray(obj)) {
return stringifyArray(obj, depth);
} else {
return stringifyObject(obj, depth);
}
break;
default:
break;
}
}
function stringifyNumber(num) {
if (isNaN(num)) {
return 'NaN';
}
if (!isFinite(num)) {
return num === Infinity ? 'Infinity' : '-Infinity';
}
return JSON.stringify(num);
}
function stringifyArray(ary, depth) {
depth -= 1;
if (depth === 0) {
return '#Array#';
}
return '[' + ary.map(function (elem, idx) {
return stringifyAny(elem, depth);
}).join(',') + ']';
}
function stringifyObject(obj, depth) {
var pairs, cname;
depth -= 1;
if (obj instanceof RegExp) {
return obj.toString();
}
cname = constructorNameOf(obj);
if (globalConstructors.some(function (ctor) { return obj instanceof ctor; })) {
return 'new ' + cname + '(' + JSON.stringify(obj) + ')';
}
if (depth === 0) {
return '#' + cname + '#';
}
pairs = [];
Object.keys(obj).forEach(function (key) {
var val = stringifyAny(obj[key], depth);
if (!/^[A-Za-z_]+$/.test(key)) {
key = JSON.stringify(key);
}
pairs.push(key + ':' + val);
});
return cname + '{' + pairs.join(',') + '}';
}
return stringify;
}
function defaultComparator(config) {
var dmp = new DiffMatchPatch();
function compare(pair, lines) {
if (isStringDiffTarget(pair)) {
showStringDiff(pair, lines);
} else {
showExpectedAndActual(pair, lines);
}
}
function isStringDiffTarget(pair) {
return typeof pair.left.value === 'string' && typeof pair.right.value === 'string';
}
function showExpectedAndActual (pair, lines) {
lines.push('');
lines.push('$$$ [' + typeNameOf(pair.right.value) + '] ' + pair.right.code);
lines.push('### [' + typeNameOf(pair.left.value) + '] ' + pair.left.code);
lines.push('$=> ' + config.stringify(pair.right.value));
lines.push('#=> ' + config.stringify(pair.left.value));
}
function showStringDiff (pair, lines) {
var patch;
if (shouldUseLineLevelDiff(pair.right.value)) {
patch = udiffLines(pair.right.value, pair.left.value);
} else {
patch = udiffChars(pair.right.value, pair.left.value);
}
lines.push('');
lines.push('--- [string] ' + pair.right.code);
lines.push('+++ [string] ' + pair.left.code);
lines.push(decodeURIComponent(patch));
}
function shouldUseLineLevelDiff (text) {
return config.lineDiffThreshold < text.split(/\r\n|\r|\n/).length;
}
function udiffLines(text1, text2) {
/*jshint camelcase: false */
var a = dmp.diff_linesToChars_(text1, text2),
diffs = dmp.diff_main(a.chars1, a.chars2, false);
dmp.diff_charsToLines_(diffs, a.lineArray);
dmp.diff_cleanupSemantic(diffs);
return dmp.patch_toText(dmp.patch_make(text1, diffs));
}
function udiffChars (text1, text2) {
/*jshint camelcase: false */
var diffs = dmp.diff_main(text1, text2, false);
dmp.diff_cleanupSemantic(diffs);
return dmp.patch_toText(dmp.patch_make(text1, diffs));
}
return compare;
}
function create (options) {
var config = extend(defaultOptions(), (options || {}));
if (typeof config.widthOf !== 'function') {
config.widthOf = multibyteStringWidthOf;
}
if (typeof config.stringify !== 'function') {
config.stringify = defaultStringifier(config);
}
if (typeof config.compare !== 'function') {
config.compare = defaultComparator(config);
}
return function (context) {
var renderer = new PowerAssertContextRenderer(config.dump, config.widthOf, context);
var renderer = new PowerAssertContextRenderer(config);
renderer.init(context);
return renderer.renderLines().join(config.lineSeparator);

@@ -349,3 +613,6 @@ };

create.PowerAssertContextRenderer = PowerAssertContextRenderer;
create.constructorNameOf = constructorNameOf;
create.typeNameOf = typeNameOf;
create.isComparedByValue = isComparedByValue;
return create;
}));
{
"name": "power-assert-formatter",
"description": "Power Assert output formatter",
"version": "0.6.1",
"version": "0.7.0",
"keywords": [

@@ -32,12 +32,13 @@ "power-assert",

"dependencies": {
"estraverse": "~1.5.0"
"esprima": "~1.2.1",
"estraverse": "~1.5.0",
"googlediff": "~0.1.0"
},
"devDependencies": {
"blanket": "~1.1.6",
"bower": "~1.3.1",
"bower": "~1.3.3",
"coveralls": "~2.10.0",
"empower": "~0.6.0",
"escodegen": "~1.3.1",
"espower": "~0.6.0",
"esprima": "~1.1.1",
"empower": "~0.7.1",
"escodegen": "~1.3.2",
"espower": "~0.7.0",
"grunt": "~0.4.2",

@@ -50,4 +51,4 @@ "grunt-bower-requirejs": "~0.9.2",

"grunt-mocha": "~0.4.10",
"grunt-mocha-test": "~0.10.0",
"jit-grunt": "~0.4.1",
"grunt-mocha-test": "~0.10.2",
"jit-grunt": "~0.5.0",
"mocha-lcov-reporter": "~0.0.1"

@@ -54,0 +55,0 @@ },

power-assert-formatter
================================
[![Build Status](https://travis-ci.org/twada/power-assert-formatter.png?branch=master)](https://travis-ci.org/twada/power-assert-formatter)
[![NPM version](https://badge.fury.io/js/power-assert-formatter.png)](http://badge.fury.io/js/power-assert-formatter)
[![Dependency Status](https://gemnasium.com/twada/power-assert-formatter.png)](https://gemnasium.com/twada/power-assert-formatter)
[![Build Status](https://travis-ci.org/twada/power-assert-formatter.svg?branch=master)](https://travis-ci.org/twada/power-assert-formatter)
[![NPM version](https://badge.fury.io/js/power-assert-formatter.svg)](http://badge.fury.io/js/power-assert-formatter)
[![Dependency Status](https://gemnasium.com/twada/power-assert-formatter.svg)](https://gemnasium.com/twada/power-assert-formatter)
[![Coverage Status](https://coveralls.io/repos/twada/power-assert-formatter/badge.png?branch=master)](https://coveralls.io/r/twada/power-assert-formatter?branch=master)

@@ -8,0 +8,0 @@ [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/)

@@ -51,5 +51,5 @@ (function (root, factory) {

'',
'assert(falsyNum);',
' | ',
' 0 ',
'assert(falsyNum)',
' | ',
' 0 ',
''

@@ -56,0 +56,0 @@ ].join(expectedSeparator));

@@ -11,3 +11,4 @@ var require = {

mocha: "../bower_components/mocha/mocha",
requirejs: "../bower_components/requirejs/require"
requirejs: "../bower_components/requirejs/require",
diff_match_patch: "../bower_components/google-diff-match-patch-js/diff_match_patch"
},

@@ -18,2 +19,5 @@ shim: {

},
diff_match_patch: {
exports: "diff_match_patch"
},
escodegen: {

@@ -20,0 +24,0 @@ exports: "escodegen"

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc