我正在寻找一种具有步骤定义的方法,例如:
给定数字1,2,3,4的集合
并使用int [],List或IEnumerable将其映射到步骤定义
正则表达式(\ d +(,\ d +)*)匹配,但是意味着我需要两个参数。
目前我有
[Given(@"a collection of numbers (\d+(,\d+)*)")]
public void givencollectionofnumbers(string p0, string p1)
{
//p0 is "1,2,3,4"
//p1 is ",4"
}
我有一个简单的工作原理是
[Given(@"a collection of numbers (.*)")]
public void givencollectionofnumbers(string p0)
{
var numbers = p0.Split(',').Select(x => int.Parse(x));
}
但是我想以一种更优雅的方式做到这一点,有可能将数字的类型更改为双精度,并确保正则表达式仅匹配数字列表。
我也不想为此使用表,因为对于简单的数据列表来说似乎过多了
有人能帮忙吗
最佳答案
我只是在我的项目上解决了相同的问题:这可以解决问题
((?:.,\d+)*(?:.\d+))
如果您还想接受单个int,请改用以下方法:
((?:.,\d+)*(?:.+))
您的主张有两个问题:
当您只需要1时,Specflow尝试将其匹配为2个参数,但是我无法在文档中找到为什么它这样做的清晰解释。
您肯定需要一个StepArgumentTransformation来以任何可枚举的形式转换输入字符串
因此,您的最后一步功能将如下所示:
[Given(@"foo ((?:.,\d+)*(?:.+))")]
public void Foo(IEnumerable<int> ints)
{
}
[StepArgumentTransformation(@"((?:.,\d+)*(?:.+))")]
public static IEnumerable<int> ListIntTransform(string ints)
{
return ints.Split(new[] { ',' }).Select(int.Parse);
}
并且您在Foo函数中收到一个Enumerable int。
希望能帮助到你。