Skip to content

Commit 96b2f40

Browse files
committed
Adapt koans to be run in plain NodeJS
1 parent 3e3c84c commit 96b2f40

20 files changed

+345
-265
lines changed
File renamed without changes.

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"main": "topics/about_asserts.js",
3+
"name": "javascript-koans",
4+
"description": "Update ======",
5+
"version": "1.0.0",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node ./",
9+
"koans": ""
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/pipetus/JavaScript-Koans.git"
14+
},
15+
"keywords": [
16+
"javascript",
17+
"koans"
18+
],
19+
"author": "",
20+
"license": "ISC",
21+
"bugs": {
22+
"url": "https://github.com/pipetus/JavaScript-Koans/issues"
23+
},
24+
"homepage": "https://github.com/pipetus/JavaScript-Koans#readme"
25+
}

support/koans.js

Lines changed: 68 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,70 +14,78 @@ Array.prototype.equalTo = function(compareTo) {
1414
return true;
1515
};
1616

17-
(function() {
17+
const test = (message, assertion) => {
18+
console.log('\x1b[31m\n')
19+
assertion()
20+
console.log('\x1b[0m\x1b[42m %s \x1b[0m\n', message)
21+
}
1822

19-
var lastAssertLogReason, ignoreFurtherFailures = false;
20-
var zenMessages = [
21-
"The path to enlightenment has many stones",
22-
"Do not stray from your path, for enlightenment comes with perseverance",
23-
"The only Zen you find on tops of mountains is the Zen you bring there",
24-
"Enlightenment occurs when someone becomes inspired by information and uses it to enhance their life",
25-
"Be master of mind rather than mastered by mind",
26-
"Zen is not some kind of excitement, but concentration on our usual everyday routine",
27-
"I think self-awareness is probably the most important thing towards being a champion",
28-
"The reward of all action is to be found in enlightenment",
29-
"lasting enlightenment can be achieved only through persistent exercise of real love",
30-
"The real meaning of enlightenment is to gaze with undimmed eyes on all darkness",
31-
"Do not think you will necessarily be aware of your own enlightenment",
32-
"Enlightenment must come little by little - otherwise it would overwhelm",
33-
"The greatest gift is to give people your enlightenment, to share it. It has to be the greatest",
34-
"In the beginner's mind there are many possibilities, but in the expert's mind there are few",
35-
"Only the hand that erases can write the true thing",
36-
"Enlightenment is ego's ultimate disappointment",
37-
"Man suffers only because he takes seriously what the gods made for fun",
38-
"It is easy to believe we are each waves and forget we are also the ocean",
39-
"Working out is my biggest hobby. It's my Zen hour. I just zone out",
40-
"A self-motivation is an enlightenment of mind, empowerment of heart and enrichment of soul to arise, awake and ascend to achieve the noble and coveted goal even if it entails walking on its enervating path all alone"
41-
];
23+
module.exports = { __, test }
4224

43-
QUnit.config.reorder = false;
25+
// (function() {
4426

45-
QUnit.done(function(results) {
46-
var failures = results.failed;
47-
var total = results.total;
48-
if (failures > 0) {
49-
var failed = $('ol#qunit-tests > li.fail');
50-
failed.hide();
51-
$(failed[0]).show();
52-
}
53-
if (failures < total) {
54-
$('h3.welcome_message').hide();
55-
}
56-
if (failures > 0) {
57-
$("#zen-help").show();
58-
}
59-
$("body").scrollTop($(document).height());
60-
});
27+
// var lastAssertLogReason, ignoreFurtherFailures = false;
28+
// var zenMessages = [
29+
// "The path to enlightenment has many stones",
30+
// "Do not stray from your path, for enlightenment comes with perseverance",
31+
// "The only Zen you find on tops of mountains is the Zen you bring there",
32+
// "Enlightenment occurs when someone becomes inspired by information and uses it to enhance their life",
33+
// "Be master of mind rather than mastered by mind",
34+
// "Zen is not some kind of excitement, but concentration on our usual everyday routine",
35+
// "I think self-awareness is probably the most important thing towards being a champion",
36+
// "The reward of all action is to be found in enlightenment",
37+
// "lasting enlightenment can be achieved only through persistent exercise of real love",
38+
// "The real meaning of enlightenment is to gaze with undimmed eyes on all darkness",
39+
// "Do not think you will necessarily be aware of your own enlightenment",
40+
// "Enlightenment must come little by little - otherwise it would overwhelm",
41+
// "The greatest gift is to give people your enlightenment, to share it. It has to be the greatest",
42+
// "In the beginner's mind there are many possibilities, but in the expert's mind there are few",
43+
// "Only the hand that erases can write the true thing",
44+
// "Enlightenment is ego's ultimate disappointment",
45+
// "Man suffers only because he takes seriously what the gods made for fun",
46+
// "It is easy to believe we are each waves and forget we are also the ocean",
47+
// "Working out is my biggest hobby. It's my Zen hour. I just zone out",
48+
// "A self-motivation is an enlightenment of mind, empowerment of heart and enrichment of soul to arise, awake and ascend to achieve the noble and coveted goal even if it entails walking on its enervating path all alone"
49+
// ];
6150

62-
QUnit.log(function(result) {
63-
lastAssertLogReason = result.message;
64-
});
51+
// QUnit.config.reorder = false;
6552

66-
QUnit.testDone(function(result) {
67-
var message;
68-
if (!ignoreFurtherFailures && result.failed > 0) {
69-
ignoreFurtherFailures = true;
70-
message = "" + randomZenMessage() + "\nTry meditating on this: " + result.module + ": " + result.name + " (" + lastAssertLogReason + ")";
71-
$("#zen-help").html(message.replace(/\n/g, "<br /><br />"));
72-
console.log(message);
73-
}
74-
});
53+
// QUnit.done(function(results) {
54+
// var failures = results.failed;
55+
// var total = results.total;
56+
// if (failures > 0) {
57+
// var failed = $('ol#qunit-tests > li.fail');
58+
// failed.hide();
59+
// $(failed[0]).show();
60+
// }
61+
// if (failures < total) {
62+
// $('h3.welcome_message').hide();
63+
// }
64+
// if (failures > 0) {
65+
// $("#zen-help").show();
66+
// }
67+
// $("body").scrollTop($(document).height());
68+
// });
7569

76-
function randomZenMessage() {
77-
var randomIndex = Math.floor(Math.random() * zenMessages.length);
78-
var zenMessage = zenMessages[randomIndex];
79-
zenMessage = zenMessage.charAt(0).toUpperCase() + zenMessage.substr(1);
80-
return "" + zenMessage + ".";
81-
}
70+
// QUnit.log(function(result) {
71+
// lastAssertLogReason = result.message;
72+
// });
73+
74+
// QUnit.testDone(function(result) {
75+
// var message;
76+
// if (!ignoreFurtherFailures && result.failed > 0) {
77+
// ignoreFurtherFailures = true;
78+
// message = "" + randomZenMessage() + "\nTry meditating on this: " + result.module + ": " + result.name + " (" + lastAssertLogReason + ")";
79+
// $("#zen-help").html(message.replace(/\n/g, "<br /><br />"));
80+
// console.log(message);
81+
// }
82+
// });
83+
84+
// function randomZenMessage() {
85+
// var randomIndex = Math.floor(Math.random() * zenMessages.length);
86+
// var zenMessage = zenMessages[randomIndex];
87+
// zenMessage = zenMessage.charAt(0).toUpperCase() + zenMessage.substr(1);
88+
// return "" + zenMessage + ".";
89+
// }
8290

83-
})();
91+
// })();

