Accepting generic type for table definition (#42011)

This commit is contained in:
Alberto
2020-01-31 14:25:24 -08:00
committed by GitHub
parent de29d6ccb3
commit 18a62ae2d8
2 changed files with 35 additions and 5 deletions
+31 -2
View File
@@ -1,9 +1,20 @@
import * as assert from "power-assert";
import { setDefinitionFunctionWrapper, setWorldConstructor, defineParameterType, After, AfterAll, Before, BeforeAll, Given, When, Then } from "cucumber";
import {
setDefinitionFunctionWrapper,
setWorldConstructor,
defineParameterType,
After,
AfterAll,
Before,
BeforeAll,
Given,
When,
Then,
TableDefinition as Table
} from "cucumber";
import cucumber = require("cucumber");
type Callback = cucumber.CallbackStepDefinition;
type Table = cucumber.TableDefinition;
type HookScenarioResult = cucumber.HookScenarioResult;
const Status = cucumber.Status;
@@ -159,6 +170,24 @@ function StepSampleWithoutDefineSupportCode() {
assert.deepEqual(actual, expected);
});
interface MyTableType {
Vegetable: string;
Rating: string;
}
Given(/^a table step with a Type$/, (table: Table<MyTableType>) => {
const expected = [
{ Vegetable: 'Apricot', Rating: '5' },
{ Vegetable: 'Brocolli', Rating: '2' },
{ Vegetable: 'Cucumber', Rating: '10' }
];
const [actual] = table.hashes();
assert.deepEqual(actual.Vegetable, 'Apricot');
assert.deepEqual(actual.Rating, '5');
assert.deepEqual(actual.WrongField, '5'); // $ExpectError
});
defineParameterType({
regexp: /particular/,
transformer: s => s.toUpperCase(),
+4 -3
View File
@@ -8,6 +8,7 @@
// Peter Morlion <https://github.com/petermorlion>
// Don Jayamanne <https://github.com/DonJayamanne>
// David Goss <https://github.com/davidjgoss>
// Alberto Silva <https://github.com/albertossilva>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
@@ -29,7 +30,7 @@ export interface CallbackStepDefinition {
(error?: any, pending?: string): void;
}
export interface TableDefinition {
export interface TableDefinition<Type = any> {
/** Returns the table as a 2-D array. */
raw(): string[][];
@@ -37,10 +38,10 @@ export interface TableDefinition {
rows(): string[][];
/** Returns an object where each row corresponds to an entry (first column is the key, second column is the value). */
rowsHash(): { [firstCol: string]: string };
rowsHash(): { [firstColumn: string]: string };
/** Returns an array of objects where each row is converted to an object (column header is the key). */
hashes(): Array<{ [colName: string]: string }>;
hashes(): Array<{ [columnName in keyof Type]: string }>;
}
export type StepDefinitionCode = (this: World, ...stepArgs: any[]) => any;