[Pkg-javascript-commits] [node-stack-utils] 04/67: add lots of tests

Bastien Roucariès rouca at moszumanska.debian.org
Thu Sep 7 09:53:01 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-stack-utils.

commit 5132ae49f8fd37de2f45d7b57b47767ceb392af2
Author: James Talmage <james at talmage.io>
Date:   Sun Jan 3 23:10:18 2016 -0500

    add lots of tests
---
 fixtures/capture-fixture.js |  62 ++++++++++++
 index.js                    |  11 ++-
 package.json                |   7 ++
 test.js                     | 230 ++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 298 insertions(+), 12 deletions(-)

diff --git a/fixtures/capture-fixture.js b/fixtures/capture-fixture.js
new file mode 100644
index 0000000..0fe8c44
--- /dev/null
+++ b/fixtures/capture-fixture.js
@@ -0,0 +1,62 @@
+'use strict';
+module.exports = CaptureFixture;
+
+function CaptureFixture(stack) {
+	this.stack = stack;
+}
+
+CaptureFixture.prototype.redirect1 = function () {
+	var args = Array.prototype.slice.call(arguments);
+	var method = args.shift();
+	return this[method].apply(this, args);
+};
+
+CaptureFixture.prototype.redirect2 = function () {
+	var args = Array.prototype.slice.call(arguments);
+	var method = args.shift();
+	return this[method].apply(this, args);
+};
+
+CaptureFixture.prototype.call = function () {
+	var args = Array.prototype.slice.call(arguments);
+	var method = args.shift();
+	return this.stack[method].apply(this.stack, args);
+};
+
+CaptureFixture.prototype.const = function () {
+	var args = Array.prototype.slice.call(arguments);
+	var method = args.shift();
+	var self = this;
+
+	function Constructor() {
+		this.val = self[method].apply(self, args);
+	}
+
+	return new Constructor().val;
+};
+
+CaptureFixture.prototype.obj = function () {
+	var args = Array.prototype.slice.call(arguments);
+	var methodName = args.shift();
+	var method = args.shift();
+	var self = this;
+
+	var obj = {};
+	obj[methodName] = function () {
+		return self[method].apply(self, args);
+	};
+
+	return obj[methodName]();
+};
+
+CaptureFixture.prototype.eval = function () {
+	var args = Array.prototype.slice.call(arguments);
+	var method = args.shift();
+	var self = this;
+
+	return eval('self[method].apply(self, args)');
+};
+
+CaptureFixture.prototype.error = function (message) {
+	return new Error(message);
+};
diff --git a/index.js b/index.js
index 5c74e63..961e6fa 100644
--- a/index.js
+++ b/index.js
@@ -120,14 +120,17 @@ StackUtils.prototype.at = function at(fn) {
 	}
 
 	var res = {
-		file: site.getFileName(),
 		line: site.getLineNumber(),
 		column: site.getColumnNumber()
 	};
 
