Skip to content

Commit

Permalink
Refactor lib files.
Browse files Browse the repository at this point in the history
Refresh tests for lib/utils.
Add test .jshintrc file.
Switch tests to Mocha.
  • Loading branch information
mattyod committed Jul 2, 2014
1 parent 342a26e commit 68a02af
Show file tree
Hide file tree
Showing 17 changed files with 288 additions and 79 deletions.
23 changes: 23 additions & 0 deletions lib/build.js
@@ -0,0 +1,23 @@
'use strict';

var index = require('./index'),
compile = require('./utils/compile'),
render = require('./utils/render');

module.exports = function (config) {
var template,
fileName,
compiled = compile.call(this, config);

render.call(this, config, compiled);

if (config.index) {
template = compiled['index.' + config.schemas.suffix];
fileName = 'index.' + config.target.suffix;

this.clipboard.files[fileName] = template(index.call(this, config));
}

delete this.clipboard.files.templates;

};
14 changes: 4 additions & 10 deletions lib/index.js
Expand Up @@ -5,13 +5,7 @@ var _ = require('underscore'),

module.exports = function (config) {
var index = {
files: {},
incrementHeader: function (tags) {
var number = (parseInt(tags[0].match(/[0-9]/), 10) + 1);
tags[0] = '<h' + number + '>';
tags[1] = '</h' + number + '>';
return tags;
}
files: {}
};

var itterate = function (obj, basePath, target, isChild) {
Expand All @@ -27,9 +21,9 @@ module.exports = function (config) {
}
}
});
}
};

itterate(config.files, '', index.files);
itterate(this.clipboard.files, '', index.files);

return index;
}
};
4 changes: 2 additions & 2 deletions lib/matic.js
Expand Up @@ -4,7 +4,7 @@ var rightClick = require('rightClick'),
fs = require('fs'),
eventEmitter = require('events').EventEmitter,
merge = require('./merge'),
render = require('./render');
build = require('./build');

