Adding karma and unit tests to get started

This commit is contained in:
Colin McLeod
2015-06-11 21:04:52 -07:00
parent 3f8cf106a1
commit cc2d91cc51
6 changed files with 148 additions and 72 deletions

View File

@@ -10,8 +10,8 @@ var appCache = require("gulp-manifest"),
eslint = require('gulp-eslint'); eslint = require('gulp-eslint');
gutil = require('gulp-util'), gutil = require('gulp-util'),
htmlmin = require('gulp-htmlmin'), htmlmin = require('gulp-htmlmin'),
jasmine = require('gulp-jasmine'),
jsonlint = require("gulp-jsonlint"), jsonlint = require("gulp-jsonlint"),
karma = require('karma').server,
less = require('gulp-less'), less = require('gulp-less'),
mainBowerFiles = require('main-bower-files'), mainBowerFiles = require('main-bower-files'),
minifyCSS = require('gulp-minify-css'), minifyCSS = require('gulp-minify-css'),
@@ -252,9 +252,13 @@ gulp.task('upload', function(done) {
); );
}); });
gulp.task('test', function () { gulp.task('test', function (done) {
return gulp.src('tests/test-*.js') karma.start({
.pipe(jasmine()); configFile: __dirname + '/test/karma.conf.js',
singleRun: true
}, function(exitStatus) {
done(exitStatus ? new gutil.PluginError('karma', { message: 'Unit tests failed!' }) : undefined);
});
}); });
gulp.task('lint', ['js-lint', 'json-lint']); gulp.task('lint', ['js-lint', 'json-lint']);

View File

@@ -9,6 +9,7 @@
"engine": "node >= 0.12.2", "engine": "node >= 0.12.2",
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {
"angular-mocks": "^1.3.16",
"async": "^0.9.0", "async": "^0.9.0",
"del": "^1.1.1", "del": "^1.1.1",
"gulp": "^3.8.11", "gulp": "^3.8.11",
@@ -28,7 +29,11 @@
"gulp-template": "^3.0.0", "gulp-template": "^3.0.0",
"gulp-uglify": "^1.2.0", "gulp-uglify": "^1.2.0",
"gulp-util": "^3.0.4", "gulp-util": "^3.0.4",
"jasmine-core": "^2.3.4",
"json-concat": "0.0.0", "json-concat": "0.0.0",
"karma": "^0.12.36",
"karma-chrome-launcher": "^0.1.12",
"karma-jasmine": "^0.3.5",
"main-bower-files": "^2.6.2", "main-bower-files": "^2.6.2",
"run-sequence": "^1.0.2", "run-sequence": "^1.0.2",
"uglify-js": "^2.4.19" "uglify-js": "^2.4.19"

34
test/karma.conf.js Normal file
View File

@@ -0,0 +1,34 @@
// Karma configuration
// Generated on Thu Jun 11 2015 19:39:40 GMT-0700 (PDT)
module.exports = function(config) {
config.set({
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'../build/lib*.js',
'../node_modules/angular-mocks/angular-mocks.js',
'../build/app*.js',
'tests/**/*.js'
],
// list of files to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
port: 9876,
colors: true,
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome'],
singleRun: false
});
};

66
test/tests/test-data.js Normal file
View File

@@ -0,0 +1,66 @@
describe("Database", function() {
var shipProperties = ["grp", "name", "manufacturer", "class", "cost", "speed", "boost", "agility", "shields", "armour", "fuelcost", "mass"];
it("has ships and components", function() {
expect(DB.ships).toBeDefined()
expect(DB.components.common).toBeDefined();
expect(DB.components.hardpoints).toBeDefined();
expect(DB.components.internal).toBeDefined();
expect(DB.components.bulkheads).toBeDefined();
});
it("has unique IDs for every hardpoint", function() {
var ids = {};
var groups = DB.components.hardpoints;
for (var g in groups) {
var group = groups[g];
for (var i = 0; i < group.length; i++) {
var id = group[i].id;
expect(ids[id]).toBeFalsy('ID already exists: ' + id);
expect(group[i].grp).toBeDefined('Hardpoint has no group defined, ID:' + id);
ids[id] = true;
}
}
});
it("has valid internal components", function() {
var ids = {};
var groups = DB.components.internal;
for (var g in groups) {
var group = groups[g];
for (var i = 0; i < group.length; i++) {
var id = group[i].id;
expect(ids[id]).toBeFalsy('ID already exists: ' + id);
expect(group[i].grp).toBeDefined('Internal component has no group defined, ID:' + id);
ids[id] = true;
}
}
});
it("has data for every ship", function() {
for (var s in DB.ships) {
for (var p = 0; p < shipProperties.length; p++) {
expect(DB.ships[s].properties[shipProperties[p]]).toBeDefined(shipProperties[p] + ' is missing for ' + s);
}
expect(DB.ships[s].slots.common.length).toEqual(7, s + ' is missing common slots');
expect(DB.ships[s].defaults.common.length).toEqual(7, s + ' is missing common defaults');
expect(DB.ships[s].slots.hardpoints.length).toEqual(DB.ships[s].defaults.hardpoints.length, s + ' hardpoint slots and defaults dont match');
expect(DB.ships[s].slots.internal.length).toEqual(DB.ships[s].defaults.internal.length, s + ' hardpoint slots and defaults dont match');
expect(DB.ships[s].retailCost).toBeGreaterThan(DB.ships[s].properties.cost, s + ' has invalid retail cost');
expect(DB.components.bulkheads[s]).toBeDefined(s + ' is missing bulkheads');
}
});
it("has components with a group defined", function() {
for (var i = 0; i < DB.components.common.length; i++) {
var group = DB.components.common[i];
for (var c in group) {
expect(group[c].grp).toBeDefined('Common component has no group defined, Type: ' + i + ', ID: ' + c);
}
}
});
});

