Skip to content

Commit

Permalink
Task 267 (#363)
Browse files Browse the repository at this point in the history
* feat: add toSetRolePosition and manual test for it

* feat: add tests for toSetRolePosition

* feat: add testes for new function on cordeBot
  • Loading branch information
lucasgmagalhaes committed Sep 19, 2020
1 parent 266de6d commit 5e6a4bd
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/api/expectMatches/operation.ts
Expand Up @@ -62,15 +62,15 @@ export abstract class ExpectOperation<P1 = any, P2 = any, P3 = any> {
}
}

protected generateReport(showExpectAndOutputValue = false, customReturnMessage?: string) {
protected generateReport(showExpectAndOutputValue = false) {
return new TestReport({
commandName: this.command,
expectation: this.expectation,
hasPassed: this.defineIsEqual(this.isEqual),
isNot: this.isNot,
output: this.output,
showExpectAndOutputValue,
customReturnMessage,
customReturnMessage: this.customReturnMessage,
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/api/expectMatches/role/index.ts
@@ -1,2 +1,6 @@
export { toSetRoleColor } from "./toSetRoleColor";
export { toDeleteRole } from "./toDeleteRole";
export { ToSetRolePosition } from "./toSetRolePosition";
export { ToSetRoleMentionable } from "./toSetRoleMentionable";
export { ToSetRoleHoist } from "./toSetRoleHoist";
export { ToRenameRole } from "./toRenameRole";
2 changes: 1 addition & 1 deletion src/api/expectMatches/role/toSetRoleMentionable.ts
Expand Up @@ -2,7 +2,7 @@ import { ExpectOperation } from "../operation";
import { RoleData } from "discord.js";
import { TestReport } from "../..";

export default class ToSetRoleMentionable extends ExpectOperation<boolean, RoleData> {
export class ToSetRoleMentionable extends ExpectOperation<boolean, RoleData> {
public async action(mentionable: boolean, roleData: RoleData): Promise<TestReport> {
try {
await this.cordeBot.sendTextMessage(this.command);
Expand Down
36 changes: 36 additions & 0 deletions src/api/expectMatches/role/toSetRolePosition.ts
@@ -0,0 +1,36 @@
import { ExpectOperation } from "../operation";
import { TestReport } from "../..";
import { RoleData } from "../../../types";

export class ToSetRolePosition extends ExpectOperation<number, RoleData> {
public async action(newPosition: number, roleData: RoleData): Promise<TestReport> {
try {
let role = await this.cordeBot.findRole(roleData);
const lastRole = this.cordeBot
.getRoles()
.sort((r1, r2) => r2.position - r1.position)
.first();

if (!role) {
this.isEqual = false;
this.forceIsEqualValue = true;
this.output = "Role not found";
} else if (newPosition > lastRole.position) {
this.forceIsEqualValue = true;
this.customReturnMessage = `the maximum position possible is ${lastRole.position}. Attempted value: ${newPosition}`;
} else {
await this.cordeBot.sendTextMessage(this.command);
role = await this.cordeBot.findRole(roleData);
if (role.position === newPosition) {
this.isEqual = true;
} else {
this.customReturnMessage = `expected position: ${newPosition}, actual position: ${role.position}`;
}
}
} catch (error) {
this.catchExecutionError(error);
}

return this.generateReport();
}
}
15 changes: 15 additions & 0 deletions src/api/interfaces/roleMatches.ts
Expand Up @@ -86,4 +86,19 @@ export interface RoleMatches {
*/
toRenameRole(newName: string, id: string): void;
toRenameRole(newName: string, roleData: RoleData): void;

/**
* Defines a new position for the role.
*
* @param newPosition The new position of the role.
* @param id Identifier of the role. Can also use RoleData to filter it.
*
* @description Role's maximum value depends of the amount of roles the guid Have.
* So, if there is only 3 roles (including the default *everyone*), the maximum
* position that a role can have is 2 (The count begins with 0, So: 0, 1, 2).
*
* @see https://discord.com/developers/docs/topics/permissions
*/
toSetRolePosition(newPosition: number, id: string): void;
toSetRolePosition(newPosition: number, roleData: RoleData): void;
}
47 changes: 31 additions & 16 deletions src/api/matcher.ts
@@ -1,16 +1,22 @@
import { MessageEmbed, ColorResolvable, Snowflake } from "discord.js";
import { ColorResolvable, MessageEmbed, Snowflake } from "discord.js";
import { testCollector } from "../common/testCollector";
import { toReturn, toAddReaction, toSetRoleColor, toDeleteRole } from "./expectMatches";
import { MessageMatches } from "./interfaces/messageMatches";
import { CordeBot } from "../core";
import { MessageData, RoleData } from "../types";
import { toRemoveReaction } from "./expectMatches/message/toRemoveReaction";
import { RoleMatches, TestReport } from "./interfaces";
import { Colors } from "../utils/colors";
import ToSetRoleMentionable from "./expectMatches/role/toSetRoleMentionable";
import {
toAddReaction,
toDeleteRole,
ToRenameRole,
toReturn,
toSetRoleColor,
ToSetRoleHoist,
ToSetRoleMentionable,
ToSetRolePosition,
} from "./expectMatches";
import { toRemoveReaction } from "./expectMatches/message/toRemoveReaction";
import { ExpectOperation } from "./expectMatches/operation";
import { CordeBot } from "../core";
import { ToSetRoleHoist } from "./expectMatches/role/toSetRoleHoist";
import { ToRenameRole } from "./expectMatches/role/toRenameRole";
import { RoleMatches, TestReport } from "./interfaces";
import { MessageMatches } from "./interfaces/messageMatches";

/**
* Defines all functions that can be used
Expand Down Expand Up @@ -113,6 +119,15 @@ class ExpectMatches implements Matches {
});
}

public toSetRolePosition(newPosition: number, id: string): void;
public toSetRolePosition(newPosition: number, roleData: RoleData): void;
public toSetRolePosition(newPosition: number, roleData: string | RoleData) {
const data = this.getRoleData(roleData);
testCollector.addTestFunction((cordeBot) => {
return this.operationFactory(ToSetRolePosition, cordeBot, newPosition, data);
});
}

protected getRoleData(roleData: string | RoleData) {
let data: RoleData;
if (typeof roleData === "string") {
Expand All @@ -126,23 +141,23 @@ class ExpectMatches implements Matches {
protected operationFactory<T extends ExpectOperation<P1>, P1>(
type: new (cordeBot: CordeBot, command: string, isNot: boolean) => T,
cordeBot: CordeBot,
p1: P1,
parameter1: P1,
): Promise<TestReport>;
protected operationFactory<T extends ExpectOperation<P1, P2>, P1, P2>(
type: new (cordeBot: CordeBot, command: string, isNot: boolean) => T,
cordeBot: CordeBot,
p1: P1,
p2: P2,
parameter1: P1,
parameter2: P2,
): Promise<TestReport>;
protected operationFactory<T extends ExpectOperation<P1, P2, P3>, P1, P2, P3>(
type: new (cordeBot: CordeBot, command: string, isNot: boolean) => T,
cordeBot: CordeBot,
p1?: P1,
p2?: P2,
p3?: P3,
parameter1?: P1,
parameter2?: P2,
parameter3?: P3,
): Promise<TestReport> {
const op = new type(cordeBot, this._commandName, this._isNot);
return op.action(p1, p2, p3);
return op.action(parameter1, parameter2, parameter3);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/cordeBot.ts
Expand Up @@ -248,6 +248,10 @@ export class CordeBot extends Events {
return null;
}

public getRoles() {
return this.guild.roles.cache;
}

/**
* Search for messages based in a filter query.
*/
Expand Down
49 changes: 29 additions & 20 deletions src/core/reporter.ts
Expand Up @@ -7,12 +7,12 @@ const SPACE = " ";
const DEFAULT_SPACE_VALUE = 4;

class Reporter {
private readonly _bgSucess = chalk.bgRgb(21, 194, 19);
private readonly _bgSuccess = chalk.bgRgb(21, 194, 19);
private readonly _bgError = chalk.bgRed;
private readonly _bold = chalk.bold;
private readonly _red = chalk.red;
private readonly _bgSucessBold = this._bgSucess.bold;
private _sucessCount = 0;
private readonly _bgSuccessBold = this._bgSuccess.bold;
private _successCount = 0;
private _failureCount = 0;

/**
Expand Down Expand Up @@ -71,8 +71,8 @@ class Reporter {

private printAssertion(report: TestReport, tab: string) {
if (report.hasPassed) {
this.printSucess(tab, report);
this._sucessCount++;
this.printSuccess(tab, report);
this._successCount++;
} else {
this._failureCount++;
this.printFailure(tab, report);
Expand All @@ -91,22 +91,22 @@ class Reporter {
}

private doesAllTestsPassed() {
return this._failureCount === 0 && this._sucessCount > 0;
return this._failureCount === 0 && this._successCount > 0;
}

private doesSomeTestsPassed() {
return this._failureCount > 0 && this._sucessCount > 0;
return this._failureCount > 0 && this._successCount > 0;
}

private printFullSuccess() {
console.log("All tests passed!");
console.log(`${this._bgSucess(" TOTAL: ")} ${chalk.bold(this._sucessCount)}`);
console.log(`${this._bgSuccess(" TOTAL: ")} ${chalk.bold(this._successCount)}`);
}

private printPartialSuccess() {
console.log("Tests passed with errors.");
console.log(`${this._bgError(" FAILURES: ")} ${chalk.bold(this._failureCount)}`);
console.log(`${this._bgSucess(" SUCESS: ")} ${chalk.bold(this._sucessCount)}`);
console.log(`${this._bgSuccess(" SUCCESS: ")} ${chalk.bold(this._successCount)}`);
}

private printFullFailure() {
Expand All @@ -115,18 +115,27 @@ class Reporter {
}

private printFailure(tabSpace: string, report: TestReport) {
if (report.showExpectAndOutputValue) {
this.printFailureWithValues(tabSpace, report);
console.log(report);
if (report.customReturnMessage) {
console.log(
`${tabSpace} ${this._bgSuccess.bold(" PASS ")} command ${chalk.bold(report.commandName)} ${
report.customReturnMessage
}`,
);
} else {
this.printFailureOnlyWithCommandName(tabSpace, report.commandName);
if (report.showExpectAndOutputValue) {
this.printFailureWithValues(tabSpace, report);
} else {
this.printFailureOnlyWithCommandName(tabSpace, report.commandName);
}
}
}

private printSucess(tabSpace: string, report: TestReport) {
private printSuccess(tabSpace: string, report: TestReport) {
if (report.showExpectAndOutputValue) {
this.printSucessWithValues(tabSpace, report);
this.printSuccessWithValues(tabSpace, report);
} else {
this.printSucessOnlyWithCommandName(tabSpace, report.commandName);
this.printSuccessOnlyWithCommandName(tabSpace, report.commandName);
}
}

Expand All @@ -144,26 +153,26 @@ class Reporter {

private printFailureOnlyWithCommandName(tabSpace: string, commandName: string) {
console.log(
`${tabSpace} ${this._bgSucess.bgRed(" FAIL ")} command ${this._bold(
`${tabSpace} ${this._bgSuccess.bgRed(" FAIL ")} command ${this._bold(
commandName,
)} not returned what was expected`,
);
}

private printSucessWithValues(tabSpace: string, report: TestReport) {
private printSuccessWithValues(tabSpace: string, report: TestReport) {
const notWord = this.getNotWordIfTrue(report.isNot);
console.log(
`${tabSpace} ${this._bgSucessBold(" PASS ")} expected ${chalk.bold(
`${tabSpace} ${this._bgSuccessBold(" PASS ")} expected ${chalk.bold(
report.commandName,
)} to${notWord}return '${chalk.bold(
this.getPrintingValueByType(report.expectation),
)}'. Returned: '${chalk.green(this.getPrintingValueByType(report.output))}'`,
);
}

private printSucessOnlyWithCommandName(tabSpace: string, commandName: string) {
private printSuccessOnlyWithCommandName(tabSpace: string, commandName: string) {
console.log(
`${tabSpace} ${this._bgSucess.bold(" PASS ")} command ${chalk.bold(
`${tabSpace} ${this._bgSuccess.bold(" PASS ")} command ${chalk.bold(
commandName,
)} returned what was expected`,
);
Expand Down

0 comments on commit 5e6a4bd

Please sign in to comment.