Skip to content

Commit

Permalink
completed files and testing for the workshop
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Lafuente committed Jan 16, 2015
1 parent 1647e8b commit 6c0d4b3
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = function(grunt) {
"Gruntfile.js",
"*.js",
"http/*.js",
"interpreter/batch/*.js",
"net/integration/*.js",
"unit-tests/io/*.js"
"interpreter/*.js",
"net/*.js",
"unit-tests/*.js"
],
options: {
// use closest-through-parent jshint configuration file
Expand Down
12 changes: 6 additions & 6 deletions http/index-events.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"use strict";

var http = require('http');
var fs = require('fs');
var http = require("http");
var fs = require("fs");

var server = http.Server();
var server = new http.Server();

server.on("request", serve);

function serve(request, response) {
console.log("Reading index.html and serving it")
var index = fs.readFileSync('index.html');
response.writeHead(200, {'Content-Type': 'html'});
console.log("Reading index.html and serving it");
var index = fs.readFileSync("index.html");
response.writeHead(200, {"Content-Type": "html"});
response.end(index);
}

Expand Down
11 changes: 6 additions & 5 deletions http/index-express.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
"use strict";

var express = require("express");
var fs = require("fs");

var server = express();

server.get("/", serve);

server.listen(9615, function(error, result) {
server.listen(9615, function(error) {
if (error) {
console.log("Error when trying to listen in port 9615.");
return;
}
console.log("Listening on port 9615");
})
});

function serve(request, response) {
console.log("Reading index.html and serving it")
var index = fs.readFileSync('index.html');
response.writeHead(200, {'Content-Type': 'html'});
console.log("Reading index.html and serving it");
var index = fs.readFileSync("index.html");
response.writeHead(200, {"Content-Type": "html"});
response.end(index);
}
10 changes: 5 additions & 5 deletions http/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use strict";

var http = require('http');
var fs = require('fs');
var http = require("http");
var fs = require("fs");

var server = http.createServer(serve);

function serve(request, response) {
console.log("Reading index.html and serving it")
var index = fs.readFileSync('index.html');
response.writeHead(200, {'Content-Type': 'html'});
console.log("Reading index.html and serving it");
var index = fs.readFileSync("index.html");
response.writeHead(200, {"Content-Type": "html"});
response.end(index);
}

Expand Down
2 changes: 1 addition & 1 deletion net/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

var net = require('net');
var net = require("net");

var server = net.Server();

Expand Down
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "node-workshop",
"description": "A set of code snippets developed for a MallorcaJS workshop",
"version": "0.0.1",
"author": {
"name": "Diego Lafuente",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "https://github.com/DiegoTUI/node-workshop.git"
},
"dependencies": {
"express": "4.11.x",
"testing":"0.1.x",
"grunt": "0.4.x",
"grunt-contrib-jshint": "0.10.x"
},
"scripts": {
"test": "node test.js"
},
"engines": {
"node": ">= 0.10.0"
}
}
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

// requires
var testing = require("testing");

/**
* Run all module tests.
*/
exports.test = function(callback) {
var tests = {};
var files = {pad: "./unit-tests/pad.js"};

for(var key in files) {
tests[key] = require(files[key]).test;
}

testing.run(tests, callback);
};

// run tests if invoked directly
if (__filename == process.argv[1])
{
exports.test(testing.show);
}
5 changes: 5 additions & 0 deletions unit-tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

var pad = require("./pad.js").pad;

console.log("3 padded: " + pad(3,3));
14 changes: 10 additions & 4 deletions unit-tests/pad.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@ var testing = require("testing");

exports.pad = function(number, digits) {
var padded = number.toString();
while (padded.length < digits)
{
while (padded.length < digits) {
padded = "0" + padded;
}
return padded;
}
};

/********* UNIT TESTS **********/

function testPad(callback) {
var number = 3;
var number2digits = 54;
testing.assertEquals(exports.pad(number, 0), "3", "wrong padding for 0 digits", callback);
testing.assertEquals(exports.pad(number, 1), "3", "wrong padding for 1 digits", callback);
testing.assertEquals(exports.pad(number, 2), "03", "wrong padding for 2 digits", callback);
testing.assertEquals(exports.pad(number, 3), "003", "wrong padding for 3 digits", callback);
testing.assertEquals(exports.pad(number2digits, 3), "054", "wrong padding for 2 digits number", callback);
testing.success(callback);
}

// Run all tests
exports.test = function(callback) {
testing.run([testPad], callback);
}

// start tests if invoked directly
if (__filename == process.argv[1]) {
testing.run([testPad], testing.show);
exports.test(testing.show);
}

0 comments on commit 6c0d4b3

Please sign in to comment.