Skip to content

Commit

Permalink
Merge pull request #72 from olegskl/refacto-adapter-cleanup
Browse files Browse the repository at this point in the history
refacto(adapter): cleanup
  • Loading branch information
maksimr committed Jan 7, 2015
2 parents e62428d + e22f781 commit 6d5455e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 69 deletions.
109 changes: 53 additions & 56 deletions src/adapter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* jshint globalstrict: true */
'use strict';

/**
* Decision maker for whether a stack entry is considered relevant.
* @param {String} entry Error stack entry.
Expand Down Expand Up @@ -93,76 +96,62 @@ function formatFailedStep(step) {
}


var indexOf = function(collection, item) {
if (collection.indexOf) {
return collection.indexOf(item);
}

for (var i = 0, l = collection.length; i < l; i++) {
if (collection[i] === item) {
return i;
}
}

return -1;
};


var SuiteNode = function(name, parent) {
function SuiteNode(name, parent) {
this.name = name;
this.parent = parent;
this.children = [];

this.addChild = function(name) {
this.addChild = function (name) {
var suite = new SuiteNode(name, this);
this.children.push(suite);
return suite;
};
};
}


var getAllSpecNames = function(topSuite) {
var specNames = {};
function processSuite(suite, pointer) {
var child;
var childPointer;

function processSuite(suite, pointer) {
var child;
var childPointer;

for (var i = 0; i < suite.children.length; i++) {
child = suite.children[i];

if (child.children) {
childPointer = pointer[child.description] = {_: []};
processSuite(child, childPointer);
} else {
if (!pointer._) {
pointer._ = [];
}
pointer._.push(child.description);
for (var i = 0; i < suite.children.length; i++) {
child = suite.children[i];

if (child.children) {
childPointer = pointer[child.description] = {_: []};
processSuite(child, childPointer);
} else {
if (!pointer._) {
pointer._ = [];
}
pointer._.push(child.description);
}
}
}


function getAllSpecNames(topSuite) {
var specNames = {};

processSuite(topSuite, specNames);

return specNames;
};
}


/**
* Very simple reporter for Jasmine.
*/
var KarmaReporter = function(tc, jasmineEnv) {
function KarmaReporter(tc, jasmineEnv) {

var currentSuite = new SuiteNode();

/**
* @param suite
* @returns {boolean} Return true if it is system jasmine top level suite
*/
var isTopLevelSuite = function (suite) {
function isTopLevelSuite(suite) {
return suite.description === 'Jasmine_TopLevel_Suite';
};

var currentSuite = new SuiteNode();
}

/**
* Jasmine 2.0 dispatches the following events:
Expand All @@ -175,7 +164,7 @@ var KarmaReporter = function(tc, jasmineEnv) {
* - specDone
*/

this.jasmineStarted = function(data) {
this.jasmineStarted = function (data) {
// TODO(vojta): Do not send spec names when polling.
tc.info({
total: data.totalSpecsDefined,
Expand All @@ -184,21 +173,21 @@ var KarmaReporter = function(tc, jasmineEnv) {
};


this.jasmineDone = function() {
this.jasmineDone = function () {
tc.complete({
coverage: window.__coverage__
});
};


this.suiteStarted = function(result) {
this.suiteStarted = function (result) {
if (!isTopLevelSuite(result)) {
currentSuite = currentSuite.addChild(result.description);
}
};


this.suiteDone = function(result) {
this.suiteDone = function (result) {
// In the case of xdescribe, only "suiteDone" is fired.
// We need to skip that.
if (result.description !== currentSuite.name) {
Expand All @@ -209,12 +198,12 @@ var KarmaReporter = function(tc, jasmineEnv) {
};


this.specStarted = function(specResult) {
this.specStarted = function (specResult) {
specResult.startTime = new Date().getTime();
};


this.specDone = function(specResult) {
this.specDone = function (specResult) {
var skipped = specResult.status === 'disabled' || specResult.status === 'pending';

var result = {
Expand Down Expand Up @@ -244,16 +233,24 @@ var KarmaReporter = function(tc, jasmineEnv) {
tc.result(result);
delete specResult.startTime;
};
};

}

var createStartFn = function(tc, jasmineEnvPassedIn) {
return function(config) {
// we pass jasmineEnv during testing
// in production we ask for it lazily, so that adapter can be loaded even before jasmine
var jasmineEnv = jasmineEnvPassedIn || window.jasmine.getEnv();
/**
* Karma starter function factory.
*
* This function is invoked from the wrapper.
* @see adapter.wrapper
*
* @param {Object} karma Karma runner instance.
* @param {Object} [jasmineEnv] Optional Jasmine environment for testing.
* @return {Function} Karma starter function.
*/
function createStartFn(karma, jasmineEnv) {
// This function will be assigned to `window.__karma__.start`:
return function () {
jasmineEnv = jasmineEnv || window.jasmine.getEnv();

jasmineEnv.addReporter(new KarmaReporter(tc, jasmineEnv));
jasmineEnv.addReporter(new KarmaReporter(karma, jasmineEnv));
jasmineEnv.execute();
};
};
}
18 changes: 5 additions & 13 deletions test/adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
These tests are executed in browser.
*/

/* jshint globalstrict: true */
'use strict';

describe('jasmine adapter', function(){
var Karma = window.__karma__.constructor;

Expand Down Expand Up @@ -247,24 +250,13 @@ describe('jasmine adapter', function(){
});
it('should split by newline and return all values for which isRelevantStackEntry returns true', function () {
isRelevantStackEntry = jasmine.createSpy('isRelevantStackEntry').andReturn(true);
expect(getRelevantStackFrom('a\nb\nc')).toEqual(['a', 'b', 'c'])
expect(getRelevantStackFrom('a\nb\nc')).toEqual(['a', 'b', 'c']);
});
it('should not return any values for which isRelevantStackEntry returns false', function () {
isRelevantStackEntry = jasmine.createSpy('isRelevantStackEntry').andReturn(false);
expect(getRelevantStackFrom('a\nb\nc')).toEqual([])
expect(getRelevantStackFrom('a\nb\nc')).toEqual([]);
});
});


describe('indexOf', function(){
it('should return index of given item', function(){
var collection = [ {}, {}, {} ];
collection.indexOf = null; // so that we can test it even on

expect(indexOf(collection, {})).toBe(-1);
expect(indexOf(collection, collection[1])).toBe(1);
expect(indexOf(collection, collection[2])).toBe(2);
});
});

});

0 comments on commit 6d5455e

Please sign in to comment.