pokemon-showdown/test/simulator/misc/random-teams.js
Dan Pantry 56ba782966 refactor PRNG to its own class
This removes the 'deterministic test' tools by preventing action at a
distance (namely, preventing the modification of the `init` method in
`Battle` during tests). This action at a distance is incredibly
confusing.

All this action at a distance did was discard any parameters that were
passed to `Battle` that weren't the first three (which was probably a
mistake by the original author) and also hard code `this.seed` and
`this.startingSeed` in `Battle`.

This functionality has now been moved to the `PRNG` class, so instead
users should pass a `PRNG` to `Battle` as the 5th constructor argument.
Users can also pass one as the third argument to `common.createBattle`
or use `common.createBattleWithPRNG` with the PRNG as the first
argument.

The PRNG is just an encapsulation of the pseudo-random algorithm in a
class. It is stateful, so make sure to take a `clone()` of the PRNG if
you want to re-use it.
2017-02-22 20:44:20 -05:00

105 lines
2.6 KiB
JavaScript

'use strict';
const common = require('./../../common');
let battle;
const TOTAL_TEAMS = 100;
const ALL_GENS = [1, 2/*, 3, 4*/, 5, 6, 7];
function isValidSet(gen, set) {
const tools = Tools.mod(`gen${gen}`);
const template = tools.getTemplate(set.species || set.name);
if (!template.exists || template.gen > gen) return false;
if (set.item) {
const item = tools.getItem(set.item);
if (!item.exists || item.gen > gen) {
return false;
}
}
if (set.ability && set.ability !== 'None') {
const ability = tools.getAbility(set.ability);
if (!ability.exists || ability.gen > gen) {
return false;
}
} else if (gen >= 3) {
return false;
}
return true;
}
describe(`Random Team generator`, function () {
afterEach(() => battle.destroy());
for (const gen of ALL_GENS) {
it(`should successfully create valid Gen ${gen} teams`, function () {
this.timeout(0);
battle = common.gen(gen).createBattle();
let teamCount = TOTAL_TEAMS;
while (teamCount--) {
let seed = battle.prng.seed.slice();
let team = null;
try {
team = battle.randomTeam(battle.p1);
let invalidSet = team.find(set => !isValidSet(gen, set));
if (invalidSet) throw new Error(`Invalid set: ${JSON.stringify(invalidSet)}`);
} catch (err) {
err.message += ` (seed ${seed})`;
throw err;
}
}
});
}
});
describe(`Challenge Cup Team generator`, function () {
afterEach(() => battle.destroy());
for (const gen of ALL_GENS) {
it(`should successfully create valid Gen ${gen} teams`, function () {
this.timeout(0);
battle = common.gen(gen).createBattle();
let teamCount = TOTAL_TEAMS;
while (teamCount--) {
let seed = battle.prng.seed.slice();
let team = null;
try {
team = battle.randomCCTeam(battle.p1);
let invalidSet = team.find(set => !isValidSet(gen, set));
if (invalidSet) throw new Error(`Invalid set: ${JSON.stringify(invalidSet)}`);
} catch (err) {
err.message += ` (seed ${seed})`;
throw err;
}
}
});
}
});
describe(`Hackmons Cup Team generator`, function () {
afterEach(() => battle.destroy());
for (const gen of ALL_GENS) {
it(`should successfully create valid Gen ${gen} teams`, function () {
this.timeout(0);
battle = common.gen(gen).createBattle();
let teamCount = TOTAL_TEAMS;
while (teamCount--) {
let seed = battle.prng.seed.slice();
let team = null;
try {
team = battle.randomHCTeam(battle.p1);
let invalidSet = team.find(set => !isValidSet(gen, set));
if (invalidSet) throw new Error(`Invalid set: ${JSON.stringify(invalidSet)}`);
} catch (err) {
err.message += ` (seed ${seed})`;
throw err;
}
}
});
}
});