module.exports = function (config) {
var emitter = new eventEmitter(),
Expand All @@ -16,7 +16,7 @@ module.exports = function (config) {
rightClick('./', 'utf8')
.copy(files, suffix)
.tap(merge, config.schemas.indent)
.tap(render, config)
.tap(build, config)
.paste(config.target.path, true);

rightClick('./')
Expand Down
16 changes: 3 additions & 13 deletions lib/merge.js
Expand Up @@ -3,7 +3,7 @@
var _ = require('underscore'),
log = require('col'),
pointy = require('./utils/pointy'),
map = require('./utils/mapParents');
map = require('./utils/map-parents');

module.exports = function (indent) {
var ids = {};
Expand All @@ -13,22 +13,12 @@ module.exports = function (indent) {
_.each(obj, function (val, key) {
var subSchema;

// Attempt to resolve the $ref within the parent schema
var inline = function () {
return ids[schema][val] || false;
};

// Attempt to resolve the $ref against the clipboard/filesystem
var local = function () {
return pointy(this.clipboard.files.schemas, val, schema);
}.bind(this);

if (key === '$ref') {
if (val.match('http://')) {
log.warn('currently unable to resolve url based refs:');
log.info(parentKey + '.' + key + ':', val);
} else {
subSchema = inline() || local();
subSchema = ids[schema][val] || pointy(this.clipboard.files.schemas, val, schema);

if (subSchema) {
parentObj[parentKey] = merge(subSchema.obj, subSchema.path);
Expand All @@ -40,7 +30,7 @@ module.exports = function (indent) {

if (_.isArray(val)) {
var arr = val;
val = [];
val = [];

_.each(arr, function (object, index) {
if (_.isObject(object)) {
Expand Down
45 changes: 0 additions & 45 deletions lib/render.js

This file was deleted.

20 changes: 20 additions & 0 deletions lib/utils/compile.js
@@ -0,0 +1,20 @@
'use strict';

var _ = require('underscore');

module.exports = function (config) {
var engine = require(config.templates.lib),
templateMatcher = new RegExp(config.templates.suffix + '$'),
compiled = {};

_.each(this.clipboard.files.templates, function (template, name) {
if (name.match(templateMatcher)) {
name = name.replace(templateMatcher, config.schemas.suffix);
compiled[name] = engine.compile(template, {
filename: config.templates.path
});
}
});

return compiled;
};
File renamed without changes.
9 changes: 1 addition & 8 deletions lib/utils/pointy.js
Expand Up @@ -20,15 +20,8 @@ module.exports = function (parentObj, subSchema, schema) {
}

fragments = dir.concat(fragments);
// console.log(schema);
// console.log(subSchema);
// console.log(fragments);
_.each(fragments, function (fragment) {

if (typeof obj[fragment] === 'string') {
obj[fragment] = obj[fragment];
}

_.each(fragments, function (fragment) {
obj = obj[fragment] ? obj[fragment] : false;
});

Expand Down
21 changes: 21 additions & 0 deletions lib/utils/render.js
@@ -0,0 +1,21 @@
'use strict';

var _ = require('underscore');

module.exports = function (config, compiled) {
var template,
fileName,
schemaMatcher = new RegExp(config.schemas.suffix + '$'),
defaultTemplate = config.templates.file + '.' + config.schemas.suffix;

_.each(this.clipboard.files.schemas, function (schema, name) {
if (typeof schema === 'string') {
template = compiled[name] || compiled[defaultTemplate];
fileName = name.replace(schemaMatcher, config.target.suffix);
schema = JSON.parse(schema);
schema.config = config;
this.clipboard.files[fileName] = template(schema);
}
}.bind(this));

};
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -30,12 +30,14 @@
"sinon": "1.10.0",
"mocha": "1.19.0",
"chai": "1.9.1",
"sinon-chai": "2.5.0",
"jade": "1.3.1",
"jshint": "2.5.1"
},
"scripts": {
"unit": "nodeunit tests",
"unitold": "nodeunit tests",
"lint": "./node_modules/jshint/bin/jshint ./",
"unit": "mocha test/unit",
"test": "npm run lint && npm run unit"
},
"engines": {
Expand Down
38 changes: 38 additions & 0 deletions test/.jshintrc
@@ -0,0 +1,38 @@
{
"predef": [
"beforeEach",
"afterEach",
"before",
"after",
"describe",
"it",
"sandbox",
"expect"
],

"node": true,

"bitwise" : true,
"camelcase" : true,
"curly" : true,
"eqeqeq" : true,
"immed" : true,
"indent" : 2,
"latedef" : "nofunc",
"newcap" : true,
"noarg" : true,
"noempty" : true,
"nonew" : true,
"quotmark" : "single",
"undef" : true,
"unused" : true,
"strict" : true,
"trailing" : true,
"maxdepth" : 3,
"maxstatements" : 10,
"maxcomplexity" : 5,
"expr" : true,

"boss" : true

}
24 changes: 24 additions & 0 deletions test/common.js
@@ -0,0 +1,24 @@
'use strict';

global.sinon = require('sinon');
global.should = require('chai')
.use(require('sinon-chai'))
.should();

(function () {
global.sandbox = function (fn) {

beforeEach(function () {
global.sandbox = global.sinon.sandbox.create({
injectInto: null,
properties: ['spy', 'stub', 'mock']
});
});

afterEach(function () {
global.sandbox.restore();
});

return fn;
};
})();
3 changes: 3 additions & 0 deletions test/mocha.opts
@@ -0,0 +1,3 @@
--require test/common.js
--recursive
--reporter spec
37 changes: 37 additions & 0 deletions test/unit/lib/utils/compile.test.js
@@ -0,0 +1,37 @@
'use strict';

var compile = require('../../../../lib/utils/compile');

describe('lib/utils/compile', sandbox(function () {
var config,
compiled;

beforeEach(function () {
config = {
templates: {
lib: 'jade',
suffix: 'jade'
},
schemas: {
suffix: 'json'
}
};

this.clipboard = {
files: {
templates: {
'foo.jade': 'h1 foo',
'bar.jade': 'h1 bar'
}
}
};

compiled = compile.call(this, config);
});

it('compiles templates with the schema suffix', function () {
(typeof compiled['foo.json']).should.equal('function');
(typeof compiled['bar.json']).should.equal('function');
});

}));
29 changes: 29 additions & 0 deletions test/unit/lib/utils/map-parents.test.js
@@ -0,0 +1,29 @@
'use strict';

var map = require('../../../../lib/utils/map-parents'),
_ = require('underscore');

describe('lib/utils/map-parents', sandbox(function () {
var object, mapping;

beforeEach(function () {
object = {
foo: { id: 'foobar', nested: { id: 'xxx' } },
bar: { id: 'qwerty' },
baz: { notId: 'bar' }
};

mapping = map(object, 'id');
});

it('returns a flat object keyed by the requested inner key', function () {
mapping.foobar.should.deep.equal(object.foo);
mapping.xxx.should.deep.equal(object.foo.nested);
mapping.qwerty.should.deep.equal(object.bar);
});

it('only maps the child objects containing the requested keys', function () {
_.keys(mapping).should.deep.equal([ 'foobar', 'xxx', 'qwerty' ]);
});

}));

0 comments on commit 68a02af

Please sign in to comment.