corde
Version:
A simple library for Discord bot tests
183 lines (166 loc) • 4.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true,
});
exports.expect = void 0;
const matcher_1 = require("./matcher");
function getMessageMatchers() {
return getFunctions(matcher_1.MessageMatches);
}
function getRoleMatchers() {
return getFunctions(matcher_1.RoleMatchesImpl);
}
function getToHaveResultsMatchers() {
return getFunctions(matcher_1.ToHaveResultMatcher);
}
function getFunctions(type) {
return Object.getOwnPropertyNames(type.prototype).filter(
(propName) => propName !== "constructor" && typeof type.prototype[propName] === "function",
);
}
const expectation = {
not: {},
inGuild: {},
inChannel: {},
};
function createTestFunction(classType, params, functionName) {
return (...args) => new classType(params)[functionName](...args);
}
function createTestsFromMatches(names, params, classType) {
return names.map((name) => {
return [name, createTestFunction(classType, params, name)];
});
}
function set(from, to) {
from.forEach((test) => {
to[test[0]] = test[1];
});
}
const messageTestNames = getMessageMatchers();
const roleMatchers = getRoleMatchers();
const resultMatchers = getToHaveResultsMatchers();
const _expect = (commandName, channelId) => {
const baseMatcherConstructor = {
commandName,
channelIdToSendCommand: channelId,
};
const messageTests = createTestsFromMatches(
messageTestNames,
baseMatcherConstructor,
matcher_1.MessageMatches,
);
const todoInCascadeMatcher = createTestsFromMatches(
resultMatchers,
baseMatcherConstructor,
matcher_1.ToHaveResultMatcher,
);
const todoInCascadeMatcherIsNot = createTestsFromMatches(
resultMatchers,
{ ...baseMatcherConstructor, isNot: true },
matcher_1.ToHaveResultMatcher,
);
set(todoInCascadeMatcher, expectation);
set(todoInCascadeMatcherIsNot, expectation.not);
const isNotMessageTests = createTestsFromMatches(
messageTestNames,
{ ...baseMatcherConstructor, isNot: true },
matcher_1.MessageMatches,
);
const roleTests = createTestsFromMatches(
roleMatchers,
baseMatcherConstructor,
matcher_1.RoleMatchesImpl,
);
const isNotRoleTests = createTestsFromMatches(
roleMatchers,
{
commandName,
isNot: true,
},
matcher_1.RoleMatchesImpl,
);
set(messageTests, expectation);
set(isNotMessageTests, expectation.not);
set(roleTests, expectation);
set(isNotRoleTests, expectation.not);
expectation.inGuild = (guildId) => {
const inGuildMatches = {
not: {},
};
const roleTests = createTestsFromMatches(
roleMatchers,
{ ...baseMatcherConstructor, guildId },
matcher_1.RoleMatchesImpl,
);
const isNotRoleTests = createTestsFromMatches(
roleMatchers,
{ ...baseMatcherConstructor, guildId, isNot: true },
matcher_1.RoleMatchesImpl,
);
set(roleTests, inGuildMatches);
set(isNotRoleTests, inGuildMatches.not);
return inGuildMatches;
};
expectation.inChannel = (channelId) => {
const inChannelMatches = {
not: {},
};
const roleTests = createTestsFromMatches(
messageTestNames,
{ ...baseMatcherConstructor, channelId },
matcher_1.MessageMatches,
);
const isNotRoleTests = createTestsFromMatches(
messageTestNames,
{ ...baseMatcherConstructor, channelId, isNot: true },
matcher_1.MessageMatches,
);
set(roleTests, inChannelMatches);
set(isNotRoleTests, inChannelMatches.not);
return inChannelMatches;
};
return expectation;
};
const messageTests = createTestsFromMatches(
messageTestNames,
{
commandName: "",
isNot: false,
isCascade: true,
},
matcher_1.MessageMatches,
);
const isNotMessageTests = createTestsFromMatches(
messageTestNames,
{
commandName: "",
isNot: true,
isCascade: true,
},
matcher_1.MessageMatches,
);
const roleTests = createTestsFromMatches(
roleMatchers,
{
commandName: "",
isNot: false,
isCascade: true,
},
matcher_1.RoleMatchesImpl,
);
const isNotRoleTests = createTestsFromMatches(
roleMatchers,
{
commandName: "",
isNot: true,
isCascade: true,
},
matcher_1.RoleMatchesImpl,
);
_expect.not = {};
set(messageTests, _expect);
set(isNotMessageTests, _expect.not);
set(roleTests, _expect);
set(isNotRoleTests, _expect.not);
const expect = _expect;
exports.expect = expect;