繁体   English   中英

如何断言实际字符串中是否包含任何字符串列表

[英]How to assert if any string in a list of strings is contained in the Actual string

我有以下字符串:

       Actual       |   Expected
"The Actual String" | "The"
                    | "Actual"
                    | "String"
                    | "Other string"
                    |    ...

我需要创建一个方法来Assert实际字符串中包含任何Expected的字符串,如下所示:

[TestClass]
public class UnitTest
{
    [TestMethod]
    public void TestMethod()
    {
        //Assertion Passed
        AssertContainsString("The Actual String", "The"); 

        //Assertion Passed
        AssertContainsString("The Actual String", "Something", "Actual"); 

        //Assertion Failed
        AssertContainsString("The Actual String", "Something", "Something Else"); 
    }

    public void AssertContainsString(string actual, params string[] expected)
    {

    }
}

我尝试了CollectionAssert.Contains方法,但是没有用。 有没有可以快速使用的方法,而无需迭代expected字符串?

我认为可以使用此“ StringAssert.Contains(actualString,containsubstring);” 在所有Framework .NET中

如果在实际变量中找到了期望数组的所有值,则返回true:

bool foundall = expected.Except(actual.Split(' ')).Count()==0;

即使实际字符串中仅包含一个值,也返回true:

bool ans = expected.Except(actual.Split(' ')).Count() != expected.Length;

字符串类的扩展方法?

    public static bool AnyIn(this string s, params string[] values)
    {
        return values.Any(x => s.Contains(x));
    }

可以这样调用:

    string test = "The actual string";
    if(test.AnyIn("The") == true)   // success
    ...
    if(test.AnyIn("The", "actual", "string") == true)   // success
    ...
    if(test.AnyIn("The", "actual", "value") == true)   // success
    ...
    if(test.AnyIn("some", "value") == true)   // fail

或者也

    System.Diagnostics.Debug.Assert(test.AnyIn("some", "value"), "No expected string found"); // fail

当然,将扩展方法放在静态类中
在Visual Studio 2010控制台应用程序中也尝试过

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main()
        {
            string test = "The actual string";

            // Prints False
            bool result = test.AnyIn("other", "value");
            Console.WriteLine(result.ToString()); 

            // Prints True
            result = test.AnyIn("other", "The");
            Console.WriteLine(result.ToString()); 

            //  No Assert dialog here 
            System.Diagnostics.Debug.Assert(test.AnyIn("other", "The"), "No expected values found");

            //  Big Assert dialog here with message "No expected values found"
            System.Diagnostics.Debug.Assert(test.AnyIn("other", "The"), "No expected values found");

        }

    }

    static class ext
    {
        public static bool AnyIn(this string s, params string[] values)
        {
            return values.Any(x => s.Contains(x));
        }
    }
}

编辑:

通过更改扩展名可以解决不同情况下的问题

public static bool AllIn(this string s, params string[] values)     
{         
     return values.Any(x => s.IndexOf(x + " ", StringComparison.CurrentCultureIgnoreCase) >= 0);     
}

但是,为防止在预期字符串之一嵌入实际字符串时出现误报,您需要在实际字符串的末尾添加一个空格

string test = "The actual string ";  // notice the extra space added at the end

如果你做了

if (actual.Split(' ').Contains(expected)) return true;

但我认为您仍然需要迭代预期的

foreach (string ex in expected)
{
    if (actual.Split(' ').Contains(ex)) return true;
}

根据Gene S注释进行编辑

expected.Any(ex => actual.Split(' ').Contains(ex))

如果需要,可以使用糖,但不会节省处理器,只会增加阅读难度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM