Skip to content

Commit

Permalink
Added better global log/error/warn functions.
Browse files Browse the repository at this point in the history
Added sinon.js for stubs in tests.
Updated grunt version to satisfy peer dependency warning.
  • Loading branch information
heff committed May 7, 2014
1 parent bcb69a0 commit 740014c
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"start",
"stop",
"strictEqual",
"test"
"test",
"sinon"
]
}
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = function(grunt) {
},
tests: {
src: ['build/files/combined.video.js', 'build/compiler/goog.base.js', 'src/js/exports.js', 'test/unit/*.js'],
externs: ['src/js/player.externs.js', 'src/js/media/flash.externs.js', 'test/qunit-externs.js'],
externs: ['src/js/player.externs.js', 'src/js/media/flash.externs.js', 'test/qunit-externs.js', 'test/sinon-externs.js'],
dest: 'build/files/test.minified.video.js'
}
},
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"devDependencies": {
"grunt-cli": "~0.1.0",
"grunt": "~0.4",
"grunt": "0.4.2",
"grunt-contrib-connect": "~0.7.1",
"grunt-contrib-jshint": "~0.4.3",
"grunt-contrib-watch": "~0.1.4",
Expand Down Expand Up @@ -57,6 +57,7 @@
"grunt-tagrelease": "~0.3.3",
"github": "~0.1.14",
"open": "0.0.4",
"grunt-version": "~0.3.0"
"grunt-version": "~0.3.0",
"sinon": "~1.9.1"
}
}
74 changes: 67 additions & 7 deletions src/js/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,14 +656,74 @@ vjs.getAbsoluteURL = function(url){
return url;
};

