Update generator to create pure-JS adaptors

This commit is contained in:
Andrew Stewart 2014-02-27 10:46:58 -08:00 committed by deadprogram
parent ce8107d607
commit ddc24eeca9
24 changed files with 175 additions and 500 deletions

View File

@ -1,157 +0,0 @@
'use strict';
module.exports = function (grunt) {
// load all grunt tasks
require('matchdep').filterDev('grunt-*').forEach(function(contrib) {
grunt.log.ok([contrib + " is loaded"]);
grunt.loadNpmTasks(contrib);
});
var config = {
dist: 'dist',
src: 'src',
distTest: 'test/dist',
srcTest: 'test/src'
};
// Project configuration.
grunt.initConfig({
config: config,
clean: {
dist: {
files: [
{
dot: true,
src: [
'<%= config.dist %>/*',
'<%= config.distTest %>/*',
'!<%= config.dist %>/.git*'
]
}
]
},
},
coffee: {
dist: {
files: [{
expand: true,
cwd: '<%= config.src %>',
src: '{,*/}*.coffee',
dest: '<%= config.dist %>',
ext: '.js'
}]
},
test: {
files: [{
expand: true,
cwd: '<%= config.srcTest %>',
src: '{,*/}*.spec.coffee',
dest: '<%= config.distTest %>',
ext: '.spec.js'
}]
}
},
jshint: {
options: {
jshintrc: '.jshintrc'
},
gruntfile: {
src: 'Gruntfile.js'
},
},
watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
dist: {
files: '<%= config.src %>/*',
tasks: ['coffee:dist', 'simplemocha:backend']
},
test: {
files: '<%= config.srcTest %>/specs/*',
tasks: ['coffee:test', 'simplemocha:backend']
}
},
simplemocha: {
options: {
globals: [
'sinon',
'chai',
'should',
'expect',
'assert',
'AssertionError',
],
timeout: 3000,
ignoreLeaks: false,
// grep: '*.spec',
ui: 'bdd',
reporter: 'spec'
},
backend: {
src: [
// add chai and sinon globally
'test/support/globals.js',
// tests
'test/dist/**/*.spec.js',
],
},
},
});
grunt.registerTask('coverageBackend', 'Test backend files as well as code coverage.', function () {
var done = this.async();
var path = './test/support/runner.js';
var options = {
cmd: 'istanbul',
grunt: false,
args: [
'cover',
'--default-excludes',
'-x', 'app/**',
'--report', 'lcov',
'--dir', './coverage/backend',
path
],
opts: {
// preserve colors for stdout in terminal
stdio: 'inherit',
},
};
function doneFunction(error, result) {
if (result && result.stderr) {
process.stderr.write(result.stderr);
}
if (result && result.stdout) {
grunt.log.writeln(result.stdout);
}
// abort tasks in queue if there's an error
done(error);
}
grunt.util.spawn(options, doneFunction);
});
// Default task.
grunt.registerTask('default', ['coffee', 'jshint']);
grunt.registerTask('test', [
'clean',
'coffee',
'simplemocha:backend',
]);
grunt.registerTask('coverage', [
'clean',
'coffee',
'coverageBackend'
]);
};

View File

@ -0,0 +1,17 @@
BIN := ./node_modules/.bin
TEST_FILES := test/support/globals.js $(shell find test/specs -type f -name "*.js")
# Our 'phony' make targets (don't involve any file changes)
.PHONY: test bdd lint
# Run Mocha, with standard reporter.
test:
@$(BIN)/mocha -r cylon --colors $(TEST_FILES)
# Run Mocha, with more verbose BDD reporter.
bdd:
@$(BIN)/mocha -r cylon --colors -R spec $(TEST_FILES)
# Run JSHint
lint:
@$(BIN)/jshint ./lib

View File

