gpt4 book ai didi

c# - 如何在需要括号的 NUnit 中编写流畅的约束

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:22 26 4
gpt4 key购买 nike

我最近开始使用 NUnit 的约束功能并遇到了以下问题。在执行顺序很重要且在普通 C# 编程中用括号解决的情况下,如何使用流畅的表达式语法编写约束?

在下面的示例中,我定义了两个单独的断言:

  1. 字符串应以 1 或 2 开头,在所有情况下字符串都应以 5 结尾
  2. 字符串应以 1 或 2 开头,如果字符串以 2 开头,则应以 5 结尾

要断言这一点,我可以考虑三种方式;经典的、流畅的约束和使用复合约束的约束。所以这会导致 6 个测试和一些测试用例。

private class SourceForParenthesisTest : IEnumerable
{
public IEnumerator GetEnumerator()
{
yield return new TestCaseData("2").Throws(typeof(AssertionException));
yield return new TestCaseData("3").Throws(typeof(AssertionException));
yield return new TestCaseData("15");
yield return new TestCaseData("25");
yield return new TestCaseData("35").Throws(typeof(AssertionException));
}
}

[TestCase("1", ExpectedException = typeof(AssertionException))]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void WithParenthesisClassic(string i)
{
var res = (i.StartsWith("1") || i.StartsWith("2")) && i.EndsWith("5");
Assert.True(res);
}

[TestCase("1", ExpectedException = typeof(AssertionException))]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void WithParenthesisOperatorConstraint(string i)
{
Assert.That(i, (Is.StringStarting("1") | Is.StringStarting("2")) & Is.StringEnding("5"));
}

[TestCase("1", ExpectedException = typeof(AssertionException), Ignore = true, IgnoreReason = "Not clear how to write this fluent expression")]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void WithParenthesisConstraint(string i)
{
Assert.That(i, Is.StringStarting("1").Or.StringStarting("2").And.StringEnding("5"));
}

[TestCase("1")]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void NoParenthesisClassic(string i)
{
var res = i.StartsWith("1") || i.StartsWith("2") && i.EndsWith("5");
Assert.True(res);
}

[TestCase("1")]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void NoParenthesisOperatorConstraint(string i)
{
Assert.That(i, Is.StringStarting("1") | Is.StringStarting("2") & Is.StringEnding("5"));
}

[TestCase("1")]
[TestCaseSource(typeof(SourceForParenthesisTest))]
public void NoParenthesisConstraint(string i)
{
Assert.That(i, Is.StringStarting("1").Or.StringStarting("2").And.StringEnding("5"));
}

实际问题出在 WithParenthesisConstraint(上面列出的断言 1)中,我想不出一种正确编写约束的方法,这导致我设置为忽略的一个失败测试用例。

如何编写此断言才能按预期工作?

最佳答案

我也看不到一个明显的方法来做到这一点。我的第一个想法是您需要将表达式解析到给定点,这使您的断言看起来像这样:

Assert.That(i, ((IResolveConstraint)Is.StringStarting("1").Or.StringStarting("2"))
.Resolve().With.StringEnding("5"))

这显然有点乱。您可以添加自己的扩展方法,使其更令人愉快,使用类似这样的方法(您显然可以将扩展方法重命名为您认为合适的任何名称):

static class MyExtensions {
public static Constraint Evaluate(this Constraint exp) {
return ((IResolveConstraint)exp).Resolve();
}
}

这将使您的测试代码断言如下:

Assert.That(i, (Is.StringStarting("1").Or.StringStarting("2")).Evaluate()
.And.StringEnding("5"));

哪个更好一些但并不理想,因此可能不是您要找的。

可能还值得考虑结合 EvaluateAnd约束以使其更易于阅读。也许像这样的扩展方法:

public static ConstraintExpression OnlyIf(this Constraint exp) {
return ((IResolveConstraint)exp).Resolve().And;
}

给出这样的测试代码:

Assert.That(i, (Is.StringStarting("1").Or.StringStarting("2")).OnlyIf()
.StringEnding("5"));

关于c# - 如何在需要括号的 NUnit 中编写流畅的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30398851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com