Skip to content

Commit

Permalink
Fixed issues with various kinds of custom builds, added testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
asciidisco committed Nov 23, 2012
1 parent 8c1c16e commit 8f45315
Show file tree
Hide file tree
Showing 16 changed files with 374 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/node_modules/
/fixtures/
.DS_Store
7 changes: 6 additions & 1 deletion .jshintrtc → .jshinttrc
Expand Up @@ -10,5 +10,10 @@
"boss": true,
"eqnull": true,
"node": true,
"es5": true
"es5": true,
globals: {
"exports": true,
"require": true,
"module": true
}
}
7 changes: 7 additions & 0 deletions CHANGELOG
@@ -1,3 +1,10 @@
v0.1.3:
date: 2012-11-23
changes:
- Added testsuite
- Fixed issues regarding the removal of the extend method (Builds without view)
- Fixed issues of the Router only build not working

v0.1.2:
date: 2012-11-22
changes:
Expand Down
111 changes: 71 additions & 40 deletions grunt.js
Expand Up @@ -4,31 +4,6 @@ module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
pkg: '<json:package.json>',
meta: {
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %>\n' +
'------------------------------\n' +
'Build @ <%= grunt.template.today("yyyy-mm-dd") %>\n' +
'Documentation and Full License Available at:\n' +
'<%= pkg.homepage %>\n' +
'<%= pkg.repository.url %>\n' +
'Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>\n\n' +
'Permission is hereby granted, free of charge, to any person obtaining a\n' +
'copy of this software and associated documentation files (the "Software"),\n' +
'to deal in the Software without restriction, including without limitation\n' +
'the rights to use, copy, modify, merge, publish, distribute, sublicense,\n' +
'and/or sell copies of the Software, and to permit persons to whom the\n\n' +
'Software is furnished to do so, subject to the following conditions:\n' +
'The above copyright notice and this permission notice shall be included in\n' +
'all copies or substantial portions of the Software.\n\n' +
'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\n' +
'EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n' +
'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n' +
'IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\n' +
'DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\n' +
'ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\n' +
'IN THE SOFTWARE.*/'
},

test: {
files: ['test/**/*.js']
Expand All @@ -43,28 +18,84 @@ module.exports = function(grunt) {
tasks: 'default'
},

qunit: {
files: [
'test/backbonetestsuite/test_view.html',
'test/backbonetestsuite/test_model.html',
'test/backbonetestsuite/test_collection.html',
'test/backbonetestsuite/test_events.html',
'test/backbonetestsuite/test_router.html',
'test/backbonetestsuite/test_setdomlibrary.html',
'test/backbonetestsuite/test_sync.html',
'test/backbonetestsuite/test_noconflict.html',
]
},

jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
node: true,
es5: true
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"boss": true,
"eqnull": true,
"node": true,
"es5": true
},
globals: {}
globals: {
"exports": true,
"require": true,
"module": true
}
}

});

// Helper tasks, used to create various custtom builds to test against
var combinations = [['View'], ['Model'], ['Events'], ['Collection'], ['Router']];
grunt.registerTask('setUp', function () {
var done = this.async();
var builder = require(__dirname + '/lib/builder').init(grunt);
var steps = combinations.length;
var stepsDone = 0;
var checkDone = function () {
if (steps === stepsDone) {
done();
}
};

// build test fixtures
combinations.forEach(function (part) {
builder.build({
config: {include: part}
}, function (transport) {
grunt.log.writeln('Prepared custom ' + part.join(',') + ' fixture');
grunt.file.write(__dirname + '/fixtures/backbone.custom.' + part.join('.') + '.js', transport.content);
stepsDone++;
checkDone();
});
});

});

grunt.registerTask('tearDown', function () {
var fs = require('fs');
// delete test fixtures
combinations.forEach(function (part) {
grunt.log.writeln('Deleting fixture backbone.custom.' + part.join('.') + '.js');
fs.unlinkSync(__dirname + '/fixtures/backbone.custom.' + part.join('.') + '.js');
});

fs.rmdirSync(__dirname + '/fixtures');
});

