Permalink
Browse files

fixed and improved processor and assertion injection

 * improved tests
 * added topic tests
 * removed Assert.inject() and Topic.inject()
 * improved brokeContextMock
  • Loading branch information...
1 parent f6b432c commit 209244d1ce9620297d23566ccc4dd4fe7a0cb12a zyndiecate committed Dec 7, 2011
View
@@ -10,11 +10,6 @@
var log = require('logerize');
/*
- * Module variables.
- */
-var customAssertions = {};
-
-/*
* Class definition.
*/
function Assert() {};
@@ -37,26 +32,16 @@ function Assert() {};
*/
Assert.prototype.create = function create(vowsContext, brokeContext) {
Object.keys(brokeContext.assert).forEach(function(brokeAssertionName) {
- var brokeAssertion = brokeContext.assert[brokeAssertionName];
+ var assertion = {
+ key: brokeAssertionName,
+ value: brokeContext.assert[brokeAssertionName]
+ };
- if(typeof brokeAssertion === 'function') vowsContext[brokeAssertionName] = brokeAssertion;
- else Assert.merge(vowsContext, brokeAssertionName, brokeAssertion);
+ if(typeof assertion.value === 'function') vowsContext[assertion.key] = assertion.value;
+ else Assert.merge(vowsContext, brokeContext, assertion);
});
};
-/*
- * Set injected custom assertions as local variable.
- *
- * @param object injectedAssertions, injected custom assertions.
- *
- * @return object customAssertions, injected custom assertions.
- */
-Assert.prototype.inject = function inject(injectedAssertions) {
- customAssertions = injectedAssertions;
-
- return customAssertions;
-};
-
/*
@@ -71,16 +56,15 @@ Assert.prototype.inject = function inject(injectedAssertions) {
* Merge custom assertions into a vows context.
*
* @param object vowsContext, the vows context to add the assertions.
- * @param string brokeAssertionName, name of the custom assertion to merge.
- * @param !function brokeAssertion, the expected parameter for a custom assertion.
+ * @param object brokeContext, a given broke context.
+ * @param object assertion, key value pair of a custom asserrtion.
*/
-Assert.merge = function merge(vowsContext, brokeAssertionName, brokeAssertion) {
- var err = 'Invalid custom assertion "' + brokeAssertionName + '"'
- , customAssertion = customAssertions[brokeAssertionName];
-
- if(typeof customAssertion !== 'function') return log.error(new Error(err).stack);
+Assert.merge = function merge(vowsContext, brokeContext, assertion) {
+ var err = 'Invalid custom assertion "' + assertion.key + '"'
+ , customAssertions = brokeContext.customAssertions[assertion.key];
- var methods = customAssertion(brokeAssertion);
+ if(typeof customAssertions === 'function') var methods = customAssertions(assertion.value);
+ else return log.error(new Error(err).stack);
if(typeof methods !== 'object' || Array.isArray(methods)) return log.error(new Error(err).stack);
View
@@ -95,10 +95,7 @@ Broke.prototype.run = function run(module, config) {
if(this.batches.length === 0) var err = 'No batches defined.';
if(typeof err === 'string') return log.error(new Error(err).stack);
- if(typeof config === 'object') {
- if(typeof config.processors === 'object') this.processors = Topic.inject(config.processors);
- if(typeof config.assertions === 'object') this.assertions = Assert.inject(config.assertions);
- }
+ this.config = config || {};
batches.call(this);
@@ -154,7 +151,7 @@ function batches() {
var self = this;
self.batches.forEach(function(batch) {
- self.suite.addBatch(TestCase.createBatch(batch));
+ self.suite.addBatch(TestCase.createBatch(batch, self.config));
});
};
View
@@ -39,7 +39,7 @@ function TestCase() {};
*
* @return object vowsBatch, a valid vows batch.
*/
-TestCase.prototype.createBatch = function createBatch(brokeBatch) {
+TestCase.prototype.createBatch = function createBatch(brokeBatch, config) {
var vowsBatch = {};
Object.keys(brokeBatch).forEach(function(contextName) {
@@ -50,10 +50,13 @@ TestCase.prototype.createBatch = function createBatch(brokeBatch) {
if(typeof brokeContext.assert !== 'object') var err = 'Invalid assert phase';
if(typeof err === 'string') return log.error(new Error(err).stack);
+ // Setup the broke context.
brokeContext.name = contextName;
brokeContext.emitter = new EventEmitter;
+ brokeContext.customProcessors = config.processors || {};
+ brokeContext.customAssertions = config.assertions || {};
- TestCase.createContext(vowsBatch, brokeContext, function(contextName, context) {
+ createContext(vowsBatch, brokeContext, function(contextName, context) {
vowsBatch[contextName] = context;
});
});
@@ -65,7 +68,7 @@ TestCase.prototype.createBatch = function createBatch(brokeBatch) {
/*
*
- * Class functions.
+ * Private functions.
*
*/
@@ -79,7 +82,7 @@ TestCase.prototype.createBatch = function createBatch(brokeBatch) {
* @param object brokeContext, a given broke context.
* @param function cb, callback to execute.
*/
-TestCase.createContext = function createContext(vowsBatch, brokeContext, cb) {
+function createContext(vowsBatch, brokeContext, cb) {
var vowsContext = {};
Manipulation.config(brokeContext);
View
@@ -15,11 +15,6 @@ var log = require('logerize');
var Manipulation = require('./manipulation.js');
/*
- * Module variables.
- */
-var customProcessors = {};
-
-/*
* Class definition.
*/
function Topic() {};
@@ -44,16 +39,16 @@ function Topic() {};
* @param object brokeContext, a given broke context.
*/
Topic.prototype.create = function create(vowsContext, brokeContext) {
- vowsContext.topic = function topic() {
- var self = this;
+ // Execute first given process phases.
+ var processPhaseName = Object.keys(brokeContext.process)[0]
+ , processPhase = brokeContext.customProcessors[processPhaseName];
- // Execute first given process phases.
- var processPhaseName = Object.keys(brokeContext.process)[0]
- , processPhase = customProcessors[processPhaseName];
+ if(typeof processPhase !== 'function') {
+ return log.error(new Error('Invalid process phase "' + processPhaseName + '".').stack);
+ }
- if(typeof processPhase === 'undefined') {
- return log.error(new Error('Invalid process phase "' + processPhaseName + '".').stack);
- }
+ vowsContext.topic = function topic() {
+ var self = this;
self.callback = Topic.callback.call(self, brokeContext, self.callback);
@@ -64,19 +59,6 @@ Topic.prototype.create = function create(vowsContext, brokeContext) {
};
};
-/*
- * Set injected custom processors as local variable.
- *
- * @param object injectedProcessors, injected custom processors.
- *
- * @return object customProcessors, injected custom processors.
- */
-Topic.prototype.inject = function inject(injectedProcessors) {
- customProcessors = injectedProcessors;
-
- return customProcessors;
-};
-
/*
@@ -88,16 +70,6 @@ Topic.prototype.inject = function inject(injectedProcessors) {
/*
- * Just throws a error.
- *
- * @param object vowsContext, the vows context to add the topic.
- * @param object brokeContext, a given broke context.
- */
-Topic.topicError = function topicError(vowsContext, brokeContext) {
- this.callback('Invalid process. Cannot execute test case.');
-};
-
-/*
* Wrap vows callback function for better control flow.
*
* @param function cb, vows "this.callback".
View
@@ -1,6 +1,6 @@
{
"name": "broke",
- "version": "0.0.3",
+ "version": "0.0.4",
"description": "Vowsjs layer for flexible unit and integration tests.",
"keywords": ["testframework", "unit tests", "tests", "integration tests"],
"author": "Tim Schindler <tim.schindler@adcloud.com>",
View
@@ -12,84 +12,75 @@ var Assert = require('../../libs/assert.js')
vows
.describe('assert')
- /*
- * Test Assert.create()
- */
+ // Test Assert.create()
.addBatch({
'call create() with normal assertions in a broke context': {
topic: function() {
var vowsContext = {};
- Assert.create(vowsContext, brokeContextMock[0]['init the test suite']);
+ Assert.create(vowsContext, new brokeContextMock.create('init the test suite'));
this.callback(undefined, vowsContext);
},
'should create normal assertions in a vows context': function(err, vowsContext) {
- var keys = Object.keys(vowsContext)
- , assertion = 'test suite initialized';
+ var assertion = 'test suite initialized';
- assert.isNull(err);
assert.isObject(vowsContext);
- assert.length(keys, 1);
- assert.equal(keys[0], assertion);
+ assert.deepEqual(Object.keys(vowsContext), [assertion]);
assert.isFunction(vowsContext[assertion]);
}
},
'call create() with used custom assertions that are not injected': {
topic: function() {
- var vowsContext = {};
+ var vowsContext = {}
+ , brokeContext = new brokeContextMock.create('test your code');
+
+ brokeContext.customAssertions = {};
- Assert.create(vowsContext, brokeContextMock[1]['test your code']);
+ Assert.create(vowsContext, brokeContext);
this.callback(undefined, vowsContext);
},
'should not create custom assertions in a vows context': function(err, vowsContext) {
- var keys = Object.keys(vowsContext);
-
- assert.isNull(err);
assert.isObject(vowsContext);
- assert.length(keys, 0);
+ assert.deepEqual(Object.keys(vowsContext), []);
}
},
'call create() with used custom assertions that are injected': {
topic: function() {
- var vowsContext = {};
+ var vowsContext = {}
+ , brokeContext = new brokeContextMock.create('test your code');
+
+ brokeContext.customAssertions = customAssertionsMock;
- Assert.inject(customAssertionsMock);
- Assert.create(vowsContext, brokeContextMock[1]['test your code']);
+ Assert.create(vowsContext, brokeContext);
this.callback(undefined, vowsContext);
},
'should create custom assertions in a vows context': function(err, vowsContext) {
- var keys = Object.keys(vowsContext)
- , firstAssertion = 'should respond without errors'
+ var firstAssertion = 'should respond without errors'
, secondAssertion = 'should respond with status code 301';
- assert.isNull(err);
assert.isObject(vowsContext);
- assert.length(keys, 2);
- assert.equal(keys[0], firstAssertion);
- assert.equal(keys[1], secondAssertion);
+ assert.deepEqual(Object.keys(vowsContext), [firstAssertion, secondAssertion]);
assert.isFunction(vowsContext[firstAssertion]);
assert.isFunction(vowsContext[secondAssertion]);
}
},
'call create() using custom assertions that dont return an object': {
topic: function() {
var vowsContext = {}
- , brokeContext = brokeContextMock[3]['test case using another broken custom assertion'];
+ , brokeContext = new brokeContextMock.create('another broken custom assertion');
+
+ brokeContext.customAssertions = customAssertionsMock;
- Assert.inject(customAssertionsMock);
Assert.create(vowsContext, brokeContext);
this.callback(undefined, vowsContext);
},
'should not create custom assertions in a vows context': function(err, vowsContext) {
- var keys = Object.keys(vowsContext);
-
- assert.isNull(err);
assert.isObject(vowsContext);
- assert.length(keys, 0);
+ assert.deepEqual(Object.keys(vowsContext), []);
}
},
})
View
@@ -30,7 +30,7 @@ vows
'call test() with string as first parameter': {
topic: function() {
var firstSuite = Broke.test('first dummy test')
- var secondSuite = Broke.test('second dummy test');
+ , secondSuite = Broke.test('second dummy test');
this.callback(undefined, firstSuite, secondSuite);
},
@@ -248,7 +248,7 @@ vows
this.callback(undefined, suite);
},
'should return a test suite including the defined batch and custom assertions': function(err, suite) {
- var keys = Object.keys(suite.assertions);
+ var keys = Object.keys(suite.config.assertions);
var assertionNames = [
'statusCode',
@@ -260,7 +260,7 @@ vows
assert.isNull(err);
assert.isObject(suite);
assert.isArray(suite.suite.batches);
- assert.isObject(suite.assertions);
+ assert.isObject(suite.config.assertions);
assert.deepEqual(keys, assertionNames);
assert.equal(suite.suite.subject, 'dummy test suite');
}
@@ -279,7 +279,7 @@ vows
this.callback(undefined, suite);
},
'should return a test suite including the defined batch and custom processors': function(err, suite) {
- var keys = Object.keys(suite.processors);
+ var keys = Object.keys(suite.config.processors);
var processorNames = [
'method',
@@ -289,7 +289,7 @@ vows
assert.isNull(err);
assert.isObject(suite);
assert.isArray(suite.suite.batches);
- assert.isObject(suite.processors);
+ assert.isObject(suite.config.processors);
assert.deepEqual(keys, processorNames);
assert.equal(suite.suite.subject, 'dummy test suite');
}
Oops, something went wrong.

0 comments on commit 209244d

Please sign in to comment.