pokemon-showdown/test/common.js
Guangcong Luo a65faf263f
Stop using assert.strict.strictEqual (#7515)
It turns out that when I switched us from `assert` to `assert.strict`,
I didn't actually update any existing tests or tell anyone:

0df0d234f2

So apparently everyone else just kept on using `strictEqual`.

This will be a PR and also throw an error if people continue trying to
use it, which should make it much clearer what PS policy is on this.

A lot of the problem may be that TypeScript marks assert.strict.equal
as deprecated when it's not. This was fixed 4 days ago:

https://github.com/DefinitelyTyped/DefinitelyTyped/pull/48452

But this probably hasn't made it to a thing yet. Until then, you'll
have to deal with TS marking your tests as deprecated, but it shouldn't
be too long.

Accidentally using `assert` instead of `assert.strict` should now show
an error. This protects against the probably much worse mistake of
accidentally using `assert.equal` rather than `assert.strict.equal`.

`assert.ok` is also deprecated now.
2020-10-14 01:19:03 -07:00

110 lines
3.0 KiB
JavaScript

'use strict';
const assert = require('./assert');
const Sim = require('./../.sim-dist');
const Dex = Sim.Dex;
const cache = new Map();
const formatsCache = new Map();
function capitalize(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}
/**
* The default random number generator seed used if one is not given.
*/
const DEFAULT_SEED = [0x09917, 0x06924, 0x0e1c8, 0x06af0];
class TestTools {
constructor(mod = 'base') {
this.currentMod = mod;
this.dex = Dex.mod(mod);
this.modPrefix = this.dex.isBase ? `[gen8] ` : `[${mod}] `;
}
mod(mod) {
if (cache.has(mod)) return cache.get(mod);
if (typeof mod !== 'string') throw new Error("This only supports strings");
if (!Dex.dexes[mod]) throw new Error(`Mod ${mod} does not exist`);
const moddedTestTools = new TestTools(mod);
cache.set(mod, moddedTestTools);
return moddedTestTools;
}
gen(genNum) {
return this.mod('gen' + genNum);
}
getFormat(options) {
if (options.formatid) return Dex.getFormat(options.formatid);
const gameType = Dex.toID(options.gameType || 'singles');
const customRules = [
options.pokemon && '-Nonexistent',
options.legality && 'Obtainable',
!options.preview && '!Team Preview',
options.sleepClause && 'Sleep Clause Mod',
!options.cancel && '!Cancel Mod',
options.endlessBattleClause && 'Endless Battle Clause',
options.inverseMod && 'Inverse Mod',
].filter(Boolean);
const customRulesID = customRules.length ? `@@@${customRules.join(',')}` : ``;
const basicFormat = this.currentMod === 'base' && gameType === 'singles' ? 'Anything Goes' : 'Custom Game';
const gameTypePrefix = gameType === 'singles' ? '' : capitalize(gameType) + ' ';
const formatName = `${this.modPrefix}${gameTypePrefix}${basicFormat}${customRulesID}`;
let format = formatsCache.get(formatName);
if (format) return format;
format = Dex.getFormat(formatName);
if (!format.exists) throw new Error(`Unidentified format: ${formatName}`);
formatsCache.set(formatName, format);
return format;
}
/**
* Creates a new Battle and returns it.
*
* @param {Object} [options]
* @param {Team[]} [teams]
* @returns {Sim.Battle} A battle.
*/
createBattle(options, teams) {
if (Array.isArray(options)) {
teams = options;
options = {};
}
if (!options) options = {};
const format = this.getFormat(options);
const battleOptions = {
format: format,
// If a seed for the pseudo-random number generator is not provided,
// a default seed (guaranteed to be the same across test executions)
// will be used.
seed: options.seed || DEFAULT_SEED,
strictChoices: options.strictChoices !== false,
};
if (!teams) return new Sim.Battle(battleOptions);
for (let i = 0; i < teams.length; i++) {
assert(Array.isArray(teams[i]), `Team provided is not an array`);
const playerSlot = `p${i + 1}`;
battleOptions[playerSlot] = {team: teams[i]};
}
return new Sim.Battle(battleOptions);
}
}
const common = exports = module.exports = new TestTools();
cache.set('base', common);
cache.set('gen8', common);