Skip to content

Commit

Permalink
update tests so that they can run in node directly (in addition to th…
Browse files Browse the repository at this point in the history
…e browser)
  • Loading branch information
kevinbarabash committed Jan 10, 2016
1 parent 1966521 commit 957b392
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [ "es2015" ],
"ignore": [
"node_modules/**/*.js"
]
}
8 changes: 1 addition & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
language: node_js
node_js:
- "0.10"
before_install:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
before_script:
- npm install -g bower
- bower install
- "node"
script: npm test
14 changes: 9 additions & 5 deletions build/debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -50404,7 +50404,7 @@ var canUseNativeGenerators = function canUseNativeGenerators() {
};

var isGeneratorFunction = function isGeneratorFunction(classFn) {
return classFn.isGenerator && classFn.isGenerator() || classFn.prototype.toString() === "[object Generator]";
return classFn.constructor.name === "GeneratorFunction";
};

var Debugger = function () {
Expand Down Expand Up @@ -51036,6 +51036,7 @@ exports.every = every;
exports.some = some;

},{}],140:[function(require,module,exports){
(function (global){
"use strict";

var array = require("./array");
Expand All @@ -51054,10 +51055,12 @@ methods.forEach(function (methodName) {
};
});

var originalSetTimeout = window.setTimeout;
var originalSetInterval = window.setInterval;
var self = typeof window !== "undefined" ? window : global;

var originalSetTimeout = self.setTimeout;
var originalSetInterval = self.setInterval;

window.setTimeout = function (callback, delay) {
self.setTimeout = function (callback, delay) {
if (_isGeneratorFunction(callback)) {
return originalSetTimeout(function () {
__schedule__(callback);
Expand All @@ -51067,7 +51070,7 @@ window.setTimeout = function (callback, delay) {
}
};

window.setInterval = function (callback, delay) {
self.setInterval = function (callback, delay) {
if (_isGeneratorFunction(callback)) {
return originalSetInterval(function () {
__schedule__(callback);
Expand All @@ -51077,6 +51080,7 @@ window.setInterval = function (callback, delay) {
}
};

}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./array":139}],141:[function(require,module,exports){
"use strict";

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Debug JavaScript using JavaScript",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/testee test/runner.html --browsers firefox",
"test": "mocha ./test/*-spec.js",
"build": "browserify ./src/debugger.js -o ./build/debugger.js -s Debugger -t [ babelify --presets [ es2015 ] ]",
"watch": "watchify ./src/debugger.js -o ./build/debugger.js -s Debugger -v -t [ babelify --presets [ es2015 ] ]",
"processing": "browserify ./src/processing-debugger.js -o ./build/processing-debugger.js --exclude ./src/debugger.js -s ProcessingDebugger -t [ babelify --presets [ es2015 ] ]"
Expand All @@ -28,6 +28,7 @@
"jquery": "^2.1.1",
"mocha": "^2.0.1",
"poster": "git://github.com/kevinb7/poster.git",
"sinon": "^1.17.2",
"testee": "^0.1.7",
"watchify": "^3.6.1"
},
Expand Down
2 changes: 1 addition & 1 deletion src/debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var canUseNativeGenerators = function() {
};

var isGeneratorFunction = function(classFn) {
return classFn.isGenerator && classFn.isGenerator() || classFn.prototype.toString() === "[object Generator]";
return classFn.constructor.name === "GeneratorFunction";
};

class Debugger {
Expand Down
9 changes: 5 additions & 4 deletions src/runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ methods.forEach(function (methodName) {
}
});

var self = typeof window !== "undefined" ? window : global;

var originalSetTimeout = window.setTimeout;
var originalSetInterval = window.setInterval;
var originalSetTimeout = self.setTimeout;
var originalSetInterval = self.setInterval;

window.setTimeout = function(callback, delay) {
self.setTimeout = function(callback, delay) {
if (_isGeneratorFunction(callback)) {
return originalSetTimeout(function() {
__schedule__(callback);
Expand All @@ -28,7 +29,7 @@ window.setTimeout = function(callback, delay) {
}
};

window.setInterval = function(callback, delay) {
self.setInterval = function(callback, delay) {
if (_isGeneratorFunction(callback)) {
return originalSetInterval(function () {
__schedule__(callback);
Expand Down
39 changes: 24 additions & 15 deletions test/array-spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
if (typeof require !== "undefined") {
var sinon = require("sinon");
var expect = require("expect.js");
require("../node_modules/regenerator/runtime.js");

var Debugger = require("../build/debugger.js");
var getFunctionBody = require("./test_utils.js").getFunctionBody;
}

[false, true].forEach(function (nativeGenerators) {
var title = nativeGenerators ?
"Array.prototype Runtime (Native Generators)" :
"Array.prototype Runtime (Regenerator Generators)";

describe(title, function () {

var _debugger, context;

var runTest = function(options) {
Expand Down Expand Up @@ -51,7 +60,7 @@
var a = [1,4,9];
var b = a.map(Math.sqrt);
});

_debugger.load(code);
_debugger.start();

Expand All @@ -71,7 +80,7 @@
_debugger.load(code);
_debugger.setBreakpoint(3);
_debugger.start();

expect(_debugger.line).to.be(3);
_debugger.resume();
expect(_debugger.line).to.be(3);
Expand All @@ -87,7 +96,7 @@
return val * val;
});
});

_debugger.load(code);
_debugger.setBreakpoint(3);
_debugger.start();
Expand All @@ -99,7 +108,7 @@
expect(_debugger.currentScope.val).to.be(3);
_debugger.resume();
});

it("should step out of the callback to the main program", function () {
var code = getFunctionBody(function () {
var a = [1,2,3];
Expand All @@ -108,7 +117,7 @@
});
print("end of line");
});

_debugger.load(code);
_debugger.setBreakpoint(3);
_debugger.start();
Expand Down Expand Up @@ -150,11 +159,11 @@
},
test: function () {
_debugger.start();

expect(context.b).to.be(6);
}
});

runTest({
title: "can break inside the callback",
code: function () {
Expand All @@ -166,7 +175,7 @@
test: function () {
_debugger.setBreakpoint(3);
_debugger.start();

expect(_debugger.line).to.be(3);
_debugger.resume();
expect(_debugger.line).to.be(3);
Expand All @@ -175,7 +184,7 @@
_debugger.resume();
}
});

runTest({
title: "reports scope variables inside the callback",
code: function () {
Expand All @@ -187,7 +196,7 @@
test: function () {
_debugger.setBreakpoint(3);
_debugger.start();

expect(_debugger.currentScope.previousVal).to.be(0);
expect(_debugger.currentScope.currentVal).to.be(1);
_debugger.resume();
Expand Down Expand Up @@ -347,7 +356,7 @@
print(val)
});
});

_debugger.load(code);
_debugger.start();

Expand All @@ -362,7 +371,7 @@
var a = [1,2,3];
a.forEach(print);
});

_debugger.load(code);
_debugger.start();

Expand All @@ -379,7 +388,7 @@
print(val)
});
});

_debugger.load(code);
_debugger.setBreakpoint(3);
_debugger.start();
Expand All @@ -399,7 +408,7 @@
print(val)
});
});

_debugger.load(code);
_debugger.setBreakpoint(3);
_debugger.start();
Expand Down
27 changes: 20 additions & 7 deletions test/debugger-spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
var self = typeof window !== "undefined" ? window : global;

if (typeof require !== "undefined") {
var sinon = require("sinon");
var expect = require("expect.js");
require("../node_modules/regenerator/runtime.js");

var Debugger = require("../build/debugger.js");
var getFunctionBody = require("./test_utils.js").getFunctionBody;
}

/*global describe, it, beforeEach, afterEach */

[false, true].forEach(function (nativeGenerators) {
var title = nativeGenerators ?
"Debugger (Native Generators)" :
"Debugger (Regenerator Generators)";

var fill, rect, print, image;

describe(title, function () {

var _debugger, context;
Expand Down Expand Up @@ -32,7 +45,7 @@

_debugger = new Debugger({
nativeGenerators: nativeGenerators,
debug: true
debug: false
});
_debugger.context = context;
});
Expand All @@ -41,12 +54,12 @@
beforeEach(function () {
_debugger.load("fill(255,0,0);x=5;console.log('hello');_test_global='apple';var z=23;");
sinon.stub(console, "log");
window._test_global = "";
self._test_global = "";
});

afterEach(function () {
console.log.restore();
delete window._test_global;
delete self._test_global;
});

it("should call functions in the context", function () {
Expand All @@ -72,7 +85,7 @@

it("should set global variables", function () {
_debugger.start();
expect(window._test_global).to.be("apple");
expect(self._test_global).to.be("apple");
});

it("shouldn't set local variables on the context", function () {
Expand Down Expand Up @@ -548,7 +561,7 @@
_debugger.stepOver();

_debugger.stepIn();
expect(_debugger.line).to.be(2); // for();
expect(_debugger.line).to.be(2); // first line of foo();
_debugger.stepOut();
expect(_debugger.line).to.be(10);
_debugger.stepOver();
Expand Down Expand Up @@ -1196,7 +1209,7 @@
});

it("should work with mulitple variables declarations + instantiation on the same line", function () {
code = getFunctionBody(function () {
var code = getFunctionBody(function () {
var a = 5, b = 10;
x = a;
y = b;
Expand Down Expand Up @@ -1880,7 +1893,7 @@

describe("better loops", function () {
it("should do something", function () {
code = getFunctionBody(function () {
var code = getFunctionBody(function () {
for (var i = 0, j = 0; i * j < 10; i++, j += 1) {
console.log(i + " * " + j + " = " + (i*j));
}
Expand Down
14 changes: 14 additions & 0 deletions test/processing-debugger-spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
var self = typeof window !== "undefined" ? window : global;

if (typeof require !== "undefined") {
var sinon = require("sinon");
var expect = require("expect.js");
require("../node_modules/regenerator/runtime.js");

self.Debugger = require("../build/debugger.js");
var ProcessingDebugger = require("../build/processing-debugger.js");
var getFunctionBody = require("./test_utils.js").getFunctionBody;
}

/*global describe, it, beforeEach, afterEach */

[false, true].forEach(function (nativeGenerators) {
var title = nativeGenerators ?
"Processing Debugger (Native Generators)" :
"Processing Debugger (Regenerator Generators)";

var fill, rect, print, image;

describe(title, function () {

var _debugger, context;
Expand Down
3 changes: 2 additions & 1 deletion test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</head>
<body>
<div id="mocha"></div>
<script src="../node_modules/regenerator/runtime.js"></script>
<!-- build files -->
<script src="../build/debugger.js"></script>
<script src="../build/processing-debugger.js"></script>
Expand All @@ -19,7 +20,7 @@
</body>
</html>
<script src="debugger-spec.js"></script>
<!--<script src="processing-debugger-spec.js"></script>-->
<script src="processing-debugger-spec.js"></script>
<script src="array-spec.js"></script>
<script>

Expand Down
6 changes: 6 additions & 0 deletions test/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ var getFunctionBody = function(func) {
var end = funcString.lastIndexOf("}");
return funcString.substring(start, end);
};

if (typeof module !== "undefined") {
module.exports = {
getFunctionBody
};
}

0 comments on commit 957b392

Please sign in to comment.