Permalink
Browse files

remotegcli-839862: Test fixes and tidy-ups

* Add environment to conversionContext - it is reasonable if we expose
  user supplied data at both execution time and conversion time.
* Fix minor doc error with labelling DOM nodes as HTML
* Sync with mozilla-central changes to helpers.js
* Use an alias for exports here because this module is used in Firefox
  mochitests where we don't have define/require
* Clean up types test properly to prevent leaks in Firefox

Signed-off-by: Joe Walker <jwalker@mozilla.com>
  • Loading branch information...
1 parent 3c62382 commit 17f8195cdf33d54582a885a2e6c8513b3f468630 @joewalker committed May 8, 2013
Showing with 94 additions and 71 deletions.
  1. +1 −1 docs/writing-commands.md
  2. +7 −3 lib/gcli/cli.js
  3. +27 −18 lib/gclitest/helpers.js
  4. +52 −49 lib/gclitest/mockCommands.js
  5. +7 −0 lib/gclitest/testTypes.js
@@ -645,7 +645,7 @@ and available as input to other commands as a plain string.
GCLI will interpret this as HTML, and parse it for display.
- { returnType: "html" }
+ { returnType: "dom" }
...
return util.createElement(context.document, 'div');
View
@@ -461,7 +461,7 @@ Object.defineProperty(Requisition.prototype, 'executionContext', {
});
Object.defineProperty(this._executionContext, 'environment', {
get: function() { return requisition.environment; },
- enumerable : true
+ enumerable: true
});
/**
@@ -481,7 +481,7 @@ Object.defineProperty(Requisition.prototype, 'executionContext', {
Object.defineProperty(this._executionContext, 'document', {
get: function() { return requisition.document; },
- enumerable : true
+ enumerable: true
});
}
}
@@ -513,7 +513,11 @@ Object.defineProperty(Requisition.prototype, 'conversionContext', {
Object.defineProperty(this._conversionContext, 'document', {
get: function() { return requisition.document; },
- enumerable : true
+ enumerable: true
+ });
+ Object.defineProperty(this._conversionContext, 'environment', {
+ get: function() { return requisition.environment; },
+ enumerable: true
});
/**
@@ -157,6 +157,7 @@ helpers.runTests = function(options, tests) {
});
var recover = function(error) {
+ ok(false, error);
console.error(error);
};
@@ -703,7 +704,7 @@ helpers._check = function(options, name, checks) {
*/
helpers._exec = function(options, name, expected) {
if (expected == null) {
- return Promise.resolve();
+ return Promise.resolve({});
}
var output = options.display.requisition.exec({ hidden: true });
@@ -716,11 +717,11 @@ helpers._exec = function(options, name, expected) {
if (!options.window.document.createElement) {
assert.log('skipping output tests (missing doc.createElement) for ' + name);
- return Promise.resolve();
+ return Promise.resolve({ output: output });
}
if (!('output' in expected)) {
- return Promise.resolve();
+ return Promise.resolve({ output: output });
}
var checkOutput = function() {
@@ -740,16 +741,19 @@ helpers._exec = function(options, name, expected) {
}
var convertPromise = converters.convert(output.data, output.type, 'dom',
- conversionContext);
+ conversionContext);
return convertPromise.then(function(node) {
div.appendChild(node);
var actualOutput = div.textContent.trim();
var doTest = function(match, against) {
- if (!match.test(against)) {
- assert.ok(false, 'html output for ' + name + ' against ' + match.source);
- log('Actual textContent');
- log(against);
+ if (match.test(against)) {
+ assert.ok(true, 'html output for ' + name + ' should match ' +
+ match.source);
+ } else {
+ assert.ok(false, 'html output for ' + name + ' should match ' +
+ match.source +
+ '. Actual textContent: "' + against + '"');
}
};
@@ -766,6 +770,8 @@ helpers._exec = function(options, name, expected) {
else {
doTest(expected.output, actualOutput);
}
+
+ return { output: output, text: actualOutput };
});
};
@@ -790,9 +796,9 @@ helpers._setup = function(options, name, action) {
/**
* Helper to shutdown the test
*/
-helpers._post = function(name, action) {
+helpers._post = function(name, action, data) {
if (typeof action === 'function') {
- return Promise.resolve(action());
+ return Promise.resolve(action(data.output, data.text));
}
return Promise.resolve(action);
};
@@ -938,19 +944,22 @@ helpers.audit = function(options, audits) {
if (typeof chunkLen !== 'number') {
chunkLen = 1;
}
- var responseTime = (new Date().getTime() - start) / chunkLen;
- totalResponseTime += responseTime;
- if (responseTime > maxResponseTime) {
- maxResponseTime = responseTime;
- maxResponseCulprit = assert.currentTest + '/' + name;
+
+ if (assert.currentTest) {
+ var responseTime = (new Date().getTime() - start) / chunkLen;
+ totalResponseTime += responseTime;
+ if (responseTime > maxResponseTime) {
+ maxResponseTime = responseTime;
+ maxResponseCulprit = assert.currentTest + '/' + name;
+ }
+ averageOver++;
}
- averageOver++;
var checkDone = helpers._check(options, name, audit.check);
return checkDone.then(function() {
var execDone = helpers._exec(options, name, audit.exec);
- return execDone.then(function() {
- return helpers._post(name, audit.post).then(function() {
+ return execDone.then(function(data) {
+ return helpers._post(name, audit.post, data).then(function() {
if (assert.testLogging) {
log('- END \'' + name + '\' in ' + assert.currentTest);
}
@@ -15,28 +15,32 @@
*/
define(function(require, exports, module) {
+var mockCommands = exports;
+
+// We use an alias for exports here because this module is used in Firefox
+// mochitests where we don't have define/require
'use strict';
var util = require('util/util');
var canon = require('gcli/canon');
var types = require('gcli/types');
-exports.option1 = { };
-exports.option2 = { };
-exports.option3 = { };
+mockCommands.option1 = { };
+mockCommands.option2 = { };
+mockCommands.option3 = { };
-exports.optionType = {
+mockCommands.optionType = {
name: 'optionType',
parent: 'selection',
lookup: [
- { name: 'option1', value: exports.option1 },
- { name: 'option2', value: exports.option2 },
- { name: 'option3', value: exports.option3 }
+ { name: 'option1', value: mockCommands.option1 },
+ { name: 'option2', value: mockCommands.option2 },
+ { name: 'option3', value: mockCommands.option3 }
]
};
-exports.optionValue = {
+mockCommands.optionValue = {
name: 'optionValue',
parent: 'delegate',
delegateType: function(executionContext) {
@@ -50,16 +54,15 @@ exports.optionValue = {
}
};
-exports.onCommandExec = util.createEvent('commands.onCommandExec');
+mockCommands.onCommandExec = util.createEvent('commands.onCommandExec');
function createExec(name) {
return function(args, executionContext) {
var data = {
- command: exports[name],
args: args,
context: executionContext
};
- exports.onCommandExec(data);
+ mockCommands.onCommandExec(data);
var argsOut = Object.keys(args).map(function(key) {
return key + '=' + args[key];
}).join(', ');
@@ -436,18 +439,18 @@ var tsfail = {
}
};
-exports.commands = {};
+mockCommands.commands = {};
/**
* Registration and de-registration.
*/
-exports.setup = function(opts) {
+mockCommands.setup = function(opts) {
// setup/shutdown needs to register/unregister types, however that means we
// need to re-initialize mockCommands.option1 and mockCommands.option2 with
// the actual types
- exports.option1.type = types.createType('string');
- exports.option2.type = types.createType('number');
- exports.option3.type = types.createType({
+ mockCommands.option1.type = types.createType('string');
+ mockCommands.option2.type = types.createType('number');
+ mockCommands.option3.type = types.createType({
name: 'selection',
lookup: [
{ name: 'one', value: 1 },
@@ -456,38 +459,38 @@ exports.setup = function(opts) {
]
});
- types.addType(exports.optionType);
- types.addType(exports.optionValue);
-
- exports.commands.tsv = canon.addCommand(tsv);
- exports.commands.tsr = canon.addCommand(tsr);
- exports.commands.tso = canon.addCommand(tso);
- exports.commands.tse = canon.addCommand(tse);
- exports.commands.tsj = canon.addCommand(tsj);
- exports.commands.tsb = canon.addCommand(tsb);
- exports.commands.tss = canon.addCommand(tss);
- exports.commands.tsu = canon.addCommand(tsu);
- exports.commands.tsf = canon.addCommand(tsf);
- exports.commands.tsn = canon.addCommand(tsn);
- exports.commands.tsnDif = canon.addCommand(tsnDif);
- exports.commands.tsnExt = canon.addCommand(tsnExt);
- exports.commands.tsnExte = canon.addCommand(tsnExte);
- exports.commands.tsnExten = canon.addCommand(tsnExten);
- exports.commands.tsnExtend = canon.addCommand(tsnExtend);
- exports.commands.tsnDeep = canon.addCommand(tsnDeep);
- exports.commands.tsnDeepDown = canon.addCommand(tsnDeepDown);
- exports.commands.tsnDeepDownNested = canon.addCommand(tsnDeepDownNested);
- exports.commands.tsnDeepDownNestedCmd = canon.addCommand(tsnDeepDownNestedCmd);
- exports.commands.tselarr = canon.addCommand(tselarr);
- exports.commands.tsm = canon.addCommand(tsm);
- exports.commands.tsg = canon.addCommand(tsg);
- exports.commands.tshidden = canon.addCommand(tshidden);
- exports.commands.tscook = canon.addCommand(tscook);
- exports.commands.tslong = canon.addCommand(tslong);
- exports.commands.tsfail = canon.addCommand(tsfail);
+ types.addType(mockCommands.optionType);
+ types.addType(mockCommands.optionValue);
+
+ mockCommands.commands.tsv = canon.addCommand(tsv);
+ mockCommands.commands.tsr = canon.addCommand(tsr);
+ mockCommands.commands.tso = canon.addCommand(tso);
+ mockCommands.commands.tse = canon.addCommand(tse);
+ mockCommands.commands.tsj = canon.addCommand(tsj);
+ mockCommands.commands.tsb = canon.addCommand(tsb);
+ mockCommands.commands.tss = canon.addCommand(tss);
+ mockCommands.commands.tsu = canon.addCommand(tsu);
+ mockCommands.commands.tsf = canon.addCommand(tsf);
+ mockCommands.commands.tsn = canon.addCommand(tsn);
+ mockCommands.commands.tsnDif = canon.addCommand(tsnDif);
+ mockCommands.commands.tsnExt = canon.addCommand(tsnExt);
+ mockCommands.commands.tsnExte = canon.addCommand(tsnExte);
+ mockCommands.commands.tsnExten = canon.addCommand(tsnExten);
+ mockCommands.commands.tsnExtend = canon.addCommand(tsnExtend);
+ mockCommands.commands.tsnDeep = canon.addCommand(tsnDeep);
+ mockCommands.commands.tsnDeepDown = canon.addCommand(tsnDeepDown);
+ mockCommands.commands.tsnDeepDownNested = canon.addCommand(tsnDeepDownNested);
+ mockCommands.commands.tsnDeepDownNestedCmd = canon.addCommand(tsnDeepDownNestedCmd);
+ mockCommands.commands.tselarr = canon.addCommand(tselarr);
+ mockCommands.commands.tsm = canon.addCommand(tsm);
+ mockCommands.commands.tsg = canon.addCommand(tsg);
+ mockCommands.commands.tshidden = canon.addCommand(tshidden);
+ mockCommands.commands.tscook = canon.addCommand(tscook);
+ mockCommands.commands.tslong = canon.addCommand(tslong);
+ mockCommands.commands.tsfail = canon.addCommand(tsfail);
};
-exports.shutdown = function(opts) {
+mockCommands.shutdown = function(opts) {
canon.removeCommand(tsv);
canon.removeCommand(tsr);
canon.removeCommand(tso);
@@ -515,10 +518,10 @@ exports.shutdown = function(opts) {
canon.removeCommand(tslong);
canon.removeCommand(tsfail);
- types.removeType(exports.optionType);
- types.removeType(exports.optionValue);
+ types.removeType(mockCommands.optionType);
+ types.removeType(mockCommands.optionValue);
- exports.commands = {};
+ mockCommands.commands = {};
};
@@ -41,6 +41,13 @@ function forEachType(options, typeSpec, callback) {
var type = types.createType(typeSpec);
callback(type);
+
+ // Clean up
+ delete typeSpec.name;
+ delete typeSpec.requisition;
+ delete typeSpec.data;
+ delete typeSpec.delegateType;
+ delete typeSpec.subtype;
});
}

0 comments on commit 17f8195

Please sign in to comment.