我如何重写此foreach使其成为以前的Linq表达式的一部分?

马格努萨林内尔

我装修我的MSTest的测试与自定义属性,SpecificationAttribute并且VerificationAttribute这是我后来用它来生成html测试规范报告。

属性如下所示:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class SpecificationAttribute : Attribute
{
    public SpecificationAttribute(int step, string text)
    {
        Step = step;
        Text = text;
    }

    public int Step { get; set; }

    public string Text { get; set; }
}

我编写了一个测试,该测试断言测试方法在使用属性时必须具有唯一的步骤。例如,以下用法未能通过我的测试,因为它应该:

[Specification(1, "Click Button)]
[Verification(1, "Verify popup")]
[Specification(1, "Click close")]
[Verification(2, "Verify popup disappears")]

我的测试看起来像这样:

[TestMethod]
public void TestForNoDuplicateStepsOnMethod()
{
    var specificationMethods = Assembly.GetExecutingAssembly().GetTypes()
        .Where(type =>
            type.GetCustomAttribute<CompilerGeneratedAttribute>() == null &&
                type.GetCustomAttribute<TestClassAttribute>() != null)
        .SelectMany(type => type.GetMethods())
        .Where(methodInfo =>
            methodInfo.GetCustomAttribute<TestMethodAttribute>() != null &&
            methodInfo.GetCustomAttributes<SpecificationAttribute>().Any())
        .ToArray();

    foreach (var method in specificationMethods)
    {
        var specificationAttributes = method.GetCustomAttributes<SpecificationAttribute>();

        var duplicates = specificationAttributes.GroupBy(s => s.Step).Where(grp => grp.Count() > 1).SelectMany(x => x).ToList();

        if (duplicates.Any())
        {
            var initialMessage = $@"{Environment.NewLine}{method.DeclaringType}.{method.Name} contains several SpecificationAttribute with the same step number." + Environment.NewLine + Environment.NewLine;

            var message = duplicates.Aggregate(initialMessage, (s, attribute) => s + attribute.Step + " " + attribute.Text + Environment.NewLine);

            Assert.Fail(message);
        }
    }
}

我想重写最后一部分,使其成为Linq查询的一部分。这可能吗?另外,我当前的测试仅输出一种失败的方法。如果Linq查询可以包括在唯一步骤号上具有失败条件的所有方法,那将是可取的。

谢尔盖·L

(编辑以通过积极的测试解决该问题)尝试此操作,但是恕我直言,它很难阅读。

[TestMethod]
public void TestForNoDuplicateStepsOnMethod()
{
    var errors = Assembly.GetExecutingAssembly().GetTypes()
        .Where(type =>
            type.GetCustomAttribute<CompilerGeneratedAttribute>() == null &&
                type.GetCustomAttribute<TestClassAttribute>() != null)
        .SelectMany(type => type.GetMethods())
        .Where(methodInfo =>
            methodInfo.GetCustomAttribute<TestMethodAttribute>() != null &&
            methodInfo.GetCustomAttributes<SpecificationAttribute>().Any())
        .Select(method =>
            method.GetCustomAttributes<SpecificationAttribute>()
        .GroupBy(s => s.Step)
            .Where(grp => grp.Count() > 1)
            .Select(x => x.Aggregate(
                $@"{Environment.NewLine}{method.DeclaringType}.{method.Name} contains several SpecificationAttribute with the same step number." 
                + Environment.NewLine,
                (s, attribute) => s + attribute.Step + " " + attribute.Text + Environment.NewLine))
                .Aggregate("", (s, message) => s + message))
            .Aggregate("", (s, message) => s + message);
        Assert.AreEqual("", errors);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我需要帮助制作此正则表达式以匹配 url 的一部分

联合和相交可以成为正则表达式的一部分吗?

如何捕获String表达式并仅替换其中一部分?

如何使用正则表达式跳过String的一部分?

如何使用正则表达式删除文本的这一部分?

如何为表达式的一部分加上别名以进行查找?

如何仅替换以下正则表达式(JavaScript)的一部分?

如何使用grep排除正则表达式的一部分

如何使正则表达式的一部分可选?

如何在正则表达式中否定类的一部分?

如何使用 C 中的宏计算表达式的一部分?

评估Lambda表达式作为表达式树的一部分

如何使用Span使其成为TextView Bold直到特殊字符的一部分?

如何线性化 JSON 并使其成为初始数组的一部分

如何传递String参数,使其成为类方法调用的一部分?

Android中ListView项内的按钮-如何使其成为onItemClickListener的一部分

我如何获得if表达式的哪一部分为真?

为什么此正则表达式返回0作为整数的一部分?

为什么我无法在python中匹配我的正则表达式的最后一部分?

如何用相应方程式的左侧部分替换表达式的一部分?

我的左联接查询没有将指定的表达式作为聚合函数的一部分

我可以在text.replace函数的第二部分中使用我的regexp表达式的一部分吗?

如何使#if #endif成为宏的一部分

如何在我的正则表达式中不包含文本字符串的第一部分?

如何使用正则表达式从 URL 中获取最后一部分?

如何使用正则表达式在Java中处理字符串的一部分

如何在 bash shell 中使用查找结果作为正则表达式的一部分

如何使用正则表达式移动字符串的一部分

如何获取与SQL Server中的正则表达式匹配的字符串的一部分