// Default task.
grunt.registerTask('default', 'lint test');
grunt.registerTask('default', 'setUp lint test qunit tearDown');

// Default task.
grunt.registerTask('travis', 'lint test');
grunt.registerTask('travis', 'setUp lint test qunit tearDown');
};
51 changes: 43 additions & 8 deletions lib/builder.js
@@ -1,5 +1,3 @@
/*global exports:true, require:true */

/*
* grunt-backbonebuilder
* https://github.com/asciidisco/grunt-backbonebuilder
Expand All @@ -14,12 +12,12 @@
var fs = require('fs');
var _ = null;
var combinations = {
Collection: ['Header', 'Setup', 'Events', 'Model', 'Collection', 'sync', 'Helpers'],
Model: ['Header', 'Setup', 'Events', 'Model', 'sync', 'Helpers'],
Collection: ['Header', 'Setup', 'Events', 'Model', 'Collection', 'Sync', 'Helpers'],
Model: ['Header', 'Setup', 'Events', 'Model', 'Collection', 'Sync', 'Helpers'],
View: ['Header', 'Setup', 'Events', 'View', 'Helpers'],
Router: ['Header', 'Setup', 'Events', 'Router', 'History', 'Helpers'],
Events: ['Header', 'Setup', 'Events', 'Helpers'],
All: ['Header', 'Setup', 'Events', 'Model', 'Collection', 'View', 'History', 'Helpers']
All: ['Header', 'Setup', 'Events', 'Model', 'Collection', 'Router', 'View', 'History', 'Sync', 'Helpers']
};

// extract a part by its character index
Expand All @@ -38,7 +36,7 @@
Router: '// Backbone.Router',
History: '// Backbone.History',
View: '// Backbone.View',
sync: '// Backbone.sync',
Sync: '// Backbone.sync',
Helpers: '// Helpers'
};

Expand All @@ -64,17 +62,54 @@
var allCombinations = [];

parts.forEach(function (part) {
allCombinations.push(combinations[part.toLowerCase() !== 'sync' ? _.str.capitalize(part.toLowerCase()) : part.toLowerCase()]);
allCombinations.push(combinations[_.str.capitalize(part.toLowerCase())]);
});

var neededParts = _.union.apply(_, allCombinations);
var backboneParts = getBackboneParts(backboneSrc);
var finalCombination = _.intersection(combinations.All, neededParts);

finalCombination.forEach(function (part) {
src += backboneParts[part.toLowerCase() !== 'sync' ? _.str.capitalize(part.toLowerCase()) : part.toLowerCase()];
src += backboneParts[_.str.capitalize(part.toLowerCase())];

if (part.toLowerCase() === 'view') {
src = src.replace('Model.extend = Collection.extend = Router.extend = View.extend = extend;', '');
}
});

var extender = '';
if(!_.contains(finalCombination, 'View') && !_.contains(finalCombination, 'view')) {
extender += ' var extend = function (protoProps, classProps) {\n';
extender += ' var child = inherits(this, protoProps, classProps)\n';
extender += ' child.extend = this.extend;\n';
extender += ' return child;\n';
extender += ' };\n\n';
}

// add the extend chain
finalCombination.forEach(function (extendo) {
var lowerEx = extendo.toLowerCase();
if (lowerEx === 'model') {
extender += 'Model.extend = ';
}

if (lowerEx === 'collection') {
extender += 'Collection.extend = ';
}

if (lowerEx === 'router') {
extender += 'Router.extend = ';
}

if (lowerEx === 'view') {
extender += 'View.extend = ';
}
});

extender += 'extend;';

src = src.replace('// Helpers', extender + '\n\n // Helpers');

return src;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "grunt-backbonebuilder",
"description": "Backbone custom builds builder",
"version": "0.1.2",
"version": "0.1.3",
"homepage": "https://github.com/asciidisco/grunt-backbonebuilder",
"author": {
"name": "asciidisco",
Expand Down
2 changes: 0 additions & 2 deletions tasks/backbonebuilder.js
@@ -1,5 +1,3 @@
/*global require: true */

