From 5a300998a8a2b38ca942d8ffe83ebc2ff2af5204 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 2 Jul 2013 15:12:16 -0400 Subject: [PATCH] Tests: console log capture utility --- test/test.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/test.js b/test/test.js index ddded768..51d2a33b 100644 --- a/test/test.js +++ b/test/test.js @@ -71,5 +71,46 @@ }; + /* + Test console log capture + + 1. Only the OpenSeadragon.console logger is touched + 2. All log messages are stored in window.testLog in arrays keyed on the logger name (e.g. log, + warning, error, etc.) as JSON-serialized strings to simplify comparisons + 3. The captured log arrays have a custom contains() method for ease of testing + 4. testLog.reset() will clear all of the message arrays, intended for use in test setup routines + */ + var testConsole = window.testConsole = {}, + testLog = window.testLog = { + log: [], + debug: [], + info: [], + warn: [], + error: [], + reset: function () { + for (var i in testLog) { + if (testLog.hasOwnProperty(i) && 'length' in testLog[i]) { + testLog[i].length = 0; + } + } + } + }; + + for (var i in testLog) { + if (testLog.hasOwnProperty(i) && testLog[i].push) { + testConsole[i] = (function (arr) { + return function () { + var args = Array.prototype.slice.call(arguments, 0); // Coerce to true Array + arr.push(JSON.stringify(args)); // Store as JSON to avoid tedious array-equality tests + }; + })(testLog[i]); + + testLog[i].contains = function (needle) { + return this.indexOf(needle) > -1; + }; + } + } + + OpenSeadragon.console = testConsole; })();