Skip to content

Commit 6c0d4b3

Browse files
author
Diego Lafuente
committed
completed files and testing for the workshop
1 parent 1647e8b commit 6c0d4b3

File tree

9 files changed

+85
-24
lines changed

9 files changed

+85
-24
lines changed

Gruntfile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ module.exports = function(grunt) {
77
"Gruntfile.js",
88
"*.js",
99
"http/*.js",
10-
"interpreter/batch/*.js",
11-
"net/integration/*.js",
12-
"unit-tests/io/*.js"
10+
"interpreter/*.js",
11+
"net/*.js",
12+
"unit-tests/*.js"
1313
],
1414
options: {
1515
// use closest-through-parent jshint configuration file

http/index-events.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
"use strict";
22

3-
var http = require('http');
4-
var fs = require('fs');
3+
var http = require("http");
4+
var fs = require("fs");
55

6-
var server = http.Server();
6+
var server = new http.Server();
77

88
server.on("request", serve);
99

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

http/index-express.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
"use strict";
22

33
var express = require("express");
4+
var fs = require("fs");
45

56
var server = express();
67

78
server.get("/", serve);
89

9-
server.listen(9615, function(error, result) {
10+
server.listen(9615, function(error) {
1011
if (error) {
1112
console.log("Error when trying to listen in port 9615.");
1213
return;
1314
}
1415
console.log("Listening on port 9615");
15-
})
16+
});
1617

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

http/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"use strict";
22

3-
var http = require('http');
4-
var fs = require('fs');
3+
var http = require("http");
4+
var fs = require("fs");
55

66
var server = http.createServer(serve);
77

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

net/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var net = require('net');
3+
var net = require("net");
44

55
var server = net.Server();
66

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "node-workshop",
3+
"description": "A set of code snippets developed for a MallorcaJS workshop",
4+
"version": "0.0.1",
5+
"author": {
6+
"name": "Diego Lafuente",
7+
"email": "dlafuente@tuitravel-ad.com"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/DiegoTUI/node-workshop.git"
12+
},
13+
"dependencies": {
14+
"express": "4.11.x",
15+
"testing":"0.1.x",
16+
"grunt": "0.4.x",
17+
"grunt-contrib-jshint": "0.10.x"
18+
},
19+
"scripts": {
20+
"test": "node test.js"
21+
},
22+
"engines": {
23+
"node": ">= 0.10.0"
24+
}
25+
}

test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
3+
// requires
4+
var testing = require("testing");
5+
6+
/**
7+
* Run all module tests.
8+
*/
9+
exports.test = function(callback) {
10+
var tests = {};
11+
var files = {pad: "./unit-tests/pad.js"};
12+
13+
for(var key in files) {
14+
tests[key] = require(files[key]).test;
15+
}
16+
17+
testing.run(tests, callback);
18+
};
19+
20+
// run tests if invoked directly
21+
if (__filename == process.argv[1])
22+
{
23+
exports.test(testing.show);
24+
}

unit-tests/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
var pad = require("./pad.js").pad;
4+
5+
console.log("3 padded: " + pad(3,3));

unit-tests/pad.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,31 @@ var testing = require("testing");
44

55
exports.pad = function(number, digits) {
66
var padded = number.toString();
7-
while (padded.length < digits)
8-
{
7+
while (padded.length < digits) {
98
padded = "0" + padded;
109
}
1110
return padded;
12-
}
11+
};
1312

1413
/********* UNIT TESTS **********/
1514

1615
function testPad(callback) {
1716
var number = 3;
17+
var number2digits = 54;
1818
testing.assertEquals(exports.pad(number, 0), "3", "wrong padding for 0 digits", callback);
1919
testing.assertEquals(exports.pad(number, 1), "3", "wrong padding for 1 digits", callback);
2020
testing.assertEquals(exports.pad(number, 2), "03", "wrong padding for 2 digits", callback);
2121
testing.assertEquals(exports.pad(number, 3), "003", "wrong padding for 3 digits", callback);
22+
testing.assertEquals(exports.pad(number2digits, 3), "054", "wrong padding for 2 digits number", callback);
2223
testing.success(callback);
2324
}
2425

26+
// Run all tests
27+
exports.test = function(callback) {
28+
testing.run([testPad], callback);
29+
}
30+
2531
// start tests if invoked directly
2632
if (__filename == process.argv[1]) {
27-
testing.run([testPad], testing.show);
33+
exports.test(testing.show);
2834
}

0 commit comments

Comments
 (0)