Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdavidlaing committed Nov 6, 2010
1 parent 88d116e commit 3ee6b5e
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 129 deletions.
4 changes: 3 additions & 1 deletion KoansRunner.html
Expand Up @@ -21,8 +21,10 @@
<script type="text/javascript" src="koans/AboutObjects.js"></script>
<script type="text/javascript" src="koans/AboutMutability.js"></script>
<script type="text/javascript" src="koans/AboutLambda.js"></script>
<script type="text/javascript" src="koans/AboutHigherOrderFunctions.js"></script>
<script type="text/javascript" src="koans/AboutHigherOrderFunctions.js"></script>
<script type="text/javascript" src="koans/AboutInheritance.js"></script>
<script type="text/javascript" src="koans/aboutApplyingWhatWeHaveLearnt.js"></script>

</head>
<body>

Expand Down
12 changes: 6 additions & 6 deletions koans/AboutApplyingWhatWeHaveLearnt.js
Expand Up @@ -32,11 +32,11 @@ describe("About Applying What We Have Learnt", function() {
return hasInvalidOperation;
};

expect(__).toBe(findNeedle(operations));
expect(findNeedle(operations)).toBe(__);
});

it("should find needle in a haystack (functional)", function () {
expect(__).toBe(df.some(operations, "x.direction === 'FWD' && x.distance > 100"));
expect(df.some(operations, "x.direction === 'FWD' && x.distance > 100")).toBe(__);
});

/*********************************************************************************/
Expand All @@ -50,7 +50,7 @@ describe("About Applying What We Have Learnt", function() {
}
}

expect(__).toBe(sum);
expect(sum).toBe(__);
});

it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () {
Expand All @@ -62,7 +62,7 @@ describe("About Applying What We Have Learnt", function() {
};
var numbers = df.repeat(1000, "+1", 1);

expect(__).toBe(df.reduce(numbers, sumIfMultipleOf3Or5, 0));
expect(df.reduce(numbers, sumIfMultipleOf3Or5, 0)).toBe(__);
});

/*********************************************************************************/
Expand All @@ -81,7 +81,7 @@ describe("About Applying What We Have Learnt", function() {
i+=1;
} while (currentFib < 4000000);

expect(__).toBe(sum);
expect(sum).toBe(__);
});

