将选定的 dataTable 行转换为 Cucumber 中的 Integer
Convert selected dataTable rows to Integer in Cucumber
sample.feature
Scenario Outline: My test case
Given: My data is loaded
When : My data is ready
Then: My data is
|name| value |
|a | <value1> |
|b | <value2> |
|c | <value3> |
|d | <value4> |
Examples:
|value1| value2|value3| value4 |
|1 | 2 | 3 | text |
steps.js //使用node.js
Then('My data is', function (dataTable) {
console.log(dataTable);
// console output
[['a', '1'],
['b', '2'],
['c', '3'],
['d', 'text']]
});
我需要将字符串值输出为字符串,将整数输出为整数
// expected output
[['a', 1],
['b', 2],
['c', 3],
['d', 'text']]
我不想根据名称进行硬编码,因为它可能会改变。是否有可用的通用解决方案?
这将是一个特定于语言的问题。这可以通过使用对象映射器来解决。在 javascript 我认为有一个 #map
功能(但我不是 100%)。所以你会想要类似的东西。
虽然我的 JS 真的很生疏
编辑:更好的解决方案是:
const ans = array2.map((array, index) => {
return array.map((element, innerIndex) => {
retVal = innerIndex%2==0 ? element + 1 : element * 2
return retVal
})
})
这将获取内部数组的每个元素,如果它是位置 0,2,4 e.t.c 处的值,则将其加 1。如果位置为 1,3,5 e.t.c.
,则乘以 2
您可以根据自己的需要进行修改。
您可以使用 isNaN(x)
检测一个值是否为数字。如果 isNaN(x)
函数 returns false
,您可以安全地忽略黄瓜数据 table 中的那个值。如果isNaN(x)
函数returns true
那么可以使用Number(x)
函数将字符串转换为数字。
isNaN
和 Number
功能是免费的。对于任何给定的输入,如果 isNaN
returns true
,则将相同的输入传递给 Number
将 return 一个 NaN
值。如果 isNaN
returns false
然后将该输入传递给 Number
将成功地将其转换为数字。
请记住,零是 JavaScript 中的假值,因此 if (Number("0"))
将计算为 false
。
function parseDataTable(dataTable) {
return dataTable.map((row, index) => {
if (!isNaN(row[1])) {
row[1] = Number(row[1]);
}
return row;
});
}
并在您的步骤定义中使用它:
Then('My data is', function (dataTable) {
dataTable = parseDataTable(dataTable);
console.log(dataTable);
/*
Should output:
[
["a", 1],
["b", 2],
["c", 3],
["d", "text"]
]
*/
});
sample.feature
Scenario Outline: My test case
Given: My data is loaded
When : My data is ready
Then: My data is
|name| value |
|a | <value1> |
|b | <value2> |
|c | <value3> |
|d | <value4> |
Examples:
|value1| value2|value3| value4 |
|1 | 2 | 3 | text |
steps.js //使用node.js
Then('My data is', function (dataTable) {
console.log(dataTable);
// console output
[['a', '1'],
['b', '2'],
['c', '3'],
['d', 'text']]
});
我需要将字符串值输出为字符串,将整数输出为整数
// expected output
[['a', 1],
['b', 2],
['c', 3],
['d', 'text']]
我不想根据名称进行硬编码,因为它可能会改变。是否有可用的通用解决方案?
这将是一个特定于语言的问题。这可以通过使用对象映射器来解决。在 javascript 我认为有一个 #map
功能(但我不是 100%)。所以你会想要类似的东西。
虽然我的 JS 真的很生疏
编辑:更好的解决方案是:
const ans = array2.map((array, index) => {
return array.map((element, innerIndex) => {
retVal = innerIndex%2==0 ? element + 1 : element * 2
return retVal
})
})
这将获取内部数组的每个元素,如果它是位置 0,2,4 e.t.c 处的值,则将其加 1。如果位置为 1,3,5 e.t.c.
,则乘以 2您可以根据自己的需要进行修改。
您可以使用 isNaN(x)
检测一个值是否为数字。如果 isNaN(x)
函数 returns false
,您可以安全地忽略黄瓜数据 table 中的那个值。如果isNaN(x)
函数returns true
那么可以使用Number(x)
函数将字符串转换为数字。
isNaN
和 Number
功能是免费的。对于任何给定的输入,如果 isNaN
returns true
,则将相同的输入传递给 Number
将 return 一个 NaN
值。如果 isNaN
returns false
然后将该输入传递给 Number
将成功地将其转换为数字。
请记住,零是 JavaScript 中的假值,因此 if (Number("0"))
将计算为 false
。
function parseDataTable(dataTable) {
return dataTable.map((row, index) => {
if (!isNaN(row[1])) {
row[1] = Number(row[1]);
}
return row;
});
}
并在您的步骤定义中使用它:
Then('My data is', function (dataTable) {
dataTable = parseDataTable(dataTable);
console.log(dataTable);
/*
Should output:
[
["a", 1],
["b", 2],
["c", 3],
["d", "text"]
]
*/
});