-	if (res.file.indexOf(this._cwd + '/') === 0 ||
-		res.file.indexOf(this._cwd + '\\') === 0) {
-		res.file = res.file.substr(this._cwd.length + 1);
+	var file = site.getFileName();
+	if (file) {
+		if (file.indexOf(this._cwd + '/') === 0 ||
+			file.indexOf(this._cwd + '\\') === 0) {
+			file = file.substr(this._cwd.length + 1);
+		}
+		res.file = file;
 	}
 
 	if (site.isConstructor()) {
diff --git a/package.json b/package.json
index 259566f..891b392 100644
--- a/package.json
+++ b/package.json
@@ -21,6 +21,13 @@
   "keywords": [
     ""
   ],
+  "config": {
+    "nyc": {
+      "exclude": [
+        "fixtures/*"
+      ]
+    }
+  },
   "dependencies": {},
   "devDependencies": {
     "ava": "^0.8.0",
diff --git a/test.js b/test.js
index 55fdfc6..7090c52 100644
--- a/test.js
+++ b/test.js
@@ -1,9 +1,12 @@
+import path from 'path';
 import test from 'ava';
-import StackUtils from './';
 import flatten from 'flatten';
+import StackUtils from './';
+import CaptureFixture from './fixtures/capture-fixture';
 
-const LinuxStack1 = join(linux1(), internals());
-const WindowsStack1 = join(windows1(), internals());
+const LinuxStack1 = join(linuxStack1(), internalStack());
+const WindowsStack1 = join(windowsStack1(), internalStack());
+const fixtureDir = path.join(__dirname, 'fixtures');
 
 test('must be called with new', t => {
 	t.is(typeof StackUtils, 'function');
@@ -53,8 +56,213 @@ test('clean: eliminates internals', t => {
 });
 
 test('clean: returns null if it is all internals', t => {
-	let stack = new StackUtils({internals: StackUtils.nodeInternals()});
-	t.is(stack.clean(join(internals())), null);
+	const stack = new StackUtils({internals: StackUtils.nodeInternals()});
+	t.is(stack.clean(join(internalStack())), null);
+});
+
+test('captureString: two redirects', t => {
+	const stack = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stack);
+
+	const capturedString = capture.redirect1('redirect2', 'call', 'captureString');
+	t.is(capturedString, join([
+		'CaptureFixture.call (capture-fixture.js:23:28)',
+		'CaptureFixture.redirect2 (capture-fixture.js:17:22)',
+		'CaptureFixture.redirect1 (capture-fixture.js:11:22)'
+	]));
+});
+
+test('captureString: with startStack function', t => {
+	const stack = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stack);
+
+	const capturedString = capture.redirect1('redirect2', 'call', 'captureString', capture.call);
+	t.is(capturedString, join([
+		'CaptureFixture.redirect2 (capture-fixture.js:17:22)',
+		'CaptureFixture.redirect1 (capture-fixture.js:11:22)'
+	]));
+});
+
+test('captureString: with limit', t => {
+	const stack = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stack);
+
+	const capturedString = capture.redirect1('redirect2', 'call', 'captureString', 1);
+	t.is(capturedString, join([
+		'CaptureFixture.call (capture-fixture.js:23:28)'
+	]));
+});
+
+test('captureString: with limit and startStack', t => {
+	const stack = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stack);
+
+	const capturedString = capture.redirect1('redirect2', 'call', 'captureString', 1, capture.call);
+	t.is(capturedString, join([
+		'CaptureFixture.redirect2 (capture-fixture.js:17:22)'
+	]));
+});
+
+test('capture returns an array of call sites', t => {
+	const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stackUtil);
+	const stack = capture.redirect1('call', 'capture').slice(0, 2);
+	t.is(stack[0].getFileName(), path.join(fixtureDir, 'capture-fixture.js'));
+	t.is(stack[0].getFunctionName(), 'CaptureFixture.call');
+	t.is(stack[1].getFunctionName(), 'CaptureFixture.redirect1');
+});
+
+test('capture: with limit', t => {
+	const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stackUtil);
+	const stack = capture.redirect1('redirect2', 'call', 'capture', 1);
+	t.is(stack.length, 1);
+	t.is(stack[0].getFunctionName(), 'CaptureFixture.call');
+});
+
+test('capture: with stackStart function', t => {
+	const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stackUtil);
+	const stack = capture.redirect1('redirect2', 'call', 'capture', capture.call);
+	t.true(stack.length > 1);
+	t.is(stack[0].getFunctionName(), 'CaptureFixture.redirect2');
+});
+
+test('capture: with limit and stackStart function', t => {
+	const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stackUtil);
+	const stack = capture.redirect1('redirect2', 'call', 'capture', 1, capture.call);
+	t.is(stack.length, 1);
+	t.is(stack[0].getFunctionName(), 'CaptureFixture.redirect2');
+});
+
+test('at', t => {
+	const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stackUtil);
+	const at = capture.redirect1('call', 'at');
+
+	t.same(at, {
+		file: 'capture-fixture.js',
+		line: 23,
+		column: 28,
+		type: 'CaptureFixture',
+		function: 'CaptureFixture.call',
+		method: 'call'
+	});
+});
+
+test('at: with stackStart', t => {
+	const stackUtil = new StackUtils({internals: internals(), cwd: __dirname});
+	const capture = new CaptureFixture(stackUtil);
+
+	const at = capture.redirect1('call', 'at', capture.call);
+
+	t.same(at, {
+		file: `fixtures${path.sep}capture-fixture.js`,
+		line: 11,
+		column: 22,
+		type: 'CaptureFixture',
+		function: 'CaptureFixture.redirect1',
+		method: 'redirect1'
+	});
+});
+
+test('at: inside a constructor call', t => {
+	const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stackUtil);
+
+	const at = capture.const('call', 'at', capture.call);
+
+	// TODO: File an issue - if this assert fails, the power assert diagram renderer blows up.
+	t.same(at, {
+		file: 'capture-fixture.js',
+		line: 32,
+		column: 27,
+		constructor: true,
+		type: 'Constructor',
+		function: 'Constructor'
+	});
+});
+
+test('at: method on an [Object] instance', t => {
+	const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stackUtil);
+
+	const at = capture.const('obj', 'foo', 'call', 'at', capture.call);
+
+	t.same(at, {
+		file: 'capture-fixture.js',
+		line: 46,
+		column: 23,
+		function: 'obj.(anonymous function)',
+		method: 'foo'
+	});
+});
+
+test('at: returns empty object if #capture() returns an empty stack', t => {
+	const stackUtil = new StackUtils();
+	stackUtil.capture = function () {
+		return [];
+	};
+	t.same(stackUtil.at(), {});
+});
+
+test('at: eval', t => {
+	const stackUtil = new StackUtils({internals: internals(), cwd: fixtureDir});
+	const capture = new CaptureFixture(stackUtil);
+
+	const at = capture.eval('call', 'at', capture.call);
+
+	t.same(at, {
+		line: 1,
+		column: 14,
+		evalOrigin: 'eval at <anonymous> (' + path.join(fixtureDir, 'capture-fixture.js') + ':57:9)',
+		function: 'eval'
+	});
+	t.pass();
+});
+
+test('parseLine', t => {
+	const stack = new StackUtils({internals: internals(), cwd: '/user/dev/project'});
+	const capture = new CaptureFixture(stack);
+
+	t.same(stack.parseLine('foo'), null, 'should not match');
+
+	t.same(stack.parseLine('    at foo (/user/dev/project/foo.js:3:8)'), {
+		file: 'foo.js',
+		line: 3,
+		column: 8,
+		function: 'foo'
+	});
+
+	t.same(stack.parseLine('    at foo (/some/other/dir/file.js:3:8)'), {
+		file: '/some/other/dir/file.js',
+		line: 3,
+		column: 8,
+		function: 'foo'
+	});
+
+	// TODO: report issue - this also causes power-assert diagram renderer to fail
+	t.same(stack.parseLine('    at new Foo (/user/dev/project/foo.js:3:8)'), {
+		file: 'foo.js',
+		line: 3,
+		column: 8,
+		constructor: true,
+		function: 'Foo'
+	});
+
+	const evalStack = capture.eval('error', 'foo').stack.split('\n');
+
+	t.same(stack.parseLine(evalStack[2]), {
+		file: '<anonymous>',
+		line: 1,
+		column: 14,
+		evalOrigin: '<anonymous>',
+		evalLine: 57,
+		evalColumn: 9,
+		evalFile: path.join(fixtureDir, 'capture-fixture.js'),
+		function: 'eval'
+	});
 });
 
 function join() {
@@ -62,7 +270,7 @@ function join() {
 	return flatten(args).join('\n') + '\n';
 }
 
-function linux1() {
+function linuxStack1() {
 	return [
 		'Error: foo',
 		'    at foo (/user/dev/project/foo.js:3:8)',
@@ -72,7 +280,7 @@ function linux1() {
 	];
 }
 
-function windows1() {
+function windowsStack1() {
 	return [
 		'Error: foo',
 		'    at foo (Z:\\user\\dev\\project\\foo.js:3:8)',
@@ -82,7 +290,7 @@ function windows1() {
 	];
 }
 
-function internals() {
+function internalStack() {
 	return [
 		'    at Module._compile (module.js:398:26)',
 		'    at Object.Module._extensions..js (module.js:405:10)',
@@ -93,3 +301,9 @@ function internals() {
 	];
 }
 
+function internals() {
+	return StackUtils.nodeInternals().concat([
+		/test\.js:[0-9]+:[0-9]+\)?$/,
+		/[\\\/]node_modules[\\\/]/
+	]);
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-stack-utils.git



More information about the Pkg-javascript-commits mailing list