it("should find the sum of all the even valued terms in the fibonacci sequence which do not exceed four million (functional)", function () {
Expand All @@ -97,7 +97,7 @@ describe("About Applying What We Have Learnt", function() {
var fib = df.until("item[0] > 4000000", calcNextFibTuple, [0,1]);
var sum = df.reduce(fib, addEven, 0);

expect(__).toBe(sum);
expect(sum).toBe(__);
});

/*********************************************************************************/
Expand Down
64 changes: 32 additions & 32 deletions koans/AboutArrays.js
Expand Up @@ -3,56 +3,56 @@ describe("About Arrays", function() {
//We shall contemplate truth by testing reality, via spec expectations.
it("should create arrays", function() {
var emptyArray = [];
expect(__).toBe(typeof(emptyArray)); //A mistake? - http:javascript.crockford.com/remedial.html
expect(__).toBe(emptyArray.length);
expect(typeof(emptyArray)).toBe(__); //A mistake? - http:javascript.crockford.com/remedial.html
expect(emptyArray.length).toBe(__);

var multiTypeArray = [0, 1, "two", function () { return 3; }, {value1: 4, value2: 5}, [6, 7]];
expect(__).toBe(multiTypeArray[0]);
expect(__).toBe(multiTypeArray[2]);
expect(__).toBe(multiTypeArray[3]());
expect(__).toBe(multiTypeArray[4].value1);
expect(__).toBe(multiTypeArray[4]["value2"]);
expect(__).toBe(multiTypeArray[5][0]);
expect(multiTypeArray[0]).toBe(__);
expect(multiTypeArray[2]).toBe(__);
expect(multiTypeArray[3]()).toBe(__);
expect(multiTypeArray[4].value1).toBe(__);
expect(multiTypeArray[4]["value2"]).toBe(__);
expect(multiTypeArray[5][0]).toBe(__);
});

it("should understand array literals", function () {
var array = [];
expect([]).toEqual(array);
expect(array).toEqual([]);

array[0] = 1;
expect([1]).toEqual(array);
expect(array).toEqual([1]);

array[1] = 2;
expect([1, __]).toEqual(array);
expect(array).toEqual([1, __]);

array.push(3);
expect(__).toEqual(array);
expect(array).toEqual(__);
});

it("should understand array length", function () {
var fourNumberArray = [1, 2, 3, 4];

expect(__).toBe(fourNumberArray.length);
expect(fourNumberArray.length).toBe(__);
fourNumberArray.push(5, 6);
expect(__).toBe(fourNumberArray.length);
expect(fourNumberArray.length).toBe(__);

var tenEmptyElementArray = new Array(10);
expect(__).toBe(tenEmptyElementArray.length);
expect(tenEmptyElementArray.length).toBe(__);

tenEmptyElementArray.length = 5;
expect(__).toBe(tenEmptyElementArray.length);
expect(tenEmptyElementArray.length).toBe(__);
});

it("should slice arrays", function () {
var array = ["peanut", "butter", "and", "jelly"];

expect(__).toEqual(array.slice(0, 1));
expect(__).toEqual(array.slice(0, 2));
expect(__).toEqual(array.slice(2, 2));
expect(__).toEqual(array.slice(2, 20));
expect(__).toEqual(array.slice(3, 0));
expect(__).toEqual(array.slice(3, 100));
expect(__).toEqual(array.slice(5, 1));
expect(array.slice(0, 1)).toEqual(__);
expect(array.slice(0, 2)).toEqual(__);
expect(array.slice(2, 2)).toEqual(__);
expect(array.slice(2, 20)).toEqual(__);
expect(array.slice(3, 0)).toEqual(__);
expect(array.slice(3, 100)).toEqual(__);
expect(array.slice(5, 1)).toEqual(__);
});

it("should know array references", function () {
Expand All @@ -62,36 +62,36 @@ describe("About Arrays", function() {
refArray[1] = "changed in function";
}
passedByReference(array);
expect(__).toBe(array[1]);
expect(array[1]).toBe(__);

var assignedArray = array;
assignedArray[5] = "changed in assignedArray";
expect(__).toBe(array[5]);
expect(array[5]).toBe(__);

var copyOfArray = array.slice();
copyOfArray[3] = "changed in copyOfArray";
expect(__).toBe(array[3]);
expect(array[3]).toBe(__);
});

it("should push and pop", function () {
var array = [1, 2];
array.push(3);

expect(__).toEqual(array);
expect(array).toEqual(__);

var poppedValue = array.pop();
expect(__).toBe(poppedValue);
expect(__).toEqual(array);
expect(poppedValue).toBe(__);
expect(array).toEqual(__);
});

it("should shifting arrays", function () {
var array = [1, 2];

array.unshift(3);
expect(__).toEqual(array);
expect(array).toEqual(__);

var shiftedValue = array.shift();
expect(__).toEqual(shiftedValue);
expect(__).toEqual(array);
expect(shiftedValue).toEqual(__);
expect(array).toEqual(__);
});
});
38 changes: 0 additions & 38 deletions koans/AboutExpects.js

This file was deleted.

27 changes: 13 additions & 14 deletions koans/AboutFunctions.js
Expand Up @@ -6,8 +6,7 @@ describe("About Functions", function() {
return a + b;
}

expect(__).toBe(add(1, 2));

expect(add(1, 2)).toBe(__);
});

it("should know internal wariables override outer variables", function () {
Expand All @@ -22,9 +21,9 @@ describe("About Functions", function() {
return message;
}

expect(__).toBe(getMessage());
expect(__).toBe(overrideMessage());
expect(__).toBe(message);
expect(getMessage()).toBe(__);
expect(overrideMessage()).toBe(__);
expect(message).toBe(__);
});

it("should have lexical scoping", function () {
Expand All @@ -36,7 +35,7 @@ describe("About Functions", function() {
}
return childfunction();
}
expect(__).toBe(parentfunction());
expect(parentfunction()).toBe(__);
});

it("should use lexical scoping to synthesise functions", function () {
Expand All @@ -53,7 +52,7 @@ describe("About Functions", function() {
var increaseBy3 = makeIncreaseByFunction(3);
var increaseBy5 = makeIncreaseByFunction(5);

expect(__).toBe(increaseBy3(10) + increaseBy5(10));
expect(increaseBy3(10) + increaseBy5(10)).toBe(__);
});

it("should allow extra function arguments", function () {
Expand All @@ -63,14 +62,14 @@ describe("About Functions", function() {
return firstArg;
}

expect(__).toBe(returnFirstArg("first", "second", "third"));
expect(returnFirstArg("first", "second", "third")).toBe(__);

function returnSecondArg(firstArg, secondArg)
{
return secondArg;
}

expect(__).toBe(returnSecondArg("only give first arg"));
expect(returnSecondArg("only give first arg")).toBe(__);

function returnAllArgs()
{
Expand All @@ -81,7 +80,7 @@ describe("About Functions", function() {
return argsArray.join(",");
}

expect(__).toBe(returnAllArgs("first", "second", "third"));
expect(returnAllArgs("first", "second", "third")).toBe(__);
});

it("should pass functions as values", function () {
Expand All @@ -95,21 +94,21 @@ describe("About Functions", function() {
};

var praiseSinger = { givePraise: appendRules };
expect(__).toBe(praiseSinger.givePraise("John"));
expect(praiseSinger.givePraise("John")).toBe(__);

praiseSinger.givePraise = appendDoubleRules;
expect(__).toBe(praiseSinger.givePraise("Mary"));
expect(praiseSinger.givePraise("Mary")).toBe(__);

});

it("should use function body as a string", function () {
var add = new Function("a", "b", "return a + b;");
expect(__).toBe(add(1, 2));
expect(add(1, 2)).toBe(__);

var multiply = function (a, b) {
//An internal comment
return a * b;
};
expect(__).toBe(multiply.toString());
expect(multiply.toString()).toBe(__);
});
});
22 changes: 11 additions & 11 deletions koans/AboutHigherOrderFunctions.js
Expand Up @@ -7,25 +7,25 @@ describe("About Higher Order Functions", function () {
var numbers = [1,2,3];
var odd = df.filter(numbers, "x % 2 !== 0");

expect(__).toEqual(odd);
expect(__).toBe(odd.length);
expect(__).toBe(numbers.length);
expect(odd).toEqual(__);
expect(odd.length).toBe(__);
expect(numbers.length).toBe(__);
});

it("should use 'map' to transform each element", function () {
var numbers = [1, 2, 3];
var numbersPlus1 = df.map(numbers, "x + 1");

expect(__).toEqual(numbersPlus1);
expect(__).toEqual(numbers);
expect(numbersPlus1).toEqual(__);
expect(numbers).toEqual(__);
});

it("should use 'reduce' to update the same result on each iteration ", function () {
var numbers = [1, 2, 3];
var reduction = df.reduce(numbers, "result + x");

expect(__).toBe(reduction);
expect(__).toEqual(numbers);
expect(reduction).toBe(__);
expect(numbers).toEqual(__);
});

it("should use 'forEach' for simple iteration", function () {
Expand All @@ -37,8 +37,8 @@ describe("About Higher Order Functions", function () {

df.forEach(numbers, isEven);

expect(__).toEqual(msg);
expect(__).toEqual(numbers);
expect(msg).toEqual(__);
expect(numbers).toEqual(__);
});

it("should use 'some' to apply until true", function () {
Expand All @@ -50,7 +50,7 @@ describe("About Higher Order Functions", function () {
};

expect(numbers.some(isEven)).toBeTruthy();
expect(__).toEqual(msg);
expect(msg).toEqual(__);
});

it("should use 'every' to applies until first false" , function () {
Expand All @@ -62,7 +62,7 @@ describe("About Higher Order Functions", function () {
};

expect(numbers.every(isEven)).toBeFalsy();
expect(__).toBe(msg);
expect(msg).toBe(__);
});
});

0 comments on commit 3ee6b5e

Please sign in to comment.