Skip to content

Commit d16c831

Browse files
committed
Improve Action interface + add testee param to act
1 parent e80c3a6 commit d16c831

File tree

8 files changed

+52
-19
lines changed

8 files changed

+52
-19
lines changed

src/framework/Testee.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ export function getValue(object: any, field: string): any {
3939
return object;
4040
}
4141

42-
function act<T>(action: Action<T>): Promise<T> {
43-
return action();
44-
}
45-
4642
export class Testee { // TODO unified with testbed interface
4743

4844
/** The current state for each described test */
@@ -153,7 +149,7 @@ export class Testee { // TODO unified with testbed interface
153149
let actual: Object | void;
154150
if (step.instruction.kind === Kind.Action) {
155151
actual = await timeout<Object | void>(`performing action . ${step.title}`, testee.timeout,
156-
act(step.instruction.value));
152+
step.instruction.value.act(testee));
157153
} else {
158154
actual = await timeout<Object | void>(`sending instruction ${step.instruction.value.type}`, testee.timeout,
159155
testee.testbed.sendRequest(map, step.instruction.value));

src/framework/scenario/Actions.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
11
import {setTimeout} from 'timers/promises';
2+
import {Testee} from '../Testee';
3+
import {TestbedEvents} from '../../testbeds/Testbed';
4+
import {Breakpoint} from '../../debug/Breakpoint';
5+
import {breakpointHitParser} from '../../messaging/Parsers';
26

3-
export type Action<T> = () => Promise<T>;
7+
export interface Action<T> {
8+
act: (testee: Testee) => Promise<T>;
9+
}
10+
11+
export interface PureAction<T> extends Action<T> {
12+
act: () => Promise<T>;
13+
}
14+
15+
export function wait(time: number): PureAction<boolean> {
16+
return {act: () => setTimeout(time).then(() => true)}
17+
}
18+
19+
export function awaitBreakpoint(): Action<Breakpoint> {
20+
return {
21+
act: (testee: Testee) => {
22+
return new Promise<Breakpoint>((resolve) => {
23+
function breakpointListener(message: string) {
24+
// check breakpoint hit message
25+
try {
26+
const breakpoint = breakpointHitParser(message);
27+
// on success: remove listener + resolve
28+
testee.testbed?.removeListener(TestbedEvents.OnMessage, breakpointListener);
29+
resolve(breakpoint);
30+
} catch (e) {
31+
32+
}
33+
}
434

5-
export function wait(time: number): Action<boolean> {
6-
return () => setTimeout(time).then(() => true)
35+
// await breakpoint hit
36+
testee.testbed?.on(TestbedEvents.OnMessage, breakpointListener)
37+
});
38+
}
39+
};
740
}

src/framework/scenario/Invoker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export class Invoker implements Step {
88
readonly instruction: Instruction;
99
readonly expected?: Expectation[];
1010

11-
constructor(func: string, args: Value[], result: number) {
12-
this.title = `ASSERT: ${func} ${args.map(val => val.value).join(' ')} ${result}`;
11+
constructor(func: string, args: Value[], result: Value) {
12+
this.title = `ASSERT: ${func} ${args.map(val => val.value).join(' ')} ${result.value}`;
1313
this.instruction = {kind: Kind.Request, value: Message.invoke(func, args)}
14-
this.expected = [{'value': {kind: 'primitive', value: result} as Expected<number>}];
14+
this.expected = [{'value': {kind: 'primitive', value: result.value} as Expected<number>}];
1515
}
1616
}

src/messaging/Parsers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function breakpointParser(text: string): Breakpoint {
4343
throw new Error('Could not messaging BREAKPOINT address in ack.');
4444
}
4545

46-
function breakpointHitParser(text: string): Breakpoint {
46+
export function breakpointHitParser(text: string): Breakpoint {
4747
const ack: Ack = ackParser(text, 'AT ');
4848

4949
let breakpointInfo = ack.text.match(/AT (0x.*)!/);

src/sourcemap/Wasm.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export namespace WASM {
1919
value: number;
2020
}
2121

22+
export function i32(n: number): WASM.Value {
23+
return {value: n, type: Type.i32};
24+
}
25+
2226
export interface Frame {
2327
type: number;
2428
fidx: string;

src/testbeds/Platform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Testbed, TesteeEvents} from './Testbed';
1+
import {Testbed, TestbedEvents} from './Testbed';
22
import {EventEmitter} from 'events';
33
import {Request} from '../messaging/Message';
44
import {MessageQueue} from '../messaging/MessageQueue';
@@ -49,7 +49,7 @@ export abstract class Platform extends EventEmitter implements Testbed {
4949
// messaging and resolve
5050
const [candidate, resolver] = this.requests[index];
5151
resolver(candidate.parser(message));
52-
this.emit(TesteeEvents.OnMessage, message);
52+
this.emit(TestbedEvents.OnMessage, message);
5353

5454
this.requests.splice(index, 1); // delete resolved request
5555
}

src/testbeds/Testbed.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {EventEmitter} from 'events';
33
import {Connection} from '../bridge/Connection';
44
import {SourceMap} from '../sourcemap/SourceMap';
55

6-
export enum TesteeEvents {
6+
export enum TestbedEvents {
77
OnMessage = 'message',
88
OnPushEvent = 'push'
99
}
@@ -17,7 +17,7 @@ export declare interface Testbed extends EventEmitter {
1717

1818
kill(): Promise<void>;
1919

20-
on(event: TesteeEvents.OnMessage, listener: (message: string) => void): this;
20+
on(event: TestbedEvents.OnMessage, listener: (message: string) => void): this;
2121

22-
on(event: TesteeEvents.OnPushEvent, listener: (data: string) => void): this;
22+
on(event: TestbedEvents.OnPushEvent, listener: (data: string) => void): this;
2323
}

test/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ framework.testee('emulator [:8500]', new EmulatorSpecification(8500));
1111
const steps: Step[] = [];
1212

1313
// ✔ ((invoke "8u_good1" (i32.const 0)) (i32.const 97))
14-
steps.push(new Invoker('8u_good1', [{value: 0, type: WASM.Type.i32}] as WASM.Value[], 97));
14+
steps.push(new Invoker('8u_good1', [{value: 0, type: WASM.Type.i32}] as WASM.Value[], WASM.i32(97)));
1515

1616
// ✔ ((invoke "8u_good3" (i32.const 0)) (i32.const 98))
17-
steps.push(new Invoker('8u_good3', [{value: 0, type: WASM.Type.i32}] as WASM.Value[], 98));
17+
steps.push(new Invoker('8u_good3', [{value: 0, type: WASM.Type.i32}] as WASM.Value[], WASM.i32(98)));
1818

1919
framework.test({
2020
title: `Test with address_0.wast`,

0 commit comments

Comments
 (0)