Skip to content

Commit 27db3d0

Browse files
committed
fixes
1 parent f728994 commit 27db3d0

File tree

5 files changed

+38
-174
lines changed

5 files changed

+38
-174
lines changed

core/test_case.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { AssertionError, ESCAPE_SEQUENCES } from "datex-core-legacy/datex_all.ts
33
import { logger } from "./utils.ts";
44
import { getBoxWidth } from "./constants.ts";
55
import { Path } from "datex-core-legacy/utils/path.ts";
6-
import { getCallerInfo } from "datex-core-legacy/utils/caller_metadata.ts";
76
import { TestManager } from "./test_manager.ts";
87
import { fitText } from "./fitText.ts";
98

@@ -203,6 +202,23 @@ export interface TestGroupOptions {
203202
else return TEST_CASE_STATE.SUCCESSFUL;
204203
}
205204

205+
// TODO: fix, getState works but .state does not (when printing short report)
206+
@property getState() {
207+
let has_failed = false,
208+
has_running = false,
209+
has_initialized = false;
210+
211+
for (const test of this.test_cases.values()) {
212+
if (test.state == TEST_CASE_STATE.FAILED) has_failed = true;
213+
else if (test.state == TEST_CASE_STATE.RUNNING) has_running = true;
214+
else if (test.state == TEST_CASE_STATE.INITIALIZED) has_initialized = true;
215+
}
216+
217+
if (has_initialized) return TEST_CASE_STATE.INITIALIZED;
218+
else if (has_running) return TEST_CASE_STATE.RUNNING;
219+
else if (has_failed) return TEST_CASE_STATE.FAILED;
220+
else return TEST_CASE_STATE.SUCCESSFUL;
221+
}
206222

