Skip to content

Commit

Permalink
Improve Action interface + add testee param to act
Browse files Browse the repository at this point in the history
  • Loading branch information
tolauwae committed Jan 25, 2024
1 parent e80c3a6 commit d16c831
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 19 deletions.
6 changes: 1 addition & 5 deletions src/framework/Testee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ export function getValue(object: any, field: string): any {
return object;
}

function act<T>(action: Action<T>): Promise<T> {
return action();
}

export class Testee { // TODO unified with testbed interface

/** The current state for each described test */
Expand Down Expand Up @@ -153,7 +149,7 @@ export class Testee { // TODO unified with testbed interface
let actual: Object | void;
if (step.instruction.kind === Kind.Action) {
actual = await timeout<Object | void>(`performing action . ${step.title}`, testee.timeout,
act(step.instruction.value));
step.instruction.value.act(testee));
} else {
actual = await timeout<Object | void>(`sending instruction ${step.instruction.value.type}`, testee.timeout,
testee.testbed.sendRequest(map, step.instruction.value));
Expand Down
39 changes: 36 additions & 3 deletions src/framework/scenario/Actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
import {setTimeout} from 'timers/promises';
import {Testee} from '../Testee';
import {TestbedEvents} from '../../testbeds/Testbed';
import {Breakpoint} from '../../debug/Breakpoint';
import {breakpointHitParser} from '../../messaging/Parsers';

export type Action<T> = () => Promise<T>;
export interface Action<T> {
act: (testee: Testee) => Promise<T>;
}

export interface PureAction<T> extends Action<T> {
act: () => Promise<T>;
}

export function wait(time: number): PureAction<boolean> {
return {act: () => setTimeout(time).then(() => true)}
}

export function awaitBreakpoint(): Action<Breakpoint> {
return {
act: (testee: Testee) => {
return new Promise<Breakpoint>((resolve) => {
function breakpointListener(message: string) {
// check breakpoint hit message
try {
const breakpoint = breakpointHitParser(message);
// on success: remove listener + resolve
testee.testbed?.removeListener(TestbedEvents.OnMessage, breakpointListener);
resolve(breakpoint);
} catch (e) {

}
}

export function wait(time: number): Action<boolean> {
return () => setTimeout(time).then(() => true)
// await breakpoint hit
testee.testbed?.on(TestbedEvents.OnMessage, breakpointListener)
});
}
};
}
6 changes: 3 additions & 3 deletions src/framework/scenario/Invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export class Invoker implements Step {
readonly instruction: Instruction;
readonly expected?: Expectation[];

constructor(func: string, args: Value[], result: number) {
this.title = `ASSERT: ${func} ${args.map(val => val.value).join(' ')} ${result}`;
constructor(func: string, args: Value[], result: Value) {
this.title = `ASSERT: ${func} ${args.map(val => val.value).join(' ')} ${result.value}`;
this.instruction = {kind: Kind.Request, value: Message.invoke(func, args)}
this.expected = [{'value': {kind: 'primitive', value: result} as Expected<number>}];
this.expected = [{'value': {kind: 'primitive', value: result.value} as Expected<number>}];
}
}
2 changes: 1 addition & 1 deletion src/messaging/Parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function breakpointParser(text: string): Breakpoint {
throw new Error('Could not messaging BREAKPOINT address in ack.');
}

function breakpointHitParser(text: string): Breakpoint {
export function breakpointHitParser(text: string): Breakpoint {
const ack: Ack = ackParser(text, 'AT ');

let breakpointInfo = ack.text.match(/AT (0x.*)!/);
Expand Down
4 changes: 4 additions & 0 deletions src/sourcemap/Wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export namespace WASM {
value: number;
}

export function i32(n: number): WASM.Value {
return {value: n, type: Type.i32};
}

export interface Frame {
type: number;
fidx: string;
Expand Down
4 changes: 2 additions & 2 deletions src/testbeds/Platform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Testbed, TesteeEvents} from './Testbed';
import {Testbed, TestbedEvents} from './Testbed';
import {EventEmitter} from 'events';
import {Request} from '../messaging/Message';
import {MessageQueue} from '../messaging/MessageQueue';
Expand Down Expand Up @@ -49,7 +49,7 @@ export abstract class Platform extends EventEmitter implements Testbed {
// messaging and resolve
const [candidate, resolver] = this.requests[index];
resolver(candidate.parser(message));
this.emit(TesteeEvents.OnMessage, message);
this.emit(TestbedEvents.OnMessage, message);

this.requests.splice(index, 1); // delete resolved request
}
Expand Down
6 changes: 3 additions & 3 deletions src/testbeds/Testbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {EventEmitter} from 'events';
import {Connection} from '../bridge/Connection';
import {SourceMap} from '../sourcemap/SourceMap';

export enum TesteeEvents {
export enum TestbedEvents {
OnMessage = 'message',
OnPushEvent = 'push'
}
Expand All @@ -17,7 +17,7 @@ export declare interface Testbed extends EventEmitter {

kill(): Promise<void>;

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

on(event: TesteeEvents.OnPushEvent, listener: (data: string) => void): this;
on(event: TestbedEvents.OnPushEvent, listener: (data: string) => void): this;
}
4 changes: 2 additions & 2 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ framework.testee('emulator [:8500]', new EmulatorSpecification(8500));
const steps: Step[] = [];

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

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

framework.test({
title: `Test with address_0.wast`,
Expand Down

0 comments on commit d16c831

Please sign in to comment.