vows-fluent.js

Github Repo

var vows = require("vows"),
	pd = require("pd");

An object that can be started

var StartAble = {

start a topic with defaults.

	"start": function _start(parent) {
		this._parent = parent;
		this._batch = parent._batch;
		return this;
	}
};

An object that can be set

var SetAble = {

sets the value of the object

	"set": function _set(value) {
		this._val = value;
		return this;
	}
};

An object that can end contains a value

var ValueEndAble = {

return the value

	"end": function _end() {
		return this._val;
	}
};

An object which can navigate up it's chain.

var ParentAble = {

Returns the parent context

	"parent": function _parent() {
		return this._parent;
	},

returns the batch this context belongs to

	"batch": function _batch() {
		return this._batch;
	},

returns the suite this context belongs to

	"suite": function _suite() {
		return this._batch._suite;
	}
};

An object which can create contexts

var ContextAble = {

Create a Context and add it to this object with the name text. Returns the Context.

	"context": function _context(text) {
		var c = exports.createContext(this);
		this._val[text] = c;
		return c;
	}
};

Topic object. This is an abstraction around Vows topics. Created for ease of extension development

exports.Topic = pd.object(pd.merge(ParentAble, SetAble, StartAble, ValueEndAble));

Vow object. This is an abstraction around Vows vows. Created for ease of extension development

exports.Vow = pd.object(pd.merge({

name the vow

	"name": function _name(name) {
		this._name = name;
		return this;
	}
}, ParentAble, SetAble, StartAble, ValueEndAble));

Context object. This is an abstraction around Vows contexts

exports.Context = pd.object(pd.merge({

start a context with defaults

	"start": function _start(parent) {
		this._val = {};
		this._vows = [];
		this._parent = parent;
		this._batch = parent._batch;
		return this;
	},

Sets the topic of the context to be topic Or returns a Topic if no topic was passed.

	"topic": function _topic(topic) {
		if (topic) {
			this._val.topic = topic;
			return this;
		} else {
			var t = exports.createTopic(this);
			this._val.topic = t;
			return t;	
		}
	},

Adds a vow to the contex with the name text and value vow or returns a Vow if no vow was passed

	"vow": function _vow(text, vow) {
		if (vow) {
			this._val[text] = vow;	
			return this;
		} else {
			var v = exports.createVow(this);
			this._vows.push(v);
			if (text) {
				v.name(text);
			}
			return v;
		}
	},

end the context abstractions writing a Vow context to _val This will call end recursively if it exists

	"end": function _end() {
		var v = this._batch.end.call(this);
		this._vows.forEach(function(vow) {
			v[vow._name] = vow.end();
		});
		return v;
	}
}, ContextAble, ParentAble));

A representation of a vows batch. A vows batch just contains a bunch of contexts

exports.Batch = pd.object(pd.merge({

start a batch with defaults

	"start": function _start(suite) {
		this._val = {};
		this._suite = this._parent = suite;
		this._batch = this;
		return this;
	},

end the object. This returns the Object that vow expects to be an object. This will call end recursively if it exists.

	"end": function _end() {
		var value = {}, val = this._val;
		Object.keys(val).forEach(function(key) {
			if (val[key].end) {
				value[key] = val[key].end();
			} else {
				value[key] = val[key];
			}
		});
		return value;
	}
}, ContextAble, ParentAble));

A representation of a vows Suite. Every application should onhly have one suite.

exports.Suite = {

start the suite with defaults

	"start": function _start(name) {
		this._val = [];
		this._suite = vows.describe(name);
		return this;
	},

Creates a batch and add's it to the suite. Returns the batch

	"batch": function _batch() {
		var b = exports.createBatch(this);
		this._val.push(b);
		return b;
	},

end the suite and call export on the vows suite

	"export": function _export(m, options) {
		var s = this.end();
		return s.export(m, options);
	},

A macro for JSLint

	"exportTo": function _exportTo(m, options) {
		return this.export(m, options);
	},

end the suite and call run on the vows suite

	"run": function _run(options, callback) {
		var s = this.end();
		return s.run(options, callback);	
	},

end the suite returning a vows Suite.

	"end": function _end() {
		this._val.forEach((function _addBatches(batch) {
			this._suite.addBatch(batch.end());
		}).bind(this));
		return this._suite;
	}
};

Generate create functions for each object. Export the objects themself. export the create functions by name. Also export lowercase versions of the name as macros for the create function

["Batch", "Vow", "Context", "Topic", "Suite"].forEach(function(name) {
	exports["create" + name] = function(obj) {
		return pd.object(exports[name]).start(obj);	
	};
	var lowercase = name.charAt(0).toLowerCase() + name.slice(1, name.length);
	exports[lowercase] = exports["create" + name];
});