Skip to content
Permalink
Browse files

Breaking: convert RuleTester to ES6 class (refs #8231) (#8263)

This converts RuleTester to an ES6 class. This will not break clients that were using RuleTester as documented, but it could break clients that are relying on undocumented behavior in RuleTester (e.g. enumerable methods).
  • Loading branch information...
not-an-aardvark authored and gyandeeps committed Apr 4, 2017
1 parent 6f7757e commit c778676204bfbb135edaa8a12ed5958c844d9492
Showing with 84 additions and 91 deletions.
  1. +84 −91 lib/testers/rule-tester.js
@@ -129,114 +129,106 @@ function freezeDeeply(x) {
// Public Interface
//------------------------------------------------------------------------------

// default separators for testing
const DESCRIBE = Symbol("describe");
const IT = Symbol("it");

/**
* Creates a new instance of RuleTester.
* @param {Object} [testerConfig] Optional, extra configuration for the tester
* @constructor
* This is `it` or `describe` if those don't exist.
* @this {Mocha}
* @param {string} text - The description of the test case.
* @param {Function} method - The logic of the test case.
* @returns {any} Returned value of `method`.
*/
function RuleTester(testerConfig) {
function defaultHandler(text, method) {
return method.apply(this);
}

class RuleTester {

/**
* The configuration to use for this tester. Combination of the tester
* configuration and the default configuration.
* @type {Object}
* Creates a new instance of RuleTester.
* @param {Object} [testerConfig] Optional, extra configuration for the tester
* @constructor
*/
this.testerConfig = lodash.merge(
constructor(testerConfig) {

/**
* The configuration to use for this tester. Combination of the tester
* configuration and the default configuration.
* @type {Object}
*/
this.testerConfig = lodash.merge(

// we have to clone because merge uses the first argument for recipient
lodash.cloneDeep(defaultConfig),
testerConfig
);

// we have to clone because merge uses the first argument for recipient
lodash.cloneDeep(defaultConfig),
testerConfig
);
/**
* Rule definitions to define before tests.
* @type {Object}
*/
this.rules = {};
}

/**
* Rule definitions to define before tests.
* @type {Object}
* Set the configuration to use for all future tests
* @param {Object} config the configuration to use.
* @returns {void}
*/
this.rules = {};
}
static setDefaultConfig(config) {
if (typeof config !== "object") {
throw new Error("RuleTester.setDefaultConfig: config must be an object");
}
defaultConfig = config;

/**
* Set the configuration to use for all future tests
* @param {Object} config the configuration to use.
* @returns {void}
*/
RuleTester.setDefaultConfig = function(config) {
if (typeof config !== "object") {
throw new Error("RuleTester.setDefaultConfig: config must be an object");
// Make sure the rules object exists since it is assumed to exist later
defaultConfig.rules = defaultConfig.rules || {};
}
defaultConfig = config;

// Make sure the rules object exists since it is assumed to exist later
defaultConfig.rules = defaultConfig.rules || {};
};

/**
* Get the current configuration used for all tests
* @returns {Object} the current configuration
*/
RuleTester.getDefaultConfig = function() {
return defaultConfig;
};
/**
* Get the current configuration used for all tests
* @returns {Object} the current configuration
*/
static getDefaultConfig() {
return defaultConfig;
}

/**
* Reset the configuration to the initial configuration of the tester removing
* any changes made until now.
* @returns {void}
*/
RuleTester.resetDefaultConfig = function() {
defaultConfig = lodash.cloneDeep(testerDefaultConfig);
};
/**
* Reset the configuration to the initial configuration of the tester removing
* any changes made until now.
* @returns {void}
*/
static resetDefaultConfig() {
defaultConfig = lodash.cloneDeep(testerDefaultConfig);
}

// default separators for testing
const DESCRIBE = Symbol("describe");
const IT = Symbol("it");

RuleTester[DESCRIBE] = RuleTester[IT] = null;
// If people use `mocha test.js --watch` command, `describe` and `it` function
// instances are different for each execution. So `describe` and `it` should get fresh instance
// always.
static get describe() {
return (
this[DESCRIBE] ||
(typeof describe === "function" ? describe : defaultHandler)
);
}

/**
* This is `it` or `describe` if those don't exist.
* @this {Mocha}
* @param {string} text - The description of the test case.
* @param {Function} method - The logic of the test case.
* @returns {any} Returned value of `method`.
*/
function defaultHandler(text, method) {
return method.apply(this);
}
static set describe(value) {
this[DESCRIBE] = value;
}

// If people use `mocha test.js --watch` command, `describe` and `it` function
// instances are different for each execution. So this should get fresh instance
// always.
Object.defineProperties(RuleTester, {
describe: {
get() {
return (
RuleTester[DESCRIBE] ||
(typeof describe === "function" ? describe : defaultHandler)
);
},
set(value) {
RuleTester[DESCRIBE] = value;
},
configurable: true,
enumerable: true
},
it: {
get() {
return (
RuleTester[IT] ||
(typeof it === "function" ? it : defaultHandler)
);
},
set(value) {
RuleTester[IT] = value;
},
configurable: true,
enumerable: true
static get it() {
return (
this[IT] ||
(typeof it === "function" ? it : defaultHandler)
);
}
});

RuleTester.prototype = {
static set it(value) {
this[IT] = value;
}

/**
* Define a rule for one particular run of tests.
@@ -246,7 +238,7 @@ RuleTester.prototype = {
*/
defineRule(name, rule) {
this.rules[name] = rule;
},
}

/**
* Adds a new rule test to execute.
@@ -562,7 +554,8 @@ RuleTester.prototype = {

return result.suite;
}
};
}

RuleTester[DESCRIBE] = RuleTester[IT] = null;

module.exports = RuleTester;

0 comments on commit c778676

Please sign in to comment.
You can’t perform that action at this time.