Permalink
Browse files

Remove duplication between sorting/display.js and

dataStructures/display.js (and the tests)
  • Loading branch information...
1 parent 37e1c2e commit df3b6e3af11cc0f8a82223597ef901f0c55b9be8 @fhd committed Apr 21, 2012
View
@@ -4,5 +4,3 @@
- Linked List (variant with sentinels instead of the current one)
- Hash Map
- Binary Tree
-- Remove duplication between sorting/display.js and
- dataStructures/display.js (and the tests)
@@ -1,11 +1,7 @@
var dataStructures = {};
-(function(dataStructures) {
+(function(dataStructures, display) {
function draw(context, width, height) {
- context.clearRect(0, 0, width, height);
- context.fillStyle = "rgb(0, 0, 0)";
- context.font = "12px sans";
- context.textBaseline = "top";
$.each(dataStructures.boxes, function(_, box) {
var padding = 5,
textPadding = 5;
@@ -170,17 +166,14 @@ var dataStructures = {};
dataStructures.boxes = [];
dataStructures.init = function(ds) {
- if (typeof currentAlgorithmFile !== "undefined")
- prettyPrint(); // Prettify
-
if (ds instanceof Stack)
createStackOperations(ds);
else if (ds instanceof Queue)
createQueueOperations(ds);
else if (ds instanceof LinkedList)
createLinkedListOperations(ds);
- setInterval(utils.createDrawFunction($("#canvas")[0], draw), 10);
+ display.init(draw);
};
dataStructures.addBox = function(index, value, callback) {
@@ -224,4 +217,4 @@ var dataStructures = {};
else
callback();
}
-})(dataStructures);
+})(dataStructures, display);
View
@@ -0,0 +1,28 @@
+var display = {};
+
+(function(display) {
+ var drawContent;
+
+ function createDrawFunction(canvas, drawFunction) {
+ return function() {
+ if (!canvas)
+ return;
+ drawFunction(canvas.getContext("2d"), canvas.width, canvas.height);
+ };
+ }
+
+ function draw(context, width, height) {
+ context.clearRect(0, 0, width, height);
+ context.fillStyle = "rgb(0, 0, 0)";
+ context.font = "12px sans";
+ context.textBaseline = "top";
+ drawContent.apply(undefined, Array.prototype.slice.call(arguments));
+ };
+
+ display.init = function(drawFunction) {
+ drawContent = drawFunction;
+ if (typeof currentAlgorithmFile !== "undefined")
+ prettyPrint();
+ setInterval(createDrawFunction($("#canvas")[0], draw), 10);
+ };
+})(display);
@@ -1,13 +1,9 @@
var sorting = {};
-(function(sorting) {
+(function(sorting, display) {
var array = [], i;
function draw(context, width, height) {
- context.clearRect(0, 0, width, height);
- context.fillStyle = "rgb(0, 0, 0)";
- context.font = "12px sans";
- context.textBaseline = "top";
$.each(array, function(index, value) {
var barWidth = 5;
context.fillRect(index * barWidth * 2 + barWidth,
@@ -29,32 +25,31 @@ var sorting = {};
};
sorting.init = function(stack) {
- if (typeof currentAlgorithmFile !== "undefined") {
- prettyPrint(); // Prettify
+ if (typeof currentAlgorithmFile === "undefined")
+ return;
+ array.shuffle();
+ $("#shuffle").click(function() {
array.shuffle();
- $("#shuffle").click(function() {
- array.shuffle();
- });
+ });
- $("#sort").click(function() {
- var controls = $("#algorithms, #shuffle, #sort"),
- worker = new Worker("js/sorting/worker.js");
- controls.attr("disabled", "true");
- worker.onmessage = function(event) {
- var data = event.data;
- array = data.array;
- if (data.finished)
- controls.removeAttr("disabled");
- };
- worker.postMessage({
- file: currentAlgorithmFile,
- functionName: currentAlgorithmFunction,
- "array": array
- });
+ $("#sort").click(function() {
+ var controls = $("#algorithms, #shuffle, #sort"),
+ worker = new Worker("js/sorting/worker.js");
+ controls.attr("disabled", "true");
+ worker.onmessage = function(event) {
+ var data = event.data;
+ array = data.array;
+ if (data.finished)
+ controls.removeAttr("disabled");
+ };
+ worker.postMessage({
+ file: currentAlgorithmFile,
+ functionName: currentAlgorithmFunction,
+ "array": array
});
+ });
- setInterval(utils.createDrawFunction($("#canvas")[0], draw), 10);
- }
+ display.init(draw);
};
-})(sorting);
+})(sorting, display);
View
@@ -1,9 +0,0 @@
-var utils = {
- createDrawFunction: function(canvas, drawFunction) {
- return function() {
- if (!canvas)
- return;
- drawFunction(canvas.getContext("2d"), canvas.width, canvas.height);
- };
- }
-};
View
@@ -4,7 +4,7 @@
<title>QUnit Tests</title>
<link rel="stylesheet" href="qunit/qunit.css" media="screen"/>
<script src="../js/libs/jquery-1.5.1.min.js"></script>
- <script src="../js/utils.js"></script>
+ <script src="../js/display.js"></script>
<script src="../js/sorting/display.js"></script>
<script src="../js/sorting/worker.js"></script>
<script src="../js/sorting/insertionSort.js"></script>
@@ -20,7 +20,7 @@
<script src="qunit/qunit.js"></script>
<script src="sinon-1.3.2.js"></script>
<script src="testUtils.js"></script>
- <script src="tests/utils.js"></script>
+ <script src="tests/display.js"></script>
<script src="tests/sorting/display.js"></script>
<script src="tests/sorting/worker.js"></script>
<script src="tests/sorting/sort.js"></script>
View
@@ -12,10 +12,4 @@ var testUtils = {};
__id: uniqueId()
};
};
-
- testUtils.uniqueFunction = function() {
- return function() {
- var id = uniqueId();
- };
- }
})(testUtils);
@@ -1,28 +1,18 @@
module("dataStructures/display", {
setup: function() {
$("body").append($("<canvas/>").attr("id", "canvas"));
+ sinon.stub(display, "init");
},
teardown: function() {
$("#canvas").remove();
+ display.init.restore();
}
});
-test("init", 2, function() {
- var f = testUtils.uniqueFunction();
-
+test("init", 1, function() {
currentAlgorithmFile = "";
- prettyPrint = sinon.spy();
- sinon.stub(utils, "createDrawFunction");
- utils.createDrawFunction.returns(f);
- sinon.stub(window, "setInterval");
-
dataStructures.init();
-
- ok(prettyPrint.called, "Google Prettify should be activated.");
- ok(setInterval.calledWith(f), "The draw function interval should be set.");
-
- utils.createDrawFunction.restore();
- setInterval.restore();
+ ok(display.init.called, "The display should be initialised.");
});
test("addBox", 2, function() {
@@ -0,0 +1,23 @@
+module("display");
+
+test("init", 3, function() {
+ var draw;
+
+ currentAlgorithmFile = "";
+ $("body").append($("<canvas/>").attr("id", "canvas"));
+
+ draw = sinon.spy();
+ prettyPrint = sinon.spy();
+
+ sinon.stub(window, "setInterval");
+
+ display.init(draw);
+
+ ok(prettyPrint.called, "Google Prettify should be activated.");
+ ok(setInterval.called, "The draw interval should be set up.");
+ setInterval.args[0][0]();
+ ok(draw.called, "The draw function should invoked.");
+
+ setInterval.restore();
+ $("#canvas").remove();
+});
@@ -7,20 +7,10 @@ test("Array.shuffle", 1, function() {
notDeepEqual(array, shuffledArray);
});
-test("init", 2, function() {
- var f = testUtils.uniqueFunction();
-
+test("init", 1, function() {
currentAlgorithmFile = "";
- prettyPrint = sinon.spy();
- sinon.stub(utils, "createDrawFunction");
- utils.createDrawFunction.returns(f);
- sinon.stub(window, "setInterval");
-
+ sinon.stub(display, "init");
sorting.init();
-
- ok(prettyPrint.called, "Google Prettify should be activated.");
- ok(setInterval.calledWith(f), "The draw function interval should be set.");
-
- utils.createDrawFunction.restore();
- setInterval.restore();
+ ok(display.init.called, "The display should be initialised.");
+ display.init.restore();
});
View
@@ -1,14 +0,0 @@
-module("utils");
-
-test("createDrawFunction", 1, function() {
- var context = testUtils.uniqueObject(),
- canvas = {
- getContext: sinon.stub().returns(context)
- },
- f = sinon.spy();
-
- utils.createDrawFunction(canvas, f)();
-
- ok(f.calledWith(context),
- "The draw function should use the context of the supplied canvas.");
-});
View
@@ -8,7 +8,7 @@ html
script(src="js/libs/jquery-1.5.1.min.js")
script(src="js/libs/jquery-ui-1.8.11.custom.min.js")
script(src="js/libs/prettify/prettify.js")
- script(src="js/utils.js")
+ script(src="js/display.js")
body
header

0 comments on commit df3b6e3

Please sign in to comment.