vows-fluent.js | |
---|---|
var vows = require("vows"),
Trait = require("traits").Trait; | |
utility create function | var create = function(trait) {
return Object.create(Object.prototype, trait);
}; |
An object that can be started | var StartAble = Trait({ |
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 = Trait({ |
sets the value of the object | "set": function _set(value) {
this._val = value;
return this;
}
}); |
An object that can end and contains an object. | var ObjectEndAble = Trait({ |
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;
}
}); |
An object that can end contains a value | var ValueEndAble = Trait({ |
return the value | "end": function _end() {
return this._val;
}
}); |
An object which can navigate up it's chain. | var ParentAble = Trait({ |
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 = Trait({ |
Create a Context and add it to this object
with the name | "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 = create(Trait.compose(ParentAble, SetAble, StartAble, ValueEndAble)); |
Vow object. This is an abstraction around Vows vows. Created for ease of extension development | exports.Vow = create(Trait.compose(ParentAble, SetAble, StartAble, ValueEndAble)); |
Context object. This is an abstraction around Vows contexts | exports.Context = create(Trait.compose(Trait({ |
start a context with defaults | "start": function _start(parent) {
this._val = {};
this._parent = parent;
this._batch = parent._batch;
return this;
}, |
Sets the topic of the context to be | "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 | "vow": function _vow(text, vow) {
if (vow) {
this._val[text] = vow;
return this;
} else {
var v = exports.createVow(this);
this._val[text] = v;
return v;
}
}
}), ContextAble, ObjectEndAble, ParentAble)); |
A representation of a vows batch. A vows batch just contains a bunch of contexts | exports.Batch = create(Trait.compose(Trait({ |
start a batch with defaults | "start": function _start(suite) {
this._val = {};
this._suite = this._parent = suite;
this._batch = this;
return this;
}
}), ContextAble, ObjectEndAble, 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 create(Trait(exports[name])).start(obj);
};
var lowercase = name.charAt(0).toLowerCase() + name.slice(1, name.length);
exports[lowercase] = exports["create" + name];
});
|