Skip to content

Commit c33f77b

Browse files
committed
Fix class evaluation context of expected expression evaluation method
1 parent eea19bf commit c33f77b

7 files changed

Lines changed: 48 additions & 25 deletions

File tree

package-lock.json

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/testing/src/Test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ export abstract class Test<T = unknown> {
121121
let expected: T;
122122
try {
123123
expected = await new Promisification<T>(
124-
this.evalExpectedExpression.apply(null, await this.promisifyExpression(...expectedExpression))
124+
this.evalExpectedExpression(...(await this.promisifyExpression(...expectedExpression)))
125125
).resolve();
126126
} catch (err: unknown) {
127-
throw new FormatError(err, "Cannot consume expected value", this.sourcePosition);
127+
throw new FormatError(err, "Cannot consume expected value:", this.sourcePosition);
128128
}
129129

130130
this.difference = this.getDifference(actual, expected);

packages/testing/src/cli/Printer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export class Printer {
4545
return Printer.colorStr(value, 34);
4646
}
4747
if (/^("|').*\1$/.test(value)) {
48-
return Printer.colorStr(value, 35);
48+
const strValue = /\\n/.test(value) ? `\n${value.slice(1, -1).replace(/\\n/g, "\n")}` : value;
49+
return Printer.colorStr(strValue, 35);
4950
}
5051
if (/^\d*(\.\d+)?$/.test(value)) {
5152
return Printer.colorStr(value, 36);

packages/testing/src/cli/cli.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ async function runSuite(): Promise<IResults> {
124124
if (!(err instanceof Error)) {
125125
console.error(
126126
`\n\x1b[31mTest suite interrupted by unexpected error${
127-
["string", "number", "boolean"].includes(typeof err) ? `:\n\n${(err as number).toString()}` : ""
127+
["string", "number", "boolean"].includes(typeof err)
128+
? (err as string | number | boolean).toString()
129+
: JSON.stringify(err)
128130
}\x1b[0m`
129131
);
130132

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"unit": "@rapidjs.org/testing-unit",
2+
"cli": "@rapidjs.org/testing-cli",
33
"http": "@rapidjs.org/testing-http",
4-
"dom": "@rapidjs.org/testing-dom"
4+
"unit": "@rapidjs.org/testing-unit"
55
}

packages/testing/template/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ rJS Testing <alias> testing suite (`<Suite>Test`): …
66

77
### Expressions
88

9-
(#### Actual)
9+
#### Actual
1010

1111
``` ts
1212
.actual(expression: any)
13-
.expected(expression: any)
1413
```
1514

16-
(#### Expected)
15+
#### Expected
16+
17+
``` ts
18+
.expected(expression: any)
19+
```
1720

1821
### Value-based Assertion
1922

packages/testing/template/src/CustomTest.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@ export class CustomTest extends Test<IObject> {
1010
public static readonly suiteTitle: string = "Custom";
1111
public static readonly suiteColor: [number, number, number] = [ 255, 255, 0 ];
1212

13-
constructor(title: string) {
14-
super(title);
15-
}
16-
17-
protected async evalActualExpression(obj: IObject): Promise<IObject> {
18-
return Object.fromEntries(
19-
Object.entries(obj)
20-
.map((entry: [ string, unknown ]) => [ entry[0].toLowerCase(), entry[1] ])
21-
) as IObject;
22-
}
23-
24-
protected isEqual(actual: IObject, expected: IObject): boolean {
25-
return !Object.keys(this.filterComparedValues(actual, expected).actual).length;
13+
protected evalActualExpression(arg1: string, arg2: boolean): IObject {
14+
// HINT:
15+
// evalActualExpression() and analog. evalExpectedExpression() are
16+
// ought to compile a value sharing a common type. he evaluation can
17+
// base on an arbitrary amount of heterogeneous arguments.
18+
// E.g.: Obtaining an HTTP response based on endpoint information.
19+
return {
20+
arg1, arg2
21+
}
2622
}
2723

28-
protected filterComparedValues(actual: IObject, expected: IObject) {
24+
protected getDifference(actual: IObject, expected: IObject) {
25+
// HINT:
26+
// Filter the previously evaluated test expression values for
27+
// differences in respect to the test purpose.
28+
// E.g.: Filtering explicitly provided HTTP response properties on
29+
// the actaul HTTP response that differ.
2930
return {
3031
actual: actual,
3132
expected: expected

0 commit comments

Comments
 (0)