// usage: log('inside coolFunc',this,arguments);
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
vjs.log = function(){
vjs.log.history = vjs.log.history || []; // store logs to an array for reference
vjs.log.history.push(arguments);
if(window.console){
window.console.log(Array.prototype.slice.call(arguments));
// if there's no console then don't try to output messages
// they will still be stored in vjs.log.history
var _noop = function(){};
var _console = window['console'] || {
'log': _noop,
'warn': _noop,
'error': _noop
};

/**
* Log messags to the console and history based on the type of message
*
* @param {String} type The type of message, or `null` for `log`
* @param {[type]} args The args to be passed to the log
* @private
*/
function _logType(type, args){
// convert args to an array to get array functions
var argsArray = Array.prototype.slice.call(args);

if (type) {
// add the type to the front of the message
argsArray.unshift(type.toUpperCase()+':');
} else {
// default to log with no prefix
type = 'log';
}

// add to history
vjs.log.history.push(argsArray);

// add console prefix after adding to history
argsArray.unshift('VIDEOJS:');

// call appropriate log function
if (_console[type].apply) {
_console[type].apply(_console, argsArray);
} else {
// ie8 doesn't allow error.apply, but it will just join() the array anyway
_console[type](argsArray.join(' '));
}
}

/**
* Log plain debug messages
*/
vjs.log = function(){
_logType(null, arguments);
};

/**
* Keep a history of log messages
* @type {Array}
*/
vjs.log.history = [];

/**
* Log error messages
*/
vjs.log.error = function(){
_logType('error', arguments);
};

/**
* Log warning messages
*/
vjs.log.warn = function(){
_logType('warn', arguments);
};

// Offset Left
Expand Down
6 changes: 3 additions & 3 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<head>
<title>Video.js Test Suite</title>

<!--[if IE]>
<script src="https://getfirebug.com/releases/lite/1.4/firebug-lite.js"></script>
<!--<![endif]-->
<!-- Sinon -->
<script src="../node_modules/sinon/pkg/sinon.js"></script>
<script src="../node_modules/sinon/pkg/sinon-ie.js"></script>

<!-- QUnit -->
<link rel="stylesheet" href="../node_modules/qunitjs/qunit/qunit.css" />
Expand Down
4 changes: 4 additions & 0 deletions test/minified-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<head>
<title>Video.js Test Suite</title>

<!-- Sinon -->
<script src="../node_modules/sinon/pkg/sinon.js"></script>
<script src="../node_modules/sinon/pkg/sinon-ie.js"></script>

<!-- QUnit -->
<link rel="stylesheet" href="../node_modules/qunitjs/qunit/qunit.css" />
<script src="../node_modules/qunitjs/qunit/qunit.js"></script>
Expand Down
4 changes: 4 additions & 0 deletions test/minified.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<head>
<title>Video.js Test Suite</title>

<!-- Sinon -->
<script src="../node_modules/sinon/pkg/sinon.js"></script>
<script src="../node_modules/sinon/pkg/sinon-ie.js"></script>

<!-- QUnit -->
<link rel="stylesheet" href="../node_modules/qunitjs/qunit/qunit.css" />
<script src="../node_modules/qunitjs/qunit/qunit.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion test/qunit-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ function start(increment){}
/**
* @param {number=} increment
*/
function stop(increment){}
function stop(increment){}
99 changes: 99 additions & 0 deletions test/sinon-externs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Sinon externs
*/
function sinon(){}

sinon.stub = function(){};
sinon.spy = function(){};
sinon.mock = function(){};

Function.prototype.alwaysCalledOn = function(){};
Function.prototype.alwaysCalledWith = function(){};
Function.prototype.alwaysCalledWithExactly = function(){};
Function.prototype.alwaysCalledWithMatch = function(){};
Function.prototype.alwaysCalledWithNew = function(){};
Function.prototype.alwaysReturned = function(){};
Function.prototype.alwaysThrew = function(){};
Function.prototype.args;
Function.prototype.arguments;
Function.prototype.behaviors;
Function.prototype.callArg = function(){};
Function.prototype.callArgOn = function(){};
Function.prototype.callArgOnWith = function(){};
Function.prototype.callArgWith = function(){};
Function.prototype.callCount;
Function.prototype.callIds;
Function.prototype.called;
Function.prototype.calledAfter = function(){};
Function.prototype.calledBefore = function(){};
Function.prototype.calledOn = function(){};
Function.prototype.calledOnce;
Function.prototype.calledThrice;
Function.prototype.calledTwice;
Function.prototype.calledWith = function(){};
Function.prototype.calledWithExactly = function(){};
Function.prototype.calledWithMatch = function(){};
Function.prototype.calledWithNew = function(){};
Function.prototype.caller;
Function.prototype.callsArg = function(){};
Function.prototype.callsArgAsync = function(){};
Function.prototype.callsArgOn = function(){};
Function.prototype.callsArgOnAsync = function(){};
Function.prototype.callsArgOnWith = function(){};
Function.prototype.callsArgOnWithAsync = function(){};
Function.prototype.callsArgWith = function(){};
Function.prototype.callsArgWithAsync = function(){};
Function.prototype.create = function(){};
Function.prototype.defaultBehavior;
Function.prototype.displayName;
Function.prototype.exceptions;
Function.prototype.firstCall;
Function.prototype.formatters;
Function.prototype.func;
Function.prototype.getCall = function(){};
Function.prototype.getCalls = function(){};
Function.prototype.id;
Function.prototype.invoke = function(){};
Function.prototype.invokeCallback = function(){};
Function.prototype.isPresent = function(){};
Function.prototype.lastCall;
Function.prototype.length;
Function.prototype.matches = function(){};
Function.prototype.name;
Function.prototype.neverCalledWith = function(){};
Function.prototype.neverCalledWithMatch = function(){};
Function.prototype.notCalled;
Function.prototype.onCall = function(){};
Function.prototype.onFirstCall = function(){};
Function.prototype.onSecondCall = function(){};
Function.prototype.onThirdCall = function(){};
Function.prototype.printf = function(){};
Function.prototype.reset = function(){};
Function.prototype.resetBehavior = function(){};
Function.prototype.restore = function(){};
Function.prototype.returnValues;
Function.prototype.returned = function(){};
Function.prototype.returns = function(){};
Function.prototype.returnsArg = function(){};
Function.prototype.returnsThis = function(){};
Function.prototype.secondCall;
Function.prototype.spyCall;
Function.prototype.thirdCall;
Function.prototype.thisValues;
Function.prototype.threw = function(){};
Function.prototype['throws'] = function(){};
Function.prototype.throwsException = function(){};
Function.prototype.toString = function(){};
Function.prototype.withArgs = function(){};
Function.prototype.yield = function(){};
Function.prototype.yieldOn = function(){};
Function.prototype.yieldTo = function(){};
Function.prototype.yieldToOn = function(){};
Function.prototype.yields = function(){};
Function.prototype.yieldsAsync = function(){};
Function.prototype.yieldsOn = function(){};
Function.prototype.yieldsOnAsync = function(){};
Function.prototype.yieldsTo = function(){};
Function.prototype.yieldsToAsync = function(){};
Function.prototype.yieldsToOn = function(){};
Function.prototype.yieldsToOnAsync = function(){};
54 changes: 54 additions & 0 deletions test/unit/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,57 @@ test('vjs.findPosition should find top and left position', function() {
position = vjs.findPosition(d);
deepEqual(position, {left: 0, top: 0}, 'If there is no gBCR, we should get zeros');
});

// LOG TESTS
test('should confirm logging functions work', function() {
var console = window['console'];
var origLog = console.log;
var origWarn = console.warn;
var origError = console.error;

// in ie8 console.log is apparently not a 'function' so sinon chokes on it
// https://github.com/cjohansen/Sinon.JS/issues/386
// instead we'll temporarily replace them with functions
if (typeof origLog === 'object') {
console.log = function(){};
console.warn = function(){};
console.error = function(){};
}

// stub the global log functions
var log = sinon.stub(console, 'log');
var error = sinon.stub(console, 'error');
var warn = sinon.stub(console, 'warn');

vjs.log('asdf', 'fdsa');
ok(log.called, 'log was called');
equal(log.firstCall.args[0], 'VIDEOJS:');
equal(log.firstCall.args[1], 'asdf');
equal(log.firstCall.args[2], 'fdsa');

vjs.log.warn('asdf', 'fdsa');
ok(warn.called, 'warn was called');
equal(warn.firstCall.args[0], 'VIDEOJS:');
equal(warn.firstCall.args[1], 'WARN:');
equal(warn.firstCall.args[2], 'asdf');
equal(warn.firstCall.args[3], 'fdsa');

vjs.log.error('asdf', 'fdsa');
ok(error.called, 'error was called');
equal(error.firstCall.args[0], 'VIDEOJS:');
equal(error.firstCall.args[1], 'ERROR:');
equal(error.firstCall.args[2], 'asdf');
equal(error.firstCall.args[3], 'fdsa');

// tear down sinon
log.restore();
error.restore();
warn.restore();

// restore ie8
if (typeof origLog === 'object') {
console.log = origLog;
console.warn = origWarn;
console.error = origError;
}
});

0 comments on commit 740014c

Please sign in to comment.