Skip to content

Commit

Permalink
Merge pull request marmelab#64 from marmelab/null_default
Browse files Browse the repository at this point in the history
[RFR] Refactor service injection to mogwais and gremlins
  • Loading branch information
fzaninotto committed May 8, 2014
2 parents 06d1d0f + c39d4eb commit 972fdb2
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 75 deletions.
12 changes: 12 additions & 0 deletions src/exceptions/loggerRequired.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
define(function(require) {
"use strict";

function LoggerRequiredException() {
this.message = 'This mogwai requires a logger to run. Please call logger(loggerObject) before executing the mogwai';
this.toString = function() {
return this.message;
};
}

return LoggerRequiredException;
});
12 changes: 12 additions & 0 deletions src/exceptions/randomizerRequired.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
define(function(require) {
"use strict";

function RandomizerRequiredException() {
this.message = 'This gremlin requires a randomizer to run. Please call randomizer(randomizerObject) before executing the gremlin';
this.toString = function() {
return this.message;
};
}

return RandomizerRequiredException;
});
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ define(function(require) {
function inject(services, objects) {
for (var i = 0, count = objects.length; i < count; i++) {
for (var name in services) {
if (typeof objects[i][name] === "function") {
if (typeof objects[i][name] === "function" && !objects[i][name]()) {
objects[i][name](services[name]);
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/mogwais/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@ define(function(require) {

var configurable = require('../utils/configurable');
var Chance = require('../vendor/chance');
var LoggerRequiredException = require('../exceptions/loggerRequired');

return function() {

var defaultWatchEvents = ['alert', 'confirm', 'prompt'];

var defaultConfirmResponse = function() {
function defaultConfirmResponse() {
// Random OK or cancel
return config.randomizer.bool();
};
}

var defaultPromptResponse = function() {
function defaultPromptResponse() {
// Return a random string
return config.randomizer.sentence();
};

var defaultLogger = { warn: function() {} };
}

/**
* @mixin
Expand All @@ -53,8 +52,8 @@ define(function(require) {
watchEvents: defaultWatchEvents,
confirmResponse: defaultConfirmResponse,
promptResponse: defaultPromptResponse,
logger: defaultLogger,
randomizer: new Chance()
logger: null,
randomizer: null
};

var alert = window.alert;
Expand All @@ -65,6 +64,11 @@ define(function(require) {
* @mixes config
*/
function alertMogwai() {

if (!config.logger) {
throw new LoggerRequiredException();
}

if (config.watchEvents.indexOf('alert') !== -1) {
window.alert = function (msg) {
config.logger.warn('mogwai ', 'alert ', msg, 'alert');
Expand Down
16 changes: 7 additions & 9 deletions src/mogwais/fps.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,23 @@ define(function(require) {
"use strict";

var configurable = require('../utils/configurable');
var LoggerRequiredException = require('../exceptions/loggerRequired');

return function() {

var defaultLogger = {
log: function() {},
warn: function() {},
error: function() {}
};

var defaultLevelSelector = function(fps) {
function defaultLevelSelector(fps) {
if (fps < 10) return 'error';
if (fps < 20) return 'warn';
return 'log';
};
}

/**
* @mixin
*/
var config = {
delay: 500, // how often should the fps be measured
levelSelector: defaultLevelSelector,
logger: defaultLogger
logger: null
};

var initialTime = -Infinity; // force initial measure
Expand Down Expand Up @@ -84,6 +79,9 @@ define(function(require) {
* @mixes config
*/
function fpsMogwai() {
if (!config.logger) {
throw new LoggerRequiredException();
}
enabled = true;
window.requestAnimationFrame(loop);
}
Expand Down
17 changes: 8 additions & 9 deletions src/mogwais/gizmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ define(function(require) {

return function() {

var defaultLogger = { warn: function() {} };

/**
* @mixin
*/
var config = {
maxErrors: 10,
logger: defaultLogger
logger: null
};

var realOnError, realLoggerError;
Expand All @@ -44,10 +42,11 @@ define(function(require) {
function gizmoMogwai() {
var nbErrors = 0;
var horde = this; // this is exceptional - don't use 'this' for mogwais in general
function incrementNbErrors(){
function incrementNbErrors() {
nbErrors++;
if (nbErrors == config.maxErrors) {
horde.stop();
if (!config.logger) return;
window.setTimeout(function() {
// display the mogwai error after the caught error
config.logger.warn('mogwai ', 'gizmo ', 'stopped test execution after ', config.maxErrors, 'errors');
Expand All @@ -62,17 +61,17 @@ define(function(require) {
return realOnError ? realOnError(message, url, linenumber) : false;
};

// console (or logger) errors
realLoggerError = config.logger.error;
config.logger.error = function() {
// console errors
realLoggerError = console.error;
console.error = function() {
incrementNbErrors();
realLoggerError.apply(config.logger, arguments);
realLoggerError.apply(console, arguments);
};
}

gizmoMogwai.cleanUp = function() {
window.onerror = realOnError;
config.logger.error = realLoggerError.bind(config.logger);
console.error = realLoggerError.bind(console);
return gizmoMogwai;
};

Expand Down
22 changes: 14 additions & 8 deletions src/species/clicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ define(function(require) {

var configurable = require('../utils/configurable');
var Chance = require('../vendor/chance');
var RandomizerRequiredException = require('../exceptions/randomizerRequired');

return function() {

Expand All @@ -57,14 +58,14 @@ define(function(require) {

var defaultClickTypes = ['click', 'click', 'click', 'click', 'click', 'click', 'dblclick', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseover', 'mouseover', 'mousemove', 'mouseout'];

var defaultPositionSelector = function() {
function defaultPositionSelector() {
return [
config.randomizer.natural({ max: document.documentElement.clientWidth - 1 }),
config.randomizer.natural({ max: document.documentElement.clientHeight - 1 })
];
};
}

var defaultShowAction = function(x, y) {
function defaultShowAction(x, y) {
var clickSignal = document.createElement('div');
clickSignal.style.border = "3px solid red";
clickSignal.style['border-radius'] = '50%'; // Chrome
Expand All @@ -85,9 +86,11 @@ define(function(require) {
setTimeout(function() {
element.style.opacity = 0;
}, 50);
};
}

var defaultCanClick = function() { return true; };
function defaultCanClick() {
return true;
}

/**
* @mixin
Expand All @@ -98,14 +101,17 @@ define(function(require) {
showAction: defaultShowAction,
canClick: defaultCanClick,
maxNbTries: 10,
logger: {},
randomizer: new Chance()
logger: null,
randomizer: null
};

/**
* @mixes config
*/
function clickerGremlin() {
if (!config.randomizer) {
throw new RandomizerRequiredException();
}
var position, posX, posY, targetElement, nbTries = 0;
do {
position = config.positionSelector();
Expand All @@ -125,7 +131,7 @@ define(function(require) {
config.showAction(posX, posY, clickType);
}

if (typeof config.logger.log == 'function') {
if (config.logger && typeof config.logger.log == 'function') {
config.logger.log('gremlin', 'clicker ', clickType, 'at', posX, posY);
}
}
Expand Down
21 changes: 14 additions & 7 deletions src/species/formFiller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ define(function(require) {

var configurable = require('../utils/configurable');
var Chance = require('../vendor/chance');
var RandomizerRequiredException = require('../exceptions/randomizerRequired');

return function() {

Expand All @@ -41,7 +42,7 @@ define(function(require) {
'input:not([type])': fillTextElement
};

var defaultShowAction = function(element) {
function defaultShowAction(element) {
if(typeof element.attributes['data-old-border'] === 'undefined') {
element.attributes['data-old-border'] = element.style.border;
}
Expand All @@ -52,9 +53,11 @@ define(function(require) {
setTimeout(function() {
element.style.border = oldBorder;
}, 500);
};
}

var defaultCanFillElement = function() { return true; };
function defaultCanFillElement() {
return true;
}

/**
* @mixin
Expand All @@ -64,18 +67,22 @@ define(function(require) {
showAction: defaultShowAction,
canFillElement: defaultCanFillElement,
maxNbTries: 10,
logger: {},
randomizer: new Chance()
logger: null,
randomizer: null
};

/**
* @mixes config
*/
function formFillerGremlin() {
if (!config.randomizer) {
throw new RandomizerRequiredException();
}

// Retrieve all selectors
var elementTypes = [];

for(var key in config.elementMapTypes) {
for (var key in config.elementMapTypes) {
if(config.elementMapTypes.hasOwnProperty(key)) {
elementTypes.push(key);
}
Expand Down Expand Up @@ -107,7 +114,7 @@ define(function(require) {
config.showAction(element);
}

if (typeof config.logger.log == 'function') {
if (config.logger && typeof config.logger.log == 'function') {
config.logger.log('gremlin', 'formFiller', 'input', value, 'in', element);
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/species/scroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,25 @@ define(function(require) {

var configurable = require('../utils/configurable');
var Chance = require('../vendor/chance');
var RandomizerRequiredException = require('../exceptions/randomizerRequired');

return function() {

var document = window.document,
documentElement = document.documentElement,
body = document.body;

var defaultPositionSelector = function() {
function defaultPositionSelector() {
var documentWidth = Math.max(body.scrollWidth, body.offsetWidth, documentElement.scrollWidth, documentElement.offsetWidth, documentElement.clientWidth),
documentHeight = Math.max(body.scrollHeight, body.offsetHeight, documentElement.scrollHeight, documentElement.offsetHeight, documentElement.clientHeight);

return [
config.randomizer.natural({ max: documentWidth - documentElement.clientWidth }),
config.randomizer.natural({ max: documentHeight - documentElement.clientHeight })
];
};
}

var defaultShowAction = function(scrollX, scrollY) {
function defaultShowAction(scrollX, scrollY) {
var clickSignal = document.createElement('div');
clickSignal.style.border = "3px solid red";
clickSignal.style.width = (documentElement.clientWidth - 25) + "px";
Expand All @@ -65,22 +66,26 @@ define(function(require) {
setTimeout(function() {
element.style.opacity = 0;
}, 50);
};
}

/**
* @mixin
*/
var config = {
positionSelector: defaultPositionSelector,
showAction: defaultShowAction,
logger: {},
randomizer: new Chance()
logger: null,
randomizer: null
};

/**
* @mixes config
*/
function scrollerGremlin() {
if (!config.randomizer) {
throw new RandomizerRequiredException();
}

var position = config.positionSelector(),
scrollX = position[0],
scrollY = position[1];
Expand Down
Loading

0 comments on commit 972fdb2

Please sign in to comment.