Skip to content
Permalink
Browse files
Merge pull request #9 from gtanner/master
Updated jake to support a test command for issue #7
  • Loading branch information
gtanner committed Dec 19, 2011
2 parents ddc56ad + 632592b commit 20c7725f4e53387aae8898b2a99c797605877408
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 50 deletions.
@@ -1,3 +1,4 @@
pkg/
tags
.DS_Store
node_modules/
@@ -27,3 +27,6 @@ task('build', ['clean'], function () {

util.puts(fs.readFileSync("build/dalek", "utf-8"));
});

desc("starts a webserver to point at to run the unit tests");
task('test', [], require('./build/test'));
@@ -1,11 +1,17 @@
var util = require('util'),
fs = require('fs');

function include(files, transform) {
files = files.map ? files : [files];
return files.map(function (file) {
var str = fs.readFileSync(file, "utf-8") + "\n";
return transform ? transform(str, file) : str;
}).join('\n');
}

module.exports = {
bundle: function (platform) {
var util = require('util'),
fs = require('fs'),
files = [
modules: function (platform) {
var files = [
"lib/utils.js",
"lib/plugin/navigator.js",
"lib/plugin/notification.js",
@@ -16,42 +22,38 @@ module.exports = {
"lib/channel.js",
"lib/builder.js",
"lib/exec/" +platform + ".js"
],
include = function (files, transform) {
files = files.map ? files : [files];
return files.map(function (file) {
var str = fs.readFileSync(file, "utf-8") + "\n";
return transform ? transform(str, file) : str;
}).join('\n');
}
]
output = "";

//include LICENSE
output += include("LICENSE", function (file) {
return "/*\n" + file + "\n*/\n";
});

//include require
output += include("thirdparty/almond.js");

//include modules
output += include(files, function (file, path) {
var id = path.replace(/lib\//, "phonegap/").replace(/\.js$/, '');
return "define('" + id + "', function (require, exports, module) {\n" + file + "});\n";
});

//include platform
output += include('lib/platform/' + platform + '.js', function (file, path) {
return "define('phonegap/platform'" +
", function (require, exports, module) {\n" + file + "});\n";
});

//include phonegap
output += include('lib/phonegap.js', function (file, path) {
return "define('phonegap'" +
", function (require, exports, module) {\n" + file + "});\n";
});

return output;
},

bundle: function (platform) {
var output = "";

//include LICENSE
output += include("LICENSE", function (file) {
return "/*\n" + file + "\n*/\n";
});

//include require
output += include("thirdparty/almond.js");

//include modules
output += this.modules(platform);

//include bootstrap
output += include('lib/bootstrap.js');

@@ -9,8 +9,11 @@
<script type="text/javascript" src="../thirdparty/jasmine/jasmine.js"></script>
<script type="text/javascript" src="../thirdparty/jasmine/jasmine-html.js"></script>

<script type="text/javascript" src="../thirdparty/almond.js"></script>

<script type="text/javascript">
(function() {
require.unordered = true;
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;

@@ -38,12 +41,11 @@
})();
</script>

<script type="text/javascript" src="../pkg/phonegap.ios.js"></script>

<!-- include tests files here... -->
<script type="text/javascript" src="test.accelerometer.js"></script>

<script type="text/javascript">
"##MODULES##"
</script>

<!-- TESTS -->
</head>

<body>
@@ -1,42 +1,53 @@
var fs = require('fs'),
_path = require('path');

function collect(path, files, matches) {
matches = matches || function (path) {
return path.match(/\.js$/);
};

if (fs.statSync(path).isDirectory()) {
fs.readdirSync(path).forEach(function (item) {
collect(_path.join(path, item), files, matches);
});
} else if (matches(path)) {
files.push(path);
}
}

module.exports = function () {
var connect = require('connect'),
fs = require('fs'),
packager = require('./packager'),
tests = [],
html = fs.readFileSync(__dirname + "/btest/test.html", "utf-8"),
html = fs.readFileSync(__dirname + "/../build/test.html", "utf-8"),
doc,
modules,
specs,
app = connect(
connect.static(__dirname + "/../lib/"),
connect.static(__dirname + "/../"),
connect.static(__dirname + "/../test/"),
connect.router(function (app) {
app.get('/', function (req, res) {
res.writeHead(200, {
"Cache-Control": "no-cache",
"Content-Type": "text/html"
});
tests = [];
collect(__dirname + "/../test", tests);

specs = tests.map(function (file, path) {
return '<script src="' + file.replace(/^.*test/, "test") +
'" type="text/javascript" charset="utf-8"></script>';
}).join();
modules = packager.modules('ios');

doc = html.replace(/<!-- TESTS -->/g, specs).replace(/"##MODULES##"/g, modules);
res.end(doc);
});
})
);

utils.collect(__dirname + "/../lib", libs);
utils.collect(__dirname + "/../test", tests);

modules = libs.reduce(function (str, file) {
str += '"' + file.replace(/^.*lib\//, "").replace(/\.js$/, "") + '",\n';
return str;
}, "").replace(/\,\n$/g, "\n");

specs = tests.reduce(function (str, file) {
str += '<script src="' +
file.replace(/^.*test/, "test") +
'" type="text/javascript" charset="utf-8"></script>\n';
return str;
}, "");

doc = html.replace(/<!-- SPECS -->/g, specs).replace(/"##FILES##"/g, modules);

app.listen(3000);

process.stdout.write("Test Server running on:");
@@ -0,0 +1,4 @@
npm install -g jake
npm install connect
git submodule init
git submodule update
Empty file.
@@ -0,0 +1,50 @@
describe("builder", function () {

var builder = require('phonegap/builder');

it("includes the module into the target", function () {

var target = {},
objects = {
foo: {
path: "phonegap/plugin/navigator"
}
};


builder.build(objects).into(target);

expect(target.foo).toBeDefined();
expect(target.foo).toBe(require("phonegap/plugin/navigator"));
});

it("returns an empty object literal if no path", function () {
var target = {},
objects = {cat: {}};

builder.build(objects).into(target);
expect(target.cat).toBeDefined();
});

it("builds out the children", function () {

var target = {},
objects = {
homer: {
children: {
bart: {},
lisa: {},
maggie: {
path: "phonegap/plugin/navigator"
}
}
}
};

builder.build(objects).into(target);

expect(target.homer.bart).toBeDefined();
expect(target.homer.maggie).toBe(require('phonegap/plugin/navigator'));
expect(target.homer.lisa).toBeDefined();
});
});
@@ -0,0 +1,18 @@
describe("network", function () {
var network = require('phonegap/plugin/network');

//TODO: There is a lot of code executed on the first require call to this plugin
// we should refactor or find a good way to call and test this code.
//
// since exec is a spy we can scrounge the list of calls to find it, but I would
// rather refactor to have something a little cleaner (maybe move this code into the init
// routine for the platform)

it("can get the network info", function () {
var success = jasmine.createSpy(),
error = jasmine.createSpy();

network.getInfo(success, error);
expect(PhoneGap.exec).toHaveBeenCalledWith(success, error, "Network Status", "getConnectionInfo", []);
});
});
@@ -0,0 +1,49 @@
describe("notification", function () {
var notification = require('phonegap/plugin/notification');
describe("when alerting", function () {
it("defaults the title to Alert and the button to OK", function () {
notification.alert("u can't touch this");
expect(PhoneGap.exec).toHaveBeenCalledWith(
undefined, null, "Notification", "alert",
["u can't touch this", "Alert", "OK"]);
});

it("passes the provided params to the exec method", function () {
var cb = jasmine.createSpy();
notification.alert("hammertime", cb, "STOP!", "GO");
expect(PhoneGap.exec).toHaveBeenCalledWith(
cb, null, "Notification", "alert",
["hammertime", "STOP!", "GO"]);
});
});

describe("when confirming", function () {
it("defaults the title to Confirm and the buttons to OK,Cancel", function () {
notification.confirm("hahh shhh push it?");
expect(PhoneGap.exec).toHaveBeenCalledWith(
undefined, null, "Notification", "confirm",
["hahh shhh push it?", "Confirm", "OK,Cancel"]);
});

it("passes the provided params to the exec method", function () {
var cb = jasmine.createSpy();
notification.confirm("and thats the way it is", cb, "It's like that", "Yes,Yes");
expect(PhoneGap.exec).toHaveBeenCalledWith(
cb, null, "Notification", "confirm",
["and thats the way it is", "It's like that", "Yes,Yes"]);
});
});

it("causes the device to vibrate", function () {
notification.vibrate(1000);
expect(PhoneGap.exec).toHaveBeenCalledWith(
null, null, "Notification", "vibrate", [1000]);
});

it("causes the device to beep", function () {
notification.beep(9001);
//It's over 9000!
expect(PhoneGap.exec).toHaveBeenCalledWith(
null, null, "Notification", "beep", [9001]);
});
});

0 comments on commit 20c7725

Please sign in to comment.