/*
* grunt-backbonebuilder
* https://github.com/asciidisco/grunt-backbonebuilder
Expand Down
51 changes: 48 additions & 3 deletions test/backbonebuilder_test.js
@@ -1,12 +1,57 @@
'use strict';

var grunt = require('grunt');
// Load local tasks.
grunt.loadTasks('tasks');

exports['require'] = {
setUp: function(done) {
'use strict';
// setup here
done();
}
},
// test if building from a custom minified file fails
'testIfBuildingFromMinifiedFails': function(test) {
'use strict';
var builder = require(__dirname + '/../lib/builder').init(grunt);
test.expect(2);

// tests here
builder.build({
config: {include: ['View'], src: __dirname + '/../node_modules/backbone/backbone-min.js'}
}, function (transport) {
test.equal(transport.type, 'error', 'Error has been detected');
test.equal(transport.content.search('unminified'), 26, 'Correct error message has been send');
test.done();
});
},
// test if building from a custom input file works
'testIfBuildingFromCustomInputWorks': function(test) {
'use strict';
var builder = require(__dirname + '/../lib/builder').init(grunt);
test.expect(2);

// tests here
builder.build({
config: {include: ['All'], src: __dirname + '/../node_modules/backbone/backbone.js'}
}, function (transport) {
test.equal(transport.type, 'content', 'Transport type set correctly');
test.equal(transport.content.length, 53107, 'File could be build');
test.done();
});
},
// test if building from a custom input file works
'testIfBuildingFromNonExistingFileFails': function(test) {
'use strict';
var builder = require(__dirname + '/../lib/builder').init(grunt);
test.expect(2);

// tests here
builder.build({
config: {include: ['All'], src: __dirname + 'xxx.js'}
}, function (transport) {
test.equal(transport.type, 'error', 'Transport type set correctly');
test.equal(transport.content.search('Could not load given'), 0, 'Correct error message has been send');
test.done();
});
},

};
27 changes: 27 additions & 0 deletions test/backbonetestsuite/test_collection.html
@@ -0,0 +1,27 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Backbone Test Suite</title>
<link rel="stylesheet" href="../../node_modules/backbone/test/vendor/qunit.css" type="text/css" media="screen" />
<script src="../../node_modules/backbone/test/vendor/json2.js"></script>
<script src="../../node_modules/backbone/test/vendor/jquery-1.7.1.js"></script>
<script src="../../node_modules/backbone/test/vendor/qunit.js"></script>
<script type="text/javascript">
QUnit.config.reorder = false;
</script>
<script src="../../node_modules/backbone/test/vendor/jslitmus.js"></script>
<script src="../../node_modules/backbone/test/vendor/underscore-1.3.1.js"></script>
<script src="../../fixtures/backbone.custom.Collection.js"></script>
<script src="../../node_modules/backbone/test/collection.js"></script>
</head>
<body>
<h1 id="qunit-header">Backbone Test Suite</h1>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<br /><br />
<h1 id="qunit-header"><a href="#">Backbone Speed Suite</a></h1>
<div id="jslitmus_container" style="margin: 20px 10px;"></div>
</body>
</html>
24 changes: 24 additions & 0 deletions test/backbonetestsuite/test_events.html
@@ -0,0 +1,24 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Backbone Test Suite</title>
<link rel="stylesheet" href="../../node_modules/backbone/test/vendor/qunit.css" type="text/css" media="screen" />
<script src="../../node_modules/backbone/test/vendor/json2.js"></script>
<script src="../../node_modules/backbone/test/vendor/jquery-1.7.1.js"></script>
<script src="../../node_modules/backbone/test/vendor/qunit.js"></script>
<script type="text/javascript">
QUnit.config.reorder = false;
</script>
<script src="../../node_modules/backbone/test/vendor/underscore-1.3.1.js"></script>
<script src="../../node_modules/backbone/backbone.js"></script>

<script src="../../fixtures/backbone.custom.Events.js"></script>
</head>
<body>
<h1 id="qunit-header">Backbone Test Suite</h1>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

0 comments on commit 8f45315

Please sign in to comment.