Skip to content

Commit

Permalink
Add npm test, travis config, remove globals.
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed Dec 27, 2012
1 parent ec2b5f0 commit 4a7314a
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
raw
raw
node_modules
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.8
notifications:
email: false
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
"author" : "Jeremy Ashkenas <jeremy@documentcloud.org>",
"repository" : {"type": "git", "url": "git://github.com/documentcloud/underscore.git"},
"main" : "underscore.js",
"version" : "1.4.3"
"version" : "1.4.3",
"devDependencies": {
"phantomjs": "0.2.2"
},
"scripts": {
"test": "phantomjs test/vendor/runner.js test/index.html?noglobals=true"
}
}
2 changes: 1 addition & 1 deletion test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ $(document).ready(function() {
equal(_.indexOf(null, 2), -1, 'handles nulls properly');

numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3];
index = _.lastIndexOf(numbers, 2, 2);
var index = _.lastIndexOf(numbers, 2, 2);
equal(index, 1, 'supports the fromIndex argument');
});

Expand Down
2 changes: 1 addition & 1 deletion test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $(document).ready(function() {
equal(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
delete obj.constructor.prototype.four;

answer = null;
var answer = null;
_.each([1, 2, 3], function(num, index, arr){ if (_.include(arr, num)) answer = true; });
ok(answer, 'can reference the original collection from inside the iterator');

Expand Down
4 changes: 2 additions & 2 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,15 @@ $(document).ready(function() {
value();
ok(returned == 6 && intercepted == 6, 'can use tapped objects in a chain');
});

test("has", function () {
var obj = {foo: "bar", func: function () {} };
ok (_.has(obj, "foo"), "has() checks that the object has a property.");
ok (_.has(obj, "baz") == false, "has() returns false if the object doesn't have the property.");
ok (_.has(obj, "func"), "has() works for functions too.");
obj.hasOwnProperty = null;
ok (_.has(obj, "foo"), "has() works even when the hasOwnProperty method is deleted.");
child = {};
var child = {};
child.prototype = obj;
ok (_.has(child, "foo") == false, "has() does not check the prototype chain for a property.")
});
Expand Down
6 changes: 3 additions & 3 deletions test/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ $(document).ready(function() {
equal(escapeTemplate({a: true}), 'checked="checked"', 'can handle slash escapes in interpolations.');

var fancyTemplate = _.template("<ul><% \
for (key in people) { \
for (var key in people) { \
%><li><%= people[key] %></li><% } %></ul>");
result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
equal(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
Expand Down Expand Up @@ -137,7 +137,7 @@ $(document).ready(function() {
interpolate : /\{\{=([\s\S]+?)\}\}/g
};

var custom = _.template("<ul>{{ for (key in people) { }}<li>{{= people[key] }}</li>{{ } }}</ul>");
var custom = _.template("<ul>{{ for (var key in people) { }}<li>{{= people[key] }}</li>{{ } }}</ul>");
result = custom({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
equal(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');

Expand All @@ -152,7 +152,7 @@ $(document).ready(function() {
interpolate : /<\?=([\s\S]+?)\?>/g
};

var customWithSpecialChars = _.template("<ul><? for (key in people) { ?><li><?= people[key] ?></li><? } ?></ul>");
var customWithSpecialChars = _.template("<ul><? for (var key in people) { ?><li><?= people[key] ?></li><? } ?></ul>");
result = customWithSpecialChars({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
equal(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');

Expand Down
98 changes: 98 additions & 0 deletions test/vendor/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Qt+WebKit powered headless test runner using Phantomjs
*
* Phantomjs installation: http://code.google.com/p/phantomjs/wiki/BuildInstructions
*
* Run with:
* phantomjs runner.js [url-of-your-qunit-testsuite]
*
* E.g.
* phantomjs runner.js http://localhost/qunit/test
*/

/*jshint latedef:false */
/*global phantom:true require:true console:true */
var url = phantom.args[0],
page = require('webpage').create();

// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
page.onConsoleMessage = function(msg) {
console.log(msg);
};

page.onInitialized = function() {
page.evaluate(addLogging);
};
page.open(url, function(status){
if (status !== "success") {
console.log("Unable to access network: " + status);
phantom.exit(1);
} else {
// page.evaluate(addLogging);
var interval = setInterval(function() {
if (finished()) {
clearInterval(interval);
onfinishedTests();
}
}, 500);
}
});

function finished() {
return page.evaluate(function(){
return !!window.qunitDone;
});
}

function onfinishedTests() {
var output = page.evaluate(function() {
return JSON.stringify(window.qunitDone);
});
phantom.exit(JSON.parse(output).failed > 0 ? 1 : 0);
}

function addLogging() {
window.document.addEventListener( "DOMContentLoaded", function() {
var current_test_assertions = [];

QUnit.testDone(function(result) {
var i,
name = result.module + ': ' + result.name;

if (result.failed) {
console.log('Assertion Failed: ' + name);

for (i = 0; i < current_test_assertions.length; i++) {
console.log(' ' + current_test_assertions[i]);
}
}

current_test_assertions = [];
});

QUnit.log(function(details) {
var response;

if (details.result) {
return;
}

response = details.message || '';

if (typeof details.expected !== 'undefined') {
if (response) {
response += ', ';
}

response += 'expected: ' + details.expected + ', but was: ' + details.actual;
}

current_test_assertions.push('Failed assertion: ' + response);
});

QUnit.done(function(result){
console.log('Took ' + result.runtime + 'ms to run ' + result.total + ' tests. ' + result.passed + ' passed, ' + result.failed + ' failed.');
window.qunitDone = result;
});
}, false );
}

0 comments on commit 4a7314a

Please sign in to comment.