Skip to content

Commit

Permalink
whitespace clean and added async support.. not enough test coverage, …
Browse files Browse the repository at this point in the history
…however
  • Loading branch information
ibolmo committed Nov 5, 2009
1 parent f00d47f commit 76592d0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
38 changes: 31 additions & 7 deletions injectigator.js
Expand Up @@ -12,7 +12,7 @@
var Injectigator = this.Injectigator = {};

// Local (private) variable.
var prevNode;
var prevNode, prevANode;

/**
* Initializes the ROOT and resets the previous known Node to the ROOT.
Expand All @@ -21,6 +21,10 @@ Injectigator.initialize = function() {
// TODO(ibolmo): Ensure previous ROOT is properly garbage collected.
prevNode = this.ROOT = this.fn();
prevNode.$parent = prevNode;

// TODO(ibolmo): Ensure previous AROOT is properly garbage collected.
prevANode = this.AROOT = this.fn();
prevANode.$parent = prevNode;
};


Expand All @@ -32,6 +36,14 @@ Injectigator.getPreviousNode = function() {
};


/**
* @returns {Function} The previous async. Injectigator node.
*/
Injectigator.getPreviousAsyncNode = function() {
return prevANode;
};


/**
* Whether the passed in function is an InjectigatorNode.
* @param {Function} node A function that may be a {@see #InjectigatorNode}.
Expand All @@ -54,18 +66,25 @@ Injectigator.enter = function(node) {

node.$called++;

var prev = (node.$periodical || node.$delayed) ? prevANode : prevNode;

// If the previous node/function hadn't finished executing, then this new
// node is a sub-node/fn of the previous. NOTE(ibolmo): for the async. case
// the nodes are treated differently.
var parent = (!prevNode.$end) ? prevNode : prevNode.$parent;
var parent = (!prev.$end) ? prev : prev.$parent;
if (!parent.$first) {
parent.$first = node;
}
parent.$last = node;
node.$parent = parent;

node.$prev = prevNode;
prevNode = prevNode.$next = node;
node.$prev = prev;
if (node.$periodical || node.$delayed) {
prevANode = prev.$next = node;
} else {
prevNode = prev.$next = node;
}

return this;
};

Expand All @@ -81,8 +100,14 @@ Injectigator.exit = function(node, result) {
node.$end = new Date;
node.$elapsed[node.$called - 1] = node.$end - node.$start;

if (prevNode.$parent == node) {
prevNode = node;
var prev = (node.$periodical || node.$delayed) ? prevANode : prevNode;

if (prev.$parent == node) {
if (node.$periodical || node.$delayed) {
prevANode = node;
} else {
prevNode = node;
}
}
return result;
};
Expand All @@ -109,7 +134,6 @@ Injectigator.fn = function(fn) {
// Initialization
Injectigator.initialize();


// Override setInterval or setTimeout. Helpful for the visualization stage.
var oI = this.setInterval;
this.setInterval = function(fn, interval){
Expand Down
31 changes: 31 additions & 0 deletions injectigator_test.js
Expand Up @@ -3,6 +3,9 @@
* {@ link http://code.google.com/p/js-test-driver/wiki/GettingStarted} on how
* to get started.
*
* NOTE(ibolmo): You'll need to have ../jsunit/jsUnitMockTimeout.js from
* http://bit.ly/jsUnitMockTimeout.
*
* @author ibolmo@gmail.com (Olmo Maldonado)
*/

Expand All @@ -13,6 +16,11 @@ TestCase('Injectigator', {
Injectigator.initialize();
},

testMockClock: function() {
assertTrue('ensure jstd loaded Clock mock first',
(setTimeout+'').indexOf('Injectigator') != -1);
},

testIsNode: function() {
var node = function(){};
assertFalse('A normal function is not an Injectigator node.',
Expand Down Expand Up @@ -211,6 +219,29 @@ TestCase('Injectigator', {
assertEquals('childNode has rootNode as parent',
rootNode,
childNode.$parent);
},

testSetTimeoutFns: function() {
var called = 0;
var node = Injectigator.fn(function() {
called++;
});

setTimeout(node, 1000);
Clock.tick(1000);

assertEquals('function gets called normally', 1, called);

// TODO(ibolmo): Use a public method when available.
assertTrue('Node should be marked as delayed', node.$delayed);

// The node should not be part of the normal execution path.
assertFalse(!!Injectigator.ROOT.$first);
assertFalse(!!Injectigator.ROOT.$last);

// The node should be part of the async root (AROOT) path.
assertEquals(node, Injectigator.AROOT.$first);
assertEquals(node, Injectigator.AROOT.$last);
}

});
1 change: 1 addition & 0 deletions jsTestDriver.conf
@@ -1,6 +1,7 @@
server: http://localhost:9876

load:
- '../jsunit/jsUnitMockTimeout.js'
- '*.js'

exclude:
Expand Down

0 comments on commit 76592d0

Please sign in to comment.