View File

@@ -0,0 +1,35 @@
describe("Ship Factory", function() {
var Ship;
beforeEach(module('shipyard'));
beforeEach(inject(['Ship', function (_Ship_) {
Ship = _Ship_;
}]));
it("can build all ships", function() {
for (var s in DB.ships) {
var shipData = DB.ships[s];
var ship = new Ship(s, shipData.properties, shipData.slots);
for (p in shipData.properties) {
expect(ship[p]).toEqual(shipData.properties[p], s + ' property [' + p + '] does not match when built');
}
ship.buildWith(shipData.defaults);
expect(ship.totalCost).toEqual(shipData.retailCost, s + ' retail cost does not match default build cost');
expect(ship.priorityBands[0].retracted).toBeGreaterThan(0);
expect(ship.powerAvailable).toBeGreaterThan(0);
expect(ship.unladenRange).toBeGreaterThan(0);
expect(ship.ladenRange).toBeGreaterThan(0);
expect(ship.cargoCapacity).toBeGreaterThan(0);
expect(ship.fuelCapacity).toBeGreaterThan(0);
expect(ship.unladenTotalRange).toBeGreaterThan(0);
expect(ship.ladenTotalRange).toBeGreaterThan(0);
expect(ship.shieldStrength).toBeGreaterThan(0);
expect(ship.armourTotal).toBeGreaterThan(0);
}
});
});

View File

@@ -1,68 +0,0 @@
var data = require('../app/db.json');
var shipProperties = ["grp", "name", "manufacturer", "class", "cost", "speed", "boost", "agility", "shields", "armour", "fuelcost", "mass"];
describe("Database", function() {
it("has ships and components", function() {
expect(data.ships).toBeDefined()
expect(data.components.common).toBeDefined();
expect(data.components.hardpoints).toBeDefined();
expect(data.components.internal).toBeDefined();
expect(data.components.bulkheads).toBeDefined();
});
it("has unique IDs for every hardpoint", function() {
var ids = {};
var groups = data.components.hardpoints;
for (var g in groups) {
var group = groups[g];
for (var i = 0; i < group.length; i++) {
var id = group[i].id;
expect(ids[id]).toBeFalsy('ID already exists: ' + id);
expect(group[i].grp).toBeDefined('Hardpoint has no group defined, ID:' + id);
ids[id] = true;
}
}
});
it("has valid internal components", function() {
var ids = {};
var groups = data.components.internal;
for (var g in groups) {
var group = groups[g];
for (var i = 0; i < group.length; i++) {
var id = group[i].id;
expect(ids[id]).toBeFalsy('ID already exists: ' + id);
expect(group[i].grp).toBeDefined('Internal component has no group defined, ID:' + id);
ids[id] = true;
}
}
});
it("has data for every ship", function() {
for (var s in data.ships) {
for (var p = 0; p < shipProperties.length; p++) {
expect(data.ships[s].properties[shipProperties[p]]).toBeDefined(shipProperties[p] + ' is missing for ' + s);
}
expect(data.ships[s].slots.common.length).toEqual(7, s + ' is missing common slots');
expect(data.ships[s].defaults.common.length).toEqual(7, s + ' is missing common defaults');
expect(data.ships[s].slots.hardpoints.length).toEqual(data.ships[s].defaults.hardpoints.length, s + ' hardpoint slots and defaults dont match');
expect(data.ships[s].slots.internal.length).toEqual(data.ships[s].defaults.internal.length, s + ' hardpoint slots and defaults dont match');
expect(data.ships[s].retailCost).toBeGreaterThan(data.ships[s].properties.cost, s + ' has invalid retail cost');
expect(data.components.bulkheads[s]).toBeDefined(s + ' is missing bulkheads');
}
});
it("has components with a group defined", function() {
for (var i = 0; i < data.components.common.length; i++) {
var group = data.components.common[i];
for (var c in group) {
expect(group[c].grp).toBeDefined('Common component has no group defined, Type: ' + i + ', ID: ' + c);
}
}
});
});