Skip to content

Commit

Permalink
Refactor test runners
Browse files Browse the repository at this point in the history
  • Loading branch information
x0k committed Jun 15, 2024
1 parent 4bee655 commit 98c757a
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 87 deletions.
3 changes: 2 additions & 1 deletion content/design-patterns/factory/src/js/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as code } from "./code.js?raw";
export * from './runner'

export { default as code } from "./code.js?raw";
export { default as Worker } from './worker?worker'
19 changes: 19 additions & 0 deletions content/design-patterns/factory/src/js/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createLogger } from "libs/logger";
import type { TestRunnerFactory } from "testing";
import { JsTestRunner } from 'testing-javascript'

import type { Input, Output } from "../tests-data";
import type { PaymentSystemType } from "../reference";

interface TestingModule {
payment(type: PaymentSystemType, base: number, amount: number): number;
}

export class TestRunner extends JsTestRunner<TestingModule, Input, Output> {
async executeTest(m: TestingModule, input: Input): Promise<Output> {
return m.payment(input.paymentSystem, input.base, input.amount);
}
}

export const factory: TestRunnerFactory<Input, Output> = async (_, { code, out }) =>
new TestRunner(createLogger(out), code);
19 changes: 1 addition & 18 deletions content/design-patterns/factory/src/js/worker.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
import { createLogger } from "libs/logger";
import type { TestRunnerFactory } from "testing";
import { startTestRunnerActor } from "testing/actor";
import { JsTestRunner } from 'testing-javascript'

import type { Input, Output } from "../tests-data";
import type { PaymentSystemType } from "../reference";

interface TestingModule {
payment(type: PaymentSystemType, base: number, amount: number): number;
}

class TestRunner extends JsTestRunner<TestingModule, Input, Output> {
async executeTest(m: TestingModule, input: Input): Promise<Output> {
return m.payment(input.paymentSystem, input.base, input.amount);
}
}

const factory: TestRunnerFactory<Input, Output> = async (_, { code, out }) =>
new TestRunner(createLogger(out), code);
import { factory } from './runner';

startTestRunnerActor(factory);
3 changes: 2 additions & 1 deletion content/design-patterns/factory/src/php/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as code } from './code.php?raw'
export * from './runner'

export { default as code } from './code.php?raw'
export { default as Worker } from './worker?worker'
33 changes: 33 additions & 0 deletions content/design-patterns/factory/src/php/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { TestRunnerFactory } from "testing";
import { FailSafePHP, PHPTestRunner, phpRuntimeFactory } from "testing-php";

import type { Input, Output } from "../tests-data";
import { PaymentSystemType } from "../reference";

const PHP_PAYMENT_SYSTEM_TYPES: Record<PaymentSystemType, string> = {
[PaymentSystemType.PayPal]: "PaymentSystemType::PAYPAL",
[PaymentSystemType.WebMoney]: "PaymentSystemType::WEBMONEY",
[PaymentSystemType.CatBank]: "PaymentSystemType::CAT_BANK",
};

export class TestRunner extends PHPTestRunner<Input, Output> {
protected override caseExecutionCode({
paymentSystem,
base,
amount,
}: Input): string {
return `strval(payment(${PHP_PAYMENT_SYSTEM_TYPES[paymentSystem]}, ${base}, ${amount}))`;
}
protected transformResult(result: string): Output {
const r = parseInt(result, 10);
if (isNaN(r)) {
throw new Error(`Invalid result type: ${result}, expected number`);
}
return r;
}
}

export const factory: TestRunnerFactory<Input, Output> = async (
_,
{ code, out }
) => new TestRunner(out, new FailSafePHP(phpRuntimeFactory), code);
33 changes: 1 addition & 32 deletions content/design-patterns/factory/src/php/worker.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,5 @@
import type { TestRunnerFactory } from "testing";
import { startTestRunnerActor } from "testing/actor";
import { FailSafePHP, PHPTestRunner, phpRuntimeFactory } from "testing-php";

import type { Input, Output } from "../tests-data";
import { PaymentSystemType } from "../reference";

const PHP_PAYMENT_SYSTEM_TYPES: Record<PaymentSystemType, string> = {
[PaymentSystemType.PayPal]: "PaymentSystemType::PAYPAL",
[PaymentSystemType.WebMoney]: "PaymentSystemType::WEBMONEY",
[PaymentSystemType.CatBank]: "PaymentSystemType::CAT_BANK",
};