@ -57,7 +57,7 @@ Explain how to connect from the computer to the device here...
In lieu of a formal styleguide, take care to maintain the existing coding style.
Add unit tests for any new or changed functionality. Lint and test your code
using [Grunt](http://gruntjs.com/).
using `make test` and `make lint`.
## Release History

View File

@ -1,42 +0,0 @@
/*
* <%= adaptorName %> adaptor
* http://cylonjs.com
*
* Copyright (c) 2013-2014 Your Name Here
* Licensed under the Apache 2.0 license.
*/
(function() {
"use strict";
var namespace,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
namespace = require('node-namespace');
require('./<%= adaptorName %>');
require('./driver');
namespace('Cylon.Adaptors', function() {
return this.<%= adaptorClassName %> = (function(_super) {
__extends(<%= adaptorClassName %>, _super);
function <%= adaptorClassName %>(opts) {
if (opts == null) {
opts = {};
}
<%= adaptorClassName %>.__super__.constructor.apply(this, arguments);
}
<%= adaptorClassName %>.prototype.connect = function(callback) {
return <%= adaptorClassName %>.__super__.connect.apply(this, arguments);
};
return <%= adaptorClassName %>;
})(Cylon.Adaptor);
});
}).call(this);

View File

@ -1,41 +0,0 @@
/*
* <%= adaptorName %>
* http://cylonjs.com
*
* Copyright (c) 2013 Your Name Here
* Your License Here
*/
(function() {
'use strict';
var namespace,
__slice = [].slice;
namespace = require('node-namespace');
require('cylon');
module.exports = {
adaptor: function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args);
return Object(result) === result ? result : child;
})(Cylon.Adaptors.<%= adaptorClassName %>, args, function(){});
},
driver: function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args);
return Object(result) === result ? result : child;
})(Cylon.Drivers.<%= adaptorClassName %>, args, function(){});
},
register: function(robot) {}
};
}).call(this);

View File

@ -1,41 +0,0 @@
/*
* <%= adaptorName %> driver
* http://cylonjs.com
*
* Copyright (c) 2013-2014 Your Name Here
* Licensed under the Apache 2.0 license.
*/
(function() {
'use strict';
var namespace,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
require('./<%= adaptorName %>');
require('./adaptor');
namespace = require('node-namespace');
namespace("Cylon.Drivers", function() {
var _ref;
return this.<%= adaptorClassName %> = (function(_super) {
__extends(<%= adaptorClassName %>, _super);
function <%= adaptorClassName %>() {
_ref = <%= adaptorClassName %>.__super__.constructor.apply(this, arguments);
return _ref;
}
<%= adaptorClassName %>.prototype.start = function(callback) {
return <%= adaptorClassName %>.__super__.start.apply(this, arguments);
};
return <%= adaptorClassName %>;
})(Cylon.Driver);
});
}).call(this);

View File

@ -0,0 +1,34 @@
/*
* <%= adaptorName %> adaptor
* http://cylonjs.com
*
* Copyright (c) 2013-2014 Your Name Here
* Licensed under the Apache 2.0 license.
*/
"use strict";
var namespace = require('node-namespace');
require('./<%= adaptorName %>');
require('./driver');
namespace('Cylon.Adaptors', function() {
this.<%= adaptorClassName %> = (function(klass) {
subclass(<%= adaptorClassName %>, klass);
function <%= adaptorClassName %>(opts) {
if (opts == null) { opts = {}; }
<%= adaptorClassName %>.__super__.constructor.apply(this, arguments);
}
<%= adaptorClassName %>.prototype.connect = function(callback) {
return <%= adaptorClassName %>.__super__.connect.apply(this, arguments);
};
return <%= adaptorClassName %>;
})(Cylon.Adaptor);
});
module.exports = Cylon.Adaptors.<%= adaptorClassName %>;

View File

@ -0,0 +1,39 @@
/*
* <%= adaptorName %>
* http://cylonjs.com
*
* Copyright (c) 2013 Your Name Here
* Your License Here
*/
"use strict";
require('cylon');
require('./adaptor');
require('./driver');
module.exports = {
adaptor: function(opts) {
// Provide a function that's an instance of your adaptor here. For example,
// the Sphero adaptor creates a new instance of the Sphero adaptor class:
//
// new Cylon.Adaptors.Sphero(args...)
return new Cylon.Adaptors.<%= adaptorClassName %>(opts);
},
driver: function(opts) {
// Provide a function that's an instance of your driver here. For example,
// the Sphero adaptor creates a new instance of the Sphero driver class:
//
// new Cylon.Drivers.Sphero(args...)
return new Cylon.Drivers.<%= adaptorClassName %>(opts);
},
register: function(robot) {
// Bootstrap your adaptor here. For example, with a Sphero, you would call
// the registerAdaptor and registerDriver functions as follows:
//
// robot.registerAdaptor('cylon-sphero', 'sphero');
// robot.registerDriver('cylon-sphero', 'sphero');
}
};

View File

