From 18a62ae2d867fa83bde6f437abc6d22e2f854e91 Mon Sep 17 00:00:00 2001 From: Alberto Date: Fri, 31 Jan 2020 23:25:24 +0100 Subject: [PATCH] Accepting generic type for table definition (#42011) --- types/cucumber/cucumber-tests.ts | 33 ++++++++++++++++++++++++++++++-- types/cucumber/index.d.ts | 7 ++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/types/cucumber/cucumber-tests.ts b/types/cucumber/cucumber-tests.ts index 6ba580b108..da431ba776 100644 --- a/types/cucumber/cucumber-tests.ts +++ b/types/cucumber/cucumber-tests.ts @@ -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) => { + 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(), diff --git a/types/cucumber/index.d.ts b/types/cucumber/index.d.ts index 2d2264223d..e47fae12a7 100644 --- a/types/cucumber/index.d.ts +++ b/types/cucumber/index.d.ts @@ -8,6 +8,7 @@ // Peter Morlion // Don Jayamanne // David Goss +// Alberto Silva // 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 { /** 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;