topics/about_arrays.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1-
module("About Arrays (topics/about_arrays.js)");
1+
// module("About Arrays (topics/about_arrays.js)");
2+
const { equal, deepEqual } = require('assert')
3+
const { __, test } = require('../support/koans')
24

3-
test("array literal syntax and indexing", function() {
4-
var favouriteThings = ["cellar door", 42, true]; // note that array elements do not have to be of the same type
5+
test("array literal syntax and indexing", () => {
6+
const favouriteThings = ["cellar door", 42, true]; // note that array elements do not have to be of the same type
57
equal(__, favouriteThings[0], 'what is in the first position of the array?');
68
equal(__, favouriteThings[1], 'what is in the second position of the array?');
79
equal(__, favouriteThings[2], 'what is in the third position of the array?');
810
});
911

10-
test("array type", function() {
12+
test("array type", () => {
1113
equal(__, typeof([]), 'what is the type of an array?');
1214
});
1315

14-
test("length", function() {
15-
var collection = ['a','b','c'];
16+
test("length", () => {
17+
const collection = ['a','b','c'];
1618
equal(__, collection.length, 'what is the length of the collection array?');
1719
});
1820

19-
test("splice", function() {
20-
var daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
21-
var workingWeek = daysOfWeek.splice(__, __);
22-
var weekend = daysOfWeek;
21+
test("splice", () => {
22+
const daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
23+
const workingWeek = daysOfWeek.splice(__, __);
24+
const weekend = daysOfWeek;
2325

2426
deepEqual(workingWeek, ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], 'what is the value of workingWeek?');
2527
deepEqual(weekend, ['Saturday', 'Sunday'], 'what is the value of weekend?');
2628
});
2729

28-
test("stack methods", function() {
29-
var stack = [];
30+
test("stack methods", () => {
31+
const stack = [];
3032
stack.push("first");
3133
stack.push("second");
3234

3335
equal(__, stack.pop(), 'what will be the first value popped off the stack?');
3436
equal(__, stack.pop(), 'what will be the second value popped off the stack?');
3537
});
3638

37-
test("queue methods", function() {
38-
var queue = [];
39+
test("queue methods", () => {
40+
const queue = [];
3941
queue.push("first");
4042
queue.push("second");
4143
queue.unshift("third");

topics/about_asserts.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
// module("About Asserts (topics/about_asserts.js)");
2+
const { ok, equal } = require('assert')
3+
// const { __, test } = require('../support/koans')
4+
const koans = require('../support/koans')
5+
// const __ = koans.__
6+
// const test = koans.test
7+
const { __ } = koans
8+
const { test } = koans
19

2-
module("About Asserts (topics/about_asserts.js)");
10+
test("ok", () => {
11+
assert.ok(__ === true, 'what will satisfy the ok assertion?');
12+
})
313

4-
test("ok", function() {
5-
ok(__ === true, 'what will satisfy the ok assertion?');
6-
});
14+
test("not ok", () => {
15+
assert.ok(__ === false, 'what is a false value?');
16+
})
717

8-
test("not ok", function() {
9-
ok(__ === false, 'what is a false value?');
10-
});
11-
12-
test("equal", function() {
13-
equal(__, 1 + 1, 'what will satisfy the equal assertion?');
14-
});
18+
test("ok", () => {
19+
assert.equal(__, 1 + 1, 'what will satisfy the equal assertion?');
20+
})

topics/about_assignment.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
// module("About Assignment (topics/about_assignment.js)");
2+
const { equal } = require('assert')
3+
const { __, test } = require('../support/koans')
14

2-
module("About Assignment (topics/about_assignment.js)");
3-
4-
test("local variables", function() {
5-
var temp = __;
5+
test("local variables", () => {
6+
const temp = __;
67
equal(temp, 1, "Assign a value to the variable temp");
78
});
89

9-
test("global variables", function() {
10+
test("global variables", () => {
1011
temp = 1; // Not using var is an example. Always use var in practise.
11-
equal(window.__, temp, 'global variables are assigned to the window object');
12+
equal(global.__, temp, 'global variables are assigned to the global object');
1213
});

topics/about_control_structures.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
1-
module("About Control Structures (topics/about_control_structures.js)");
1+
// module("About Control Structures (topics/about_control_structures.js)");
2+
const { equal } = require('assert')
3+
const { __, test } = require('../support/koans')
24

3-
test("if", function() {
4-
var isPositive = false;
5+
test("if", () => {
6+
let isPositive = false;
57
if (2 > 0) {
68
isPositive = true;
79
}
810
equal(__, isPositive, 'what is the value of isPositive?');
911
});
1012

11-
test("for", function() {
12-
var counter = 10;
13-
for (var i = 1; i <= 3; i++) {
13+
test("for", () => {
14+
let counter = 10;
15+
for (let i = 1; i <= 3; i++) {
1416
counter = counter + i;
1517
}
1618
equal(__, counter, 'what is the value of counter?');
1719
});
1820

19-
test("for in", function() {
21+
test("for in", () => {
2022
// this syntax will be explained in about objects
21-
var person = {
23+
const person = {
2224
name: "Amory Blaine",
2325
age: 102
2426
};
25-
var result = "";
27+
let result = "";
28+
2629
// for in enumerates the property names of an object
27-
for (var property_name in person) {
30+
for (let property_name in person) {
2831
result = result + property_name;
2932
}
3033
equal(__, result, 'what is the value of result?');
3134
});
3235

33-
test("ternary operator", function() {
34-
var fruit = true ? "apple" : "orange";
36+
test("ternary operator", () => {
37+
let fruit = true ? "apple" : "orange";
3538
equal(__, fruit, 'what is the value of fruit?');
3639

3740
fruit = false ? "apple" : "orange";
3841
equal(__, fruit, 'now what is the value of fruit?');
3942
});
4043

41-
test("switch", function() {
42-
var result = 0;
44+
test("switch", () => {
45+
let result = 0;
4346
switch (2) {
4447
case 1:
4548
result = 1;
@@ -51,8 +54,8 @@ test("switch", function() {
5154
equal(__, result, 'what is the value of result?');
5255
});
5356

54-
test("switch default case", function() {
55-
var result = "Pippin";
57+
test("switch default case", () => {
58+
let result = "Pippin";
5659
switch ("m") {
5760
case "f":
5861
result = "Frodo";
@@ -67,7 +70,7 @@ test("switch default case", function() {
6770
equal(__, result, 'what is the value of result?');
6871
});
6972

70-
test("null coalescing", function() {
71-
var result = null || "a value";
73+
test("null coalescing", () => {
74+
let result = null || "a value";
7275
equal(__, result, 'what is the value of result?');
7376
});

topics/about_equality.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1+
// module("About Equality (topics/about_equality.js)");
2+
const { equal, ok } = require('assert')
3+
const { __, test } = require('../support/koans')
14

2-
module("About Equality (topics/about_equality.js)");
3-
4-
test("numeric equality", function() {
5+
test("numeric equality", () => {
56
equal(3 + __, 7, "");
67
});
78

8-
test("string equality", function() {
9+
test("string equality", () => {
910
equal("3" + __, "37", "concatenate the strings");
1011
});
1112

12-
test("equality without type coercion", function() {
13+
test("equality without type coercion", () => {
1314
ok(3 === __, 'what is exactly equal to 3?');
1415
});
1516

16-
test("equality with type coercion", function() {
17+
test("equality with type coercion", () => {
1718
ok(3 == "__", 'what string is equal to 3, with type coercion?');
1819
});
1920

20-
test("string literals", function() {
21+
test("string literals", () => {
2122
equal(__, "frankenstein", "quote types are interchangable, but must match.");
2223
equal(__, 'frankenstein', "quote types can use both single and double quotes.");
2324
});

0 commit comments

Comments
 (0)