@ -0,0 +1,33 @@
/*
* <%= adaptorName %> driver
* http://cylonjs.com
*
* Copyright (c) 2013-2014 Your Name Here
* Licensed under the Apache 2.0 license.
*/
"use strict";
require('./<%= adaptorName %>');
require('./adaptor');
var namespace = require('node-namespace');
namespace("Cylon.Drivers", function() {
this.<%= adaptorClassName %> = (function(klass) {
subclass(<%= adaptorClassName %>, klass);
function <%= adaptorClassName %>() {
<%= adaptorClassName %>.__super__.constructor.apply(this, arguments);
}
<%= adaptorClassName %>.prototype.start = function(callback) {
return <%= adaptorClassName %>.__super__.start.apply(this, arguments);
};
return <%= adaptorClassName %>;
})(Cylon.Driver);
});
module.exports = Cylon.Drivers.<%= adaptorClassName %>;

View File

@ -1,7 +1,7 @@
{
"name": "<%= adaptorName %>",
"version": "0.1.0",
"main": "dist/<%= adaptorName %>.js",
"main": "lib/<%= adaptorName %>.js",
"description": "Cylon module for <%= adaptorClassName %>",
"homepage": "http://cylonjs.com",
"bugs": "Your bug report URL here",
@ -20,16 +20,11 @@
}
],
"devDependencies": {
"matchdep": "~0.1.1",
"grunt-contrib-jshint": "~0.6.0",
"grunt-contrib-watch": "~0.5.0",
"grunt-contrib-coffee": "~0.7.0",
"grunt-simple-mocha": "~0.4.0",
"grunt-contrib-clean": "~0.5.0",
"sinon-chai": "~2.4.0",
"chai": "~1.7.2",
"mocha": "~1.12.1",
"sinon": "~1.7.3"
"sinon-chai": "~2.5.0",
"chai": "~1.9.0",
"mocha": "~1.17.1",
"sinon": "~1.8.2",
"jshint": "~2.4.4"
},
"dependencies": {
"node-namespace": "~1.0.0",

View File

@ -1,22 +0,0 @@
###
* <%= adaptorName %> adaptor
* http://cylonjs.com
*
* Copyright (c) 2013-2014 Your Name Here
* Licensed under the Apache 2.0 license.
###
"use strict"
namespace = require 'node-namespace'
require './<%= adaptorName %>'
require './driver'
namespace 'Cylon.Adaptors', ->
class @<%= adaptorClassName %> extends Cylon.Adaptor
constructor: (opts = {}) ->
super
connect: (callback) ->
super

View File

@ -1,35 +0,0 @@
###
* <%= adaptorName %>
* http://cylonjs.com
*
* Copyright (c) 2013 Your Name Here
* Your License Here
###
'use strict'
namespace = require 'node-namespace'
require 'cylon'
module.exports =
adaptor: (args...) ->
# Provide a function that's an instance of your adaptor here. For example,
# the Sphero adaptor creates a new instance of the Sphero adaptor class:
#
# new Cylon.Adaptors.Sphero(args...)
new Cylon.Adaptors.<%= adaptorClassName %>(args...)
driver: (args...) ->
# Provide a function that's an instance of your driver here. For example,
# the Sphero adaptor creates a new instance of the Sphero driver class:
#
# new Cylon.Drivers.Sphero(args...)
new Cylon.Drivers.<%= adaptorClassName %>(args...)
register: (robot) ->
# Bootstrap your adaptor here. For example, with a Sphero, you would call
# the registerAdaptor and registerDriver functions as follows:
#
# robot.registerAdaptor 'cylon-sphero', 'sphero'
# robot.registerDriver 'cylon-sphero', 'sphero'

View File

@ -1,19 +0,0 @@
###
* <%= adaptorName %> driver
* http://cylonjs.com
*
* Copyright (c) 2013-2014 Your Name Here
* Licensed under the Apache 2.0 license.
###
'use strict'
require './<%= adaptorName %>'
require './adaptor'
namespace = require 'node-namespace'
namespace "Cylon.Drivers", ->
class @<%= adaptorClassName %> extends Cylon.Driver
start: (callback) ->
super

View File

@ -1,13 +0,0 @@
(function() {
'use strict';
var adaptor;
adaptor = source("adaptor");
describe("Cylon.Adaptors.<%= adaptorClassName %>", function() {
var module;
module = new Cylon.Adaptors.<%= adaptorClassName %>;
return it("needs tests");
});
}).call(this);

View File

@ -1,45 +0,0 @@
(function() {
'use strict';
var module;
module = source("<%= adaptorName %>");
describe("Cylon.<%= adaptorClassName %>", function() {
it("standard async test", function(done) {
var bool;
bool = false;
bool.should.be["false"];
setTimeout(function() {
bool.should.be["false"];
bool = true;
return bool.should.be["true"];
});
150;
setTimeout(function() {
bool.should.be["true"];
return done();
});
return 300;
});
it("standard sync test", function() {
var data, obj;
data = [];
obj = {
id: 5,
name: 'test'
};
data.should.be.empty;
data.push(obj);
data.should.have.length(1);
data[0].should.be.eql(obj);
return data[0].should.be.equal(obj);
});
it("should be able to register", function() {
return module.register.should.be.a('function');
});
return it("should be able to create adaptor", function() {
return module.adaptor.should.be.a('function');
});
});
}).call(this);

View File

@ -1,17 +0,0 @@
(function() {
'use strict';
var driver;
driver = source("driver");
describe("Cylon.Drivers.<%= adaptorClassName %>", function() {
var module;
module = new Cylon.Drivers.<%= adaptorClassName %>({
device: {
connection: 'connect'
}
});
return it("needs tests");
});
}).call(this);

View File

@ -0,0 +1,9 @@
"use strict";
var <%= adaptorClassName %> = source("adaptor");
describe("Cylon.Adaptors.<%= adaptorClassName %>", function() {
var adaptor = new <%= adaptorClassName %>();
it("needs tests");
});

View File

@ -0,0 +1,17 @@
"use strict";
var module = source("<%= adaptorName %>");
describe("Cylon.<%= adaptorClassName %>", function() {
it("should be able to register", function() {
expect(module.register).to.be.a('function');
});
it("should be able to create adaptor", function() {
expect(module.adaptor()).to.be.a('object');
});
it("should be able to create driver", function() {
expect(module.adaptor({ device: {} })).to.be.a('object');
});
});

View File

@ -0,0 +1,11 @@
"use strict";
var <%= adaptorClassName %> = source("driver");
describe("Cylon.Drivers.<%= adaptorClassName %>", function() {
var driver = new <%= adaptorClassName %>({
device: { connection: 'connect' }
});
it("needs tests");
});

View File

@ -1,8 +0,0 @@
'use strict'
adaptor = source("adaptor")
describe "Cylon.Adaptors.<%= adaptorClassName %>", ->
module = new Cylon.Adaptors.<%= adaptorClassName %>
it "needs tests"

View File

@ -1,36 +0,0 @@
'use strict'
module = source "<%= adaptorName %>"
describe "Cylon.<%= adaptorClassName %>", ->
it "standard async test", (done) ->
bool = false
bool.should.be.false
setTimeout ->
bool.should.be.false
bool = true
bool.should.be.true
150
setTimeout ->
bool.should.be.true
done()
300
it "standard sync test", ->
data = []
obj = id: 5, name: 'test'
data.should.be.empty
data.push obj
data.should.have.length 1
# soft equal
data[0].should.be.eql obj
# hard equal
data[0].should.be.equal obj
it "should be able to register", ->
module.register.should.be.a 'function'
it "should be able to create adaptor", ->
module.adaptor.should.be.a 'function'

View File

@ -1,9 +0,0 @@
'use strict'
driver = source("driver")
describe "Cylon.Drivers.<%= adaptorClassName %>", ->
module = new Cylon.Drivers.<%= adaptorClassName %>
device: { connection: 'connect' }
it "needs tests"

View File

@ -8,18 +8,23 @@ var path = require('path');
var chai = require('chai');
var sinonChai = require('sinon-chai');
require('cylon');
Logger.setup(false); // turn off Cylon's logger by default
global.chai = chai;
global.should = chai.should();
global.expect = chai.expect;
global.assert = chai.assert;
global.AssertionError = chai.AssertionError;
global.sinon = require('sinon');
global.spy = sinon.spy;
global.stub = sinon.stub;
// can be used by test modules to require production modules,
// relative to the base path (where the Gruntfile.js also lives)
global.source = function (src) {
console.log('source loading: ' + src)
var resource = path.normalize('../../dist/' + src);
var resource = path.normalize('../../lib/' + src);
return require(resource);
};
@ -41,4 +46,4 @@ global.err = function (fn, msg) {
chai.use(sinonChai);
// if you want a detailed error stack output
// chai.Assertion.includeStack = true;
chai.Assertion.includeStack = true;

View File

@ -8,7 +8,7 @@ var Mocha = require('mocha');
var mocha = new Mocha({ reporter: 'spec', ui: 'bdd'});
function run(cb) {
var files = grunt.file.expand(__dirname + '/../dist/**/*.spec.js');
var files = grunt.file.expand(__dirname + '/../specs/**/*.spec.js');
console.log(files)
files.forEach(function (file) {
mocha.addFile(file);