木瓜数据表
安装
npm i @cucumber-stories/cucumber-datatable
这是什么?
Cucumber DataTable是一个用于简化并增强gherkins特征文件中的数据表格的库. 通过为您的数据表提供计划,您可以很容易地从愚蠢的字符串传递到结构化和输入的数据.
样本比100个字好:
想象一下你想处理这样一个格金表:
Feature: Heroes sample
Scenario: A simple heroes table
Given the following heroes
| Name | Age |
| Bruce Wayne | 83 |
| Tony Stark | 52 |
| Bruce Banner | 53 |
感谢 @cucumber-stories/cucumber-datatable 写这个...
import { DataTable } from "@cucumber/cucumber";
import { cucumberDataTable } from "@cucumber-stories/cucumber-datatable";
const getHeroes = cucumberDataTable({
name: { columnName: "Name", converter: Converters.String },
age: { columnName: "Age", converter: Converters.Number },
});
Given(/^The following heroes$/, function (dataTable: DataTable) {
const heroes = getHeroes(dataTable);
console.log(heroes); // [{ name: 'Bruce Wayne', age: 83 }, ...]
});
不是那样的
import { DataTable } from "@cucumber/cucumber";
Given(/^The following heroes$/, function (table: DataTable) {
const lines = data.raw();
// Here you have an hugly array of string arrays (string[][]) ☹️
});
如何用取来?
首先,你必须定义一个由两件事组成的词典. 它的密钥与输出对象相同,并附加以下元数据:
columnName: gherkin 文件中的列名 (.feature(中文(简体) )converter: 用于从gherkin数据表中转换数据的转换器
你必须使用 cucumberDataTable() 函数创建用于您步骤中的可重用函数以转换您的
数据.
import { cucumberDataTable } from "@cucumber-stories/cucumber-datatable";
const dictionary = {
name: {
columnName: "Name", // Column name in Gherkin file
converter: Converters.String, // Converter to use (more samples below)
},
age: {
columnName: "Age",
converter: Converters.Number,
},
};
const getStructuredData = cucumberDataTable(dictionary);
// ....
Given(/^The following table/, function (dataTable: DataTable) {
const data = getStructuredData(dataTable);
});
转换器
转换器用于转换数据. 在框外,图书馆提供一些有用的转换器.
一些转换器可以配置,你可以有细节 转换器配置 节.
| 名称 | 在盖尔金 | 产出 | 默认配置 |
|---|---|---|---|
Converters.String | "Foo" | "Foo" | |
Converters.Number | "42" | 42 | |
Converters.YesNoToBoolean | "yes" 或者说 "no" | true 或者说 false | { yes: "yes"; no: "no" } |
Converters.StringArray | "Foo, Bar" | ["Foo", "Bar"] | { separator: "," } |
Converters.ObjectArray | "Foo:42, Bar:32" | 见下文 | { propertySeparator: ":", itemSeparator: "," } |
Converters.Nullable | "<null>" | 见下文 | { nullValue: "<null>" } |
Converters.Custom | 您的特殊格式化逻辑 | 见下文 |
转换器配置
如果转换器需要配置,可以拨打 .withConfig 带有指定配置的方法,例如:
const dictionary = {
tags: {
columnName: "Tags",
converter: Converters.StringArray.withConfig({
separator: "-",
}),
},
};
无效
如果您的一些值需要无效, 并想要用作 null 在代码中,您可以使用 Nullable
转换器.
const dictionary = {
name: {
columnName: "Name",
converter: Converters.Nullable(Converters.String),
},
};
// name will be string | null
您也可以配置无效值, 默认为 <null>
const dictionary = {
name: {
columnName: "Name",
converter: Converters.Nullable(Converters.String, {
nullValue: "vide",
}),
},
};
// name will be string | null, when the value is "vide"
对象数组转换器
这个 Converters.ObjectArray() 有点棘手,所以它值得它自己 在我们的文件.
设想以下的 gherkin 文件 :
Feature: Heroes sample
Scenario:
Given the following attributes
| Attributes | Other |
| 1:Color,2:Size | other |
| 10:Name | other |
如果您想要这样的输出 :
[
{
attributes: [
{ code: 1, name: "Color" },
{ code: 2, name: "Size" },
],
other: "other",
},
{
attributes: [{ code: 10, name: "Name" }],
other: "other",
},
];
你也许需要 Converters.ObjectArray()。 。 。 .
要使用此转换器, 您必须定义 :
- 不同项目的分隔符
- 同一对象不同属性的分隔符
然后是筑巢物体的图谋:
import {
cucumberDataTable,
Converters,
} from "@cucumber-stories/cucumber-datatable";
const dictionary = {
attributes: {
columnName: "Attributes",
converter: Converters.ObjectArray({
code: {
position: 0,
converter: Converters.Number,
},
name: {
position: 1,
converter: Converters.String,
},
}).withConfig({
propertySeparator: ":",
itemSeparator: ",",
}),
},
// Other columns...
other: {
columnName: "Other",
converter: Converters.String,
},
};
const getStructuredData = cucumberDataTable(dictionary);
你不必说明
.withConfig如果您想要保留默认配置 实值{ propertySeparator: ":", itemSeparator: "," }
自定义转换器
简而言之,转换器就是 Converter 课。 要创建自定义转换器以满足您的需要, 您需要
用于 Converter.Custom(<Your converter function>)。 。 。 .
转换器函数为:
- 参数中的字符串(来自gherkin文件的数据),
- 作为第二个参数的可选配置,
- 并返回你想要的东西.
例如,如果想要从一列输出自定义结构,您可以这样做:
import {
cucumberDataTable,
Converters,
} from "@cucumber-stories/cucumber-datatable";
const dictionary = {
code: {
columnName: "Code",
converter: Converters.Custom((code: string) => ({ nested: code })),
},
configurableCode: {
columnName: "Configurable code",
converter: Converters.Custom(
(code: string, config?: { append: string }) => code + config?.append
).withConfig({
append: "-code",
}),
},
// Other columns...
other: {
columnName: "Other",
converter: Converters.String,
},
};
const getStructuredData = cucumberDataTable(dictionary);
它将改变这个表格:
Feature: Heroes sample
Scenario: A simple heroes table
Given the following table
| Code | Configurable code | Other |
| 001 | 001 | other |
| 002 | 001 | other |
输入 :
[
{ code: { nested: "001" }, configurableCode: "001-code", other: "other" },
{ code: { nested: "002" }, configurableCode: "002-code", other: "other" },
];
发展
正在测试
运行 npm test 财务报告和审定财务报表 npm run test-types
发布新版本
npm version <major|minor|patch>git push --tags- 在此标签上创建发布 .