207223
get duration() {
208224
let duration = 0;
@@ -362,17 +378,18 @@ export interface TestGroupOptions {
362378
_logger.flush();
363379
}
364380

365-
printReportShort(_logger = logger) {
381+
async printReportShort(_logger = logger) {
366382

367383
const fileName = new Path(this.context).name;
368384
const groupTitleLength = getBoxWidth() - fileName.length;
369385

386+
const ownState = await this.getState();
370387

371388
_logger.lock();
372389

373-
if (this.state == TEST_CASE_STATE.SUCCESSFUL)
390+
if (ownState == TEST_CASE_STATE.SUCCESSFUL)
374391
_logger.plain `[[#bold#color(green) PASS ]]#bold#color(green) ${(this.formatted_name.padEnd(groupTitleLength - 10, " "))} #reset#color(grey)(${fileName})#reset`
375-
else if (this.state == TEST_CASE_STATE.FAILED) {
392+
else if (ownState == TEST_CASE_STATE.FAILED) {
376393
_logger.plain `[[#bold#color(red) FAIL ]]#bold#color(red) ${(this.formatted_name.padEnd(groupTitleLength - 10, " "))} #reset#color(grey)(${fileName})#reset`
377394

378395
// test cases
@@ -399,7 +416,7 @@ export interface TestGroupOptions {
399416
}
400417
}
401418

402-
else if (this.state == TEST_CASE_STATE.RUNNING) _logger.plain `[[#bold#color(cyan) RUNNING ]]#bold#color(cyan) ${(this.formatted_name.padEnd(groupTitleLength - 13, " "))} #reset#color(grey)(${fileName})#reset`
419+
else if (ownState == TEST_CASE_STATE.RUNNING) _logger.plain `[[#bold#color(cyan) RUNNING ]]#bold#color(cyan) ${(this.formatted_name.padEnd(groupTitleLength - 13, " "))} #reset#color(grey)(${fileName})#reset`
403420
else _logger.plain `[[#bold#color(cyan) INITIALIZED ]]#bold#color(cyan) ${(this.formatted_name.padEnd(groupTitleLength - 17, " "))} #reset#color(grey)(${fileName})#reset`
404421

405422

core/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ export function printHeaderInfo(files:URL[]){
134134

135135
logger.plain `${main_color}╔═ [[ unyt tests ]]#reset ${VERSION}${main_color}${' '.padEnd(getBoxWidth()-17-VERSION.length, '═')}
136136
${main_color}${' '.repeat(getBoxWidth()-2)}
137-
${main_color}║ #color(white)Test Files:${' '.repeat(getBoxWidth()-15)}${main_color}║`
137+
${main_color}║ #color(white)${files.length?'Test Files:' : 'No test files found'}${' '.repeat(getBoxWidth()-(files.length?15:23))}${main_color}║`
138138

139139
for (const file of files) {
140140
logger.plain `${main_color}║ #color(grey)${file.toString().replace("file://","").padEnd(getBoxWidth()-7, ' ')}${main_color}║`
141141
}
142142

143143
logger.plain `${main_color}${' '.repeat(getBoxWidth()-2)}${main_color}║`
144144

145-
logger.plain `${main_color}║ #color(white)Endpoint: #color(grey)${Datex.Runtime.endpoint.toString().padEnd(getBoxWidth()-14, ' ')}${main_color}║`
145+
if (files.length) logger.plain `${main_color}║ #color(white)Endpoint: #color(grey)${Datex.Runtime.endpoint.toString().padEnd(getBoxWidth()-14, ' ')}${main_color}║`
146146

147147
logger.plain `${main_color}${' '.repeat(getBoxWidth()-2)}║`
148148
logger.plain `${main_color}${'═'.repeat(getBoxWidth()-2)}╝`

runners/typescript_test_runner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { Path } from "datex-core-legacy/utils/path.ts";
55

66
import { type DocNode, type ClassDef, type FunctionDef } from "https://deno.land/x/deno_doc@0.58.0/lib/types.d.ts";
77
import { TestGroupOptions } from "../core/test_case.ts";
8-
import { METADATA } from "../testing/legacy_decorators.ts";
98
import { DEFAULT_TIMEOUT, TEST_CASE_DATA, TEST_GROUP_DATA, TIMEOUT } from "../testing/test.ts";
109
import { AssertionError } from "datex-core-legacy/types/errors.ts";
10+
import { METADATA } from "datex-core-legacy/js_adapter/js_class_adapter.ts";
1111
const doc = globalThis.Deno ? (await import("https://deno.land/x/deno_doc@0.58.0/mod.ts")).doc : null;
1212
const json5 = globalThis.Deno ? await import("https://deno.land/x/json5@v1.0.0/mod.ts") : null;
1313

testing/legacy_decorators.ts

-135
This file was deleted.

testing/test.ts

+13-31
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010

1111
import { Datex } from "datex-core-legacy";
1212
import { AssertionError, Disjunction, Endpoint, Logger, LOG_LEVEL, logger } from "datex-core-legacy/datex_all.ts";
13-
import { handleDecoratorArgs, METADATA } from "./legacy_decorators.ts";
14-
import type { context_kind, context_meta_getter, context_meta_setter, context_name } from "./legacy_decorators.ts";
1513
import { TestGroupOptions } from "../core/test_case.ts";
16-
import { handleClassDecoratorWithOptionalArgs } from "datex-core-legacy/mod.ts";
17-
import { Decorators } from "datex-core-legacy/js_adapter/js_class_adapter.ts";
14+
import { handleClassDecoratorWithOptionalArgs, handleClassMethodDecoratorWithArgs } from "datex-core-legacy/mod.ts";
15+
import { Decorators, METADATA } from "datex-core-legacy/js_adapter/js_class_adapter.ts";
1816

1917
export * from "./assertions.ts";
2018

@@ -122,23 +120,16 @@ async function registerTests(group_name:string, value:Function){
122120
}
123121

124122

125-
126-
127-
128123
export const TEST_GROUP_DATA = Symbol("test_group_data");
129124
export const TEST_CASE_DATA = Symbol("test_case");
130125
export const TIMEOUT = Symbol("timeout");
131126
export const DEFAULT_TIMEOUT = 60; // 60s
132127

133-
type decorator_target = {[key: string]: any} & Partial<Record<keyof Array<any>, never>>;
134-
135-
136-
// @Test (legacy decorators support)
137128
export function Test(name:string): (value: Function, context: ClassDecoratorContext|ClassMethodDecoratorContext) => void
138129
export function Test(options: TestGroupOptions): (value: Function, context: ClassDecoratorContext) => void
139130
export function Test(name:string, options: TestGroupOptions): (value: Function, context: ClassDecoratorContext) => void
140131

141-
export function Test(test_parameters:(unknown|unknown[])[]): (value: Function, context: ClassMethodDecoratorContext) => void
132+
export function Test<F extends (...args: any) => any>(test_parameters:(Parameters<F>|(Parameters<F>['length'] extends 1 ? Parameters<F>[0] : never))[]): (value: F, context: ClassMethodDecoratorContext) => void
142133
export function Test(name:string, test_parameters:(unknown|unknown[])[]): (value: Function, context: ClassMethodDecoratorContext) => void
143134
// export function Test(name:string, test_parameters:(unknown|unknown[])[], options:TestGroupOptions):any
144135

@@ -195,25 +186,16 @@ function _Test(value:any, context:ClassDecoratorContext|ClassMethodDecoratorCont
195186
}
196187

197188

198-
// @Timeout (legacy decorators support) - DEFAULT Timeout is 60s, MAX depends on test manager timeout, currently 10min
199-
export function Timeout(seconds:number):any
200-
export function Timeout(target: Function):any
201-
export function Timeout(target: Function, options:any):any
202-
export function Timeout(...args:any[]) {return handleDecoratorArgs(args, <any>_Timeout)}
203-
204-
function _Timeout(value:any, name:context_name, kind:context_kind, _is_static:boolean, _is_private:boolean, setMetadata:context_meta_setter, getMetadata:context_meta_getter, params:[number]) {
205-
206-
if (kind == 'method') {
207-
if (!params || typeof params[0] != "number") throw new Error("The @Timeout requires a timeout value (in seconds) as a parameter");
208-
209-
const test_name = name;
210-
setMetadata(TIMEOUT, params[0]);
211-
}
212-
213-
else {
214-
throw new Error("The @Timeout decorator can only be used on test case methods");
215-
}
216-
189+
/**
190+
* Set a custom timeout for a test case (default is 60s, max depends on test manager timeout, currently 10min)
191+
* @param seconds timeout in seconds
192+
* @returns
193+
*/
194+
export function Timeout(seconds: number) {
195+
return handleClassMethodDecoratorWithArgs([seconds], ([seconds], value, context) => {
196+
if (typeof seconds !== "number") throw new Error("The @Timeout decorator requires a timeout value (in seconds) as a parameter");
197+
Decorators.setMetadata(context, TIMEOUT, seconds);
198+
})
217199
}
218200

219201
// interface TestManager in main process - ignored if isSameProcess == true

0 commit comments

Comments
 (0)