class TestRunner extends PHPTestRunner<Input, Output> {
protected override caseExecutionCode({
paymentSystem,
base,
amount,
}: Input): string {
return `strval(payment(${PHP_PAYMENT_SYSTEM_TYPES[paymentSystem]}, ${base}, ${amount}))`;
}
protected transformResult(result: string): Output {
const r = parseInt(result, 10);
if (isNaN(r)) {
throw new Error(`Invalid result type: ${result}, expected number`);
}
return r;
}
}

const factory: TestRunnerFactory<Input, Output> = async (
_,
{ code, out }
) => new TestRunner(out, new FailSafePHP(phpRuntimeFactory), code);
import { factory } from './runner';

startTestRunnerActor(factory);
3 changes: 2 additions & 1 deletion content/design-patterns/factory/src/python/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as code } from "./code.py?raw"
export * from './runner'

export { default as code } from "./code.py?raw"
export { default as Worker } from "./worker?worker"
20 changes: 20 additions & 0 deletions content/design-patterns/factory/src/python/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createLogger } from "libs/logger";
import type { TestRunnerFactory } from "testing";
import { PyTestRunner, pyRuntimeFactory } from "testing-python";

import type { Input, Output } from "../tests-data";

export class TestRunner extends PyTestRunner<Input, Output> {
protected override caseExecutionCode({
paymentSystem,
base,
amount,
}: Input): string {
return `payment("${paymentSystem}", ${base}, ${amount})`;
}
}

export const factory: TestRunnerFactory<Input, Output> = async (
ctx,
{ code, out }
) => new TestRunner(await pyRuntimeFactory(ctx, createLogger(out)), code);
14 changes: 1 addition & 13 deletions content/design-patterns/factory/src/python/worker.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import { createLogger } from 'libs/logger';
import type { TestRunnerFactory } from "testing";
import { startTestRunnerActor } from "testing/actor";
import { PyTestRunner, pyRuntimeFactory } from "testing-python";

import type { Input, Output } from "../tests-data";

class TestRunner extends PyTestRunner<Input, Output> {
protected override caseExecutionCode({ paymentSystem, base, amount }: Input): string {
return `payment("${paymentSystem}", ${base}, ${amount})`;
}
}

const factory: TestRunnerFactory<Input, Output> = async (ctx, { code, out }) =>
new TestRunner(await pyRuntimeFactory(ctx, createLogger(out)), code);
import { factory } from "./runner";

startTestRunnerActor(factory);
3 changes: 2 additions & 1 deletion content/design-patterns/factory/src/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as code } from './code.ts?raw'
export * from './runner.js'

export { default as code } from './code.ts?raw'
export { default as Worker } from './worker?worker'
21 changes: 21 additions & 0 deletions content/design-patterns/factory/src/ts/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createLogger } from "libs/logger";
import type { TestRunnerConfig, TestRunnerFactory } from "testing";
import { TsTestRunner } from "testing-typescript";

import type { Input, Output } from "../tests-data";
import type { PaymentSystemType } from "../reference";

interface TestingModule {
payment(type: PaymentSystemType, base: number, amount: number): number;
}

export class TestRunner extends TsTestRunner<TestingModule, Input, Output> {
async executeTest(m: TestingModule, input: Input): Promise<Output> {
return m.payment(input.paymentSystem, input.base, input.amount);
}
}

export const factory: TestRunnerFactory<Input, Output> = async (
_,
{ code, out }: TestRunnerConfig
) => new TestRunner(createLogger(out), code);
21 changes: 1 addition & 20 deletions content/design-patterns/factory/src/ts/worker.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
import { createLogger } from "libs/logger";
import type { TestRunnerConfig, TestRunnerFactory } from "testing";
import { startTestRunnerActor } from "testing/actor";
import { TsTestRunner } from "testing-typescript";

import type { Input, Output } from "../tests-data";
import type { PaymentSystemType } from "../reference";

interface TestingModule {
payment(type: PaymentSystemType, base: number, amount: number): number;
}

class TestRunner extends TsTestRunner<TestingModule, Input, Output> {
async executeTest(m: TestingModule, input: Input): Promise<Output> {
return m.payment(input.paymentSystem, input.base, input.amount);
}
}

const factory: TestRunnerFactory<Input, Output> = async (
_,
{ code, out }: TestRunnerConfig
) => new TestRunner(createLogger(out), code);
import { factory } from './runner.js'

startTestRunnerActor(factory);

0 comments on commit 98c757a

Please sign in to comment.