Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Initial commit of the example project including source code and unit test
code along with a grunt task for running the tests from the command line.
  • Loading branch information
jonnyreeves committed Aug 14, 2012
0 parents commit 13136d7
Show file tree
Hide file tree
Showing 14 changed files with 4,476 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
@@ -0,0 +1,11 @@
# qunit-require
An example which shows how to get QUnit and RequireJS to play nice with each other.

For more information, please refer my [blog entry]( http://www.jonnyreeves.co.uk/2012/qunit-and-requirejs) which describes this setup in further detail, or just dive head-first into the code comments :)

## Running the Tests
Although this example project has almost zero real world value, you can execute the default [grunt](https://github.com/cowboy/grunt) task to lint and execute the unit tests from the command line. From the root of the project, issue:

grunt

Alternativley, open the [Test Runner](https://raw.github.com/jonnyreeves/qunit-require/master/tests/tests.html] in your web browser.
41 changes: 41 additions & 0 deletions grunt.js
@@ -0,0 +1,41 @@
/*global module:false*/
module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
lint: {
src: ['src/example/**/*.js'],
tests: ['tests/example/**/*.js']
},
qunit: {
files: ['tests/tests.html']
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
browser: true
},

src: {
globals: { define: true }
},
tests: {
globals: { QUnit: true, define: true }
}
},
uglify: {}
});

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

};
32 changes: 32 additions & 0 deletions src/example/model/Game.js
@@ -0,0 +1,32 @@
define(function (require) {
"use strict";

// Represents a Computer Game.
function Game(name, pegiRating) {
this._name = name;
this._pegiRating = pegiRating;
}

// Define the Game object methods.
Game.prototype = {

// Repoint the Constructor function.
constructor: Game,

// Returns true if this Game is suitible for the supplied age
isSuitibleFor: function (age) {

// Forward to the requires to the PegiRating object, applies the
// Law of Demeter (least knowledge).
return this._pegiRating.isSuitibleFor(age);
},

toString: function () {
return this._name + " (" + this._pegiRating + ")";
}
};

// Export the Constructor function.
return Game;

});
37 changes: 37 additions & 0 deletions src/example/model/PegiRatings.js
@@ -0,0 +1,37 @@
define(function (require) {
"use strict";

// Represents a PegiRating value
function PegiRating(minAge) {
this._minAge = minAge;
}

// Define the Game object methods.
PegiRating.prototype = {

// Repoint the Constructor function.
constructor: PegiRating,

// Returns true if the supplied age range is equal to or greater than
// the minimum age indicated by this Pegi Rating.
isSuitibleFor: function(age) {
return age >= this._minAge;
},

toString: function () {
return "PEGI " + this._minAge;
}
};

// Don't export the Constructor, instead export a hash of values as the
// PegiRating module is intended to be used as an Enum (all valid values
// are known ahead of execution).
return {
PEGI_3: new PegiRating(3),
PEGI_7: new PegiRating(7),
PEGI_12: new PegiRating(12),
PEGI_16: new PegiRating(16),
PEGI_18: new PegiRating(18)
};

});
40 changes: 40 additions & 0 deletions src/example/model/Player.js
@@ -0,0 +1,40 @@
define(function (require) {
"use strict";

// Import underscore.
var _ = require("vendor/underscore");

// Represents a Player who onws a collection of Comptuer Games.
function Player(name, age, games) {
this._name = name;
this._age = age;
this._games = _.isArray(games) ? games.slice() : [];
}

// Define the Player object methods.
Player.prototype = {

// Repoint the Constructor function.
constructor: Player,

// Returns an Array of Games which this Player should not have in their
// collection because they are indenteded for an older audience based
// on their Pegi Rating.
getUnsuitableGames: function () {
var self = this;

return _.filter(this._games, function (game) {
return !game.isSuitibleFor(self._age);
});
},

toString: function () {
return this._name + " (" + this._age + ") has " +
this._games.length + " game(s)";
}
};

// Export the Constructor function.
return Player;

});
7 changes: 7 additions & 0 deletions src/require-config.js
@@ -0,0 +1,7 @@
var require = {
shim: {
"vendor/underscore": {
exports: "_"
}
}
};

0 comments on commit 13136d7

Please sign in to comment.