Skip to content

Commit

Permalink
[init] implement std page generator
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasMadsen committed Jul 10, 2012
0 parents commit 9032db1
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

/node_modules/

111 changes: 111 additions & 0 deletions blow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* Copyright (c) 2012 Andreas Madsen
* MIT License
*/

var fs = require('fs');
var path = require('path');
var tako = require('tako');
var domstream = require('domstream');

module.exports = function(port, files, style) {

var app = tako();

var mochaCorePath = path.resolve( path.dirname(require.resolve('mocha')), 'mocha.js');
app.route('/file/mocha.js').file(mochaCorePath);

var mochaStylePath = path.resolve( path.dirname(require.resolve('mocha')), 'mocha.css');
app.route('/file/mocha.css').file(mochaStylePath);

var chaiPath = path.resolve( path.dirname(require.resolve('chai')), 'chai.js');
app.route('/file/chai.js').file(chaiPath);

// find the shared dirname
var root = path.dirname(files[0]), i, l;
for (i = 1, l = files.length; i < l; i++) {
var testDir = path.dirname(files[i]);

while (root !== testDir) {
root = path.dirname(root);
testDir = path.dirname(testDir);
}
}

// create pathmap
var map = {};
for (i = 0, l = files.length; i < l; i++) {
map[ files[i].slice(root.length) ] = files[i];
}

// route all static test files
Object.keys(map).forEach(function (relative) {
console.log('/test' + relative + ' = ' + map[relative]);
app.route('/test' + relative).file(map[relative]);
});

//
var indexFile = generateIndex(map, style);
app.route('/').html(indexFile);

// route all subtest pages
Object.keys(map).forEach(function (relative) {
var content = generateTest(relative, style);
app.route(relative).html(content);
});

app.httpServer.listen(port, function () {
console.log('mocha server online at http://127.0.0.1:' + port);
});
};

// Standart output
var base = domstream( fs.readFileSync( path.resolve(path.dirname(module.filepath), 'index.html') ) );

// generate the master testsuite
function generateIndex(files, style) {
var document = base.copy();

// modify title
var title = document.find().only().elem('title').toValue();
title.setContent('Mocha Tests - all');

// add testcases
var head = document.find().only().elem('head').toValue();

// set mocha style
head.append('<script>mocha.setup("' + style + '")</script>');

// bind testcases
Object.keys(files).forEach(function (relative) {
head.append('<script src="/test' + relative + '"></script>');
});

// bind mocha runner
head.append('<script> window.onload = function () { mocha.run() }; </script>');

return document.content;
}

// generate individual testcases
function generateTest(file, style) {
var document = base.copy();

// modify title
var title = document.find().only().elem('title').toValue();
title.setContent('Mocha Tests - ' + file);

// add testcases
var head = document.find().only().elem('head').toValue();

// set mocha style
head.append('<script>mocha.setup("' + style + '")</script>');

// bind testcases
head.append('<script src="/test' + file + '"></script>');

// bind mocha runner
head.append('<script> window.onload = function () { mocha.run() }; </script>');

return document.content;
}
15 changes: 15 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>

<link rel="stylesheet" href="/file/mocha.css" />
<script src="/file/chai.js"></script>
<script src="/file/mocha.js"></script>

</head>
<body>
<div id="mocha"></div>
</body>
</html>
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "blow",
"description": "simple boilerplate for the testframework mocha brower",
"version": "0.1.0",
"author": "Andreas Madsen <amwebdk@gmail.com>",
"main": "./blow.js",
"repository" : {
"type": "git",
"url": "git://github.com/AndreasMadsen/blow.git"
},
"keywords": [
"test",
"testsuite",
"test framework",
"browser"
],
"devDependencies": {
"mocha" : "1.3.x",
"chai": "1.1.x",
"domstream": "0.3.x",
"tako": "0.3.x"
},
"license": "MIT",
"engines": {
"node": "0.6 || 0.8",
"npm": "1"
}
}

0 comments on commit 9032db1

Please sign in to comment.