Browse files

refactor to run tests using node; add travis

  • Loading branch information...
1 parent 493c21e commit b84336708bb475c73f948c923bf519d9ba03d4ef @andrewrjones committed Apr 7, 2013
Showing with 196 additions and 190 deletions.
  1. +6 −0 .travis.yml
  2. +42 −0 Gruntfile.js
  3. +83 −75 doubly-linked-list.js
  4. +0 −37 grunt.js
  5. +20 −1 package.json
  6. +0 −28 test/doubly-linked-list.html
  7. +45 −49 test/doubly-linked-list_test.js
View
6 .travis.yml
@@ -0,0 +1,6 @@
+language: node_js
+node_js:
+ - "0.10"
+
+before_install:
+ - git submodule update --init --recursive
View
42 Gruntfile.js
@@ -0,0 +1,42 @@
+module.exports = function (grunt) {
+ 'use strict';
+
+ // Project configuration.
+ grunt.initConfig({
+ nodeunit: {
+ files: ['test/**/*.js']
+ },
+ beautify: {
+ files: '<%= jshint.files %>'
+ },
+ jshint: {
+ options: {
+ curly: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ boss: true,
+ eqnull: true,
+ node: true,
+ globals: {
+ exports: true
+ }
+ },
+ files: ['Gruntfile.js', 'doubly-linked-list.js', 'test/**/*.js']
+ }
+ });
+
+ grunt.loadNpmTasks('grunt-contrib-jshint');
+ grunt.loadNpmTasks('grunt-beautify');
+ grunt.loadNpmTasks('grunt-contrib-nodeunit');
+
+ // Default task.
+ grunt.registerTask('default', ['jshint', 'nodeunit']);
+
+ grunt.registerTask('tidy', ['beautify']);
+
+};
View
158 doubly-linked-list.js
@@ -1,111 +1,119 @@
+(function (exports) {
+ "use strict";
/*
* Constructor. Takes no arguments.
*/
-function DoublyLinkedList() {
- // pointer to first item
- this._head = null;
- // pointer to the last item
- this._tail = null;
- // length of list
- this._length = 0;
-}
-
-// Wraps data in a node object.
-DoublyLinkedList.prototype._createNewNode = function(data){
- var node = {
- data: data,
+
+ function DoublyLinkedList() {
+ // pointer to first item
+ this._head = null;
+ // pointer to the last item
+ this._tail = null;
+ // length of list
+ this._length = 0;
+ }
+
+ // Wraps data in a node object.
+ DoublyLinkedList.prototype._createNewNode = function (data) {
+ var node = {
+ data: data,
next: null,
prev: null
+ };
+ return node;
};
- return node;
-};
/*
* Appends a node to the end of the list.
*/
-DoublyLinkedList.prototype.append = function(data){
- var node = this._createNewNode(data);
-
- if (this._length === 0) {
-
- // first node, so all pointers to this
- this._head = node;
- this._tail = node;
- } else {
-
- // put on the tail
- this._tail.next = node;
- node.prev = this._tail;
- this._tail = node;
- }
-
- // update count
- this._length++;
+ DoublyLinkedList.prototype.append = function (data) {
+ var node = this._createNewNode(data);
+
+ if (this._length === 0) {
+
+ // first node, so all pointers to this
+ this._head = node;
+ this._tail = node;
+ } else {
- return node;
-};
+ // put on the tail
+ this._tail.next = node;
+ node.prev = this._tail;
+ this._tail = node;
+ }
+
+ // update count
+ this._length++;
+
+ return node;
+ };
/*
* Prepends a node to the end of the list.
*/
-DoublyLinkedList.prototype.prepend = function(data){
- var node = this._createNewNode(data);
-
- if (this.first === null) {
-
- // we are empty, so this is the first node
- // use the same logic as append
- this.append(node);
- return;
- } else {
-
- // place before head
- this._head.prev = node;
- node.next = this._head;
- this._head = node;
- }
-
- // update count
- this._length++;
+ DoublyLinkedList.prototype.prepend = function (data) {
+ var node = this._createNewNode(data);
+
+ if (this.first === null) {
- return node;
-};
+ // we are empty, so this is the first node
+ // use the same logic as append
+ this.append(node);
+ return;
+ } else {
+
+ // place before head
+ this._head.prev = node;
+ node.next = this._head;
+ this._head = node;
+ }
+
+ // update count
+ this._length++;
+
+ return node;
+ };
/*
* Returns the node at the specified index. The index starts at 0.
*/
-DoublyLinkedList.prototype.item = function(index){
- if (index >= 0 && index < this._length){
- var node = this._head;
- while (index--) { node = node.next; }
- return node;
- }
-};
+ DoublyLinkedList.prototype.item = function (index) {
+ if (index >= 0 && index < this._length) {
+ var node = this._head;
+ while (index--) {
+ node = node.next;
+ }
+ return node;
+ }
+ };
/*
* Returns the node at the head of the list.
*/
-DoublyLinkedList.prototype.head = function(){
- return this._head;
-};
+ DoublyLinkedList.prototype.head = function () {
+ return this._head;
+ };
/*
* Returns the node at the tail of the list.
*/
-DoublyLinkedList.prototype.tail = function(){
- return this._tail;
-};
+ DoublyLinkedList.prototype.tail = function () {
+ return this._tail;
+ };
/*
* Returns the size of the list.
*/
-DoublyLinkedList.prototype.size = function(){
- return this._length;
-};
+ DoublyLinkedList.prototype.size = function () {
+ return this._length;
+ };
/*
* Removes the item at the index.
*/
-DoublyLinkedList.prototype.remove = function(index){
- throw "Not implemented";
-};
+ DoublyLinkedList.prototype.remove = function (index) {
+ throw "Not implemented";
+ };
+
+ exports.DoublyLinkedList = DoublyLinkedList;
+})(typeof exports === 'undefined' ? this['DLL'] = {} : exports);
View
37 grunt.js
@@ -1,37 +0,0 @@
-module.exports = function(grunt) {
-
- // Project configuration.
- grunt.initConfig({
- qunit: {
- files: ['test/**/*.html']
- },
- lint: {
- files: ['grunt.js', 'doubly-linked-list.js', 'lib/**/*.js', 'test/**/*.js']
- },
- watch: {
- files: '<config:lint.files>',
- tasks: 'default'
- },
- 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
- },
- globals: {}
- }
- });
-
- // tasks
- grunt.registerTask('default', 'lint qunit');
-
-};
View
21 package.json
@@ -1,9 +1,28 @@
{
"name": "DoublyLinkedList",
+ "author": {
+ "name": "Andrew Jones",
+ "email": "andrew@andrew-jones.com",
+ "url": "http://andrew-jones.com"
+ },
"version": "0.0.1",
"private": true,
"dependencies": {},
"devDependencies": {
- "grunt": "~0.3.7"
+ "grunt": "~0.4",
+ "grunt-cli": "~0.1",
+ "grunt-beautify": "git://github.com/terryweiss/grunt-beautify.git#FixForGrunt040",
+ "grunt-contrib-nodeunit": "~0.1.2",
+ "grunt-contrib-jshint": "~0.1.1"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/andrewrjones/grunt-ssh/blob/master/LICENSE-MIT"
+ }
+ ],
+ "main": "doubly-linked-list.js",
+ "scripts": {
+ "test": "grunt"
}
}
View
28 test/doubly-linked-list.html
@@ -1,28 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head>
- <meta charset="utf-8">
- <title>DoublyLinkedList Test Suite</title>
- <!-- Load local jQuery, removing access to $ (use jQuery, not $). -->
- <script src='http://code.jquery.com/jquery-1.7.2.js' type='text/javascript'></script>
- <script>jQuery.noConflict()</script>
- <!-- Load local QUnit (grunt requires v1.0.0 or newer). -->
- <link rel="stylesheet" href="../ext/qunit/qunit/qunit.css" media="screen">
- <script src="../ext/qunit/qunit/qunit.js"></script>
- <!-- Load local lib and tests. -->
- <script src="../doubly-linked-list.js"></script>
- <script src="doubly-linked-list_test.js"></script>
-</head>
-<body>
- <h1 id="qunit-header">DoublyLinkedList Test Suite</h1>
- <h2 id="qunit-banner"></h2>
- <div id="qunit-testrunner-toolbar"></div>
- <h2 id="qunit-userAgent"></h2>
- <ol id="qunit-tests"></ol>
- <div id="qunit-fixture">
- <span>lame test markup</span>
- <span>normal test markup</span>
- <span>awesome test markup</span>
- </div>
-</body>
-</html>
View
94 test/doubly-linked-list_test.js
@@ -1,73 +1,69 @@
-/*global QUnit:false, module:false, test:false, asyncTest:false, expect:false*/
-/*global start:false, stop:false ok:false, equal:false, notEqual:false, deepEqual:false*/
-/*global notDeepEqual:false, strictEqual:false, notStrictEqual:false, raises:false*/
-/*global DoublyLinkedList:false, jQuery:false*/
-(function($) {
+var DLL = require('../doubly-linked-list.js');
- module('DoublyLinkedList#unit', {
- setup: function() {
- this.elems = $('#qunit-fixture').children();
- }
- });
-
- test("DoublyLinkedList", 27, function() {
- var list = new DoublyLinkedList(),
+module.exports = {
+ 'DoublyLinkedList': function (test) {
+ "use strict";
+
+ test.expect(27);
+
+ var list = new DLL.DoublyLinkedList(),
node = null;
- ok(list);
- strictEqual(list.size(), 0);
-
+ test.ok(list);
+ test.strictEqual(list.size(), 0);
+
// append items to the list
list.append('data1');
- strictEqual(list.size(), 1);
+ test.strictEqual(list.size(), 1);
node = list.append('data2');
- strictEqual(list.size(), 2);
+ test.strictEqual(list.size(), 2);
list.append('data3');
- strictEqual(list.size(), 3);
+ test.strictEqual(list.size(), 3);
// check pointers of middle node
- strictEqual(node.prev.data, 'data1');
- strictEqual(node.next.data, 'data3');
-
+ test.strictEqual(node.prev.data, 'data1');
+ test.strictEqual(node.next.data, 'data3');
+
// get tail item
node = list.tail();
- ok(node);
- strictEqual(node.data, 'data3');
- strictEqual(node.prev.data, 'data2');
- strictEqual(node.next, null);
-
+ test.ok(node);
+ test.strictEqual(node.data, 'data3');
+ test.strictEqual(node.prev.data, 'data2');
+ test.strictEqual(node.next, null);
+
// get at 2
node = list.item(1);
- ok(node);
- strictEqual(node.data, 'data2');
+ test.ok(node);
+ test.strictEqual(node.data, 'data2');
// check pointers
- strictEqual(node.prev.data, 'data1');
- strictEqual(node.next.data, 'data3');
-
+ test.strictEqual(node.prev.data, 'data1');
+ test.strictEqual(node.next.data, 'data3');
+
// prepend an item
node = list.prepend('data4');
- strictEqual(node.prev, null);
- strictEqual(node.next.data, 'data1');
- strictEqual(list.size(), 4);
-
+ test.strictEqual(node.prev, null);
+ test.strictEqual(node.next.data, 'data1');
+ test.strictEqual(list.size(), 4);
+
// get new item
node = list.head();
- ok(node);
- strictEqual(node.data, 'data4');
- strictEqual(node.prev, null);
- strictEqual(node.next.data, 'data1');
-
+ test.ok(node);
+ test.strictEqual(node.data, 'data4');
+ test.strictEqual(node.prev, null);
+ test.strictEqual(node.next.data, 'data1');
+
// get at 2
node = list.item(1);
- ok(node);
- strictEqual(node.data, 'data1');
+ test.ok(node);
+ test.strictEqual(node.data, 'data1');
// check pointers
- strictEqual(node.prev.data, 'data4');
- strictEqual(node.next.data, 'data2');
-
+ test.strictEqual(node.prev.data, 'data4');
+ test.strictEqual(node.next.data, 'data2');
+
// not yet implemented
- raises(function(){
+ test.throws(function () {
list.remove(1);
});
- });
-}(jQuery));
+ test.done();
+ }
+};

0 comments on commit b843367

Please sign in to comment.