简体   繁体   English

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

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

I have the following strings: 我有以下字符串:

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

I need to create a method that will Assert that any of the Expected strings is contained in the actual string, something like this: 我需要创建一个方法来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)
    {

    }
}

I tried the CollectionAssert.Contains method but it didn't work. 我尝试了CollectionAssert.Contains方法,但是没有用。 Is there a quick method I can use without iterating into the expected strings? 有没有可以快速使用的方法,而无需迭代expected字符串?

I think it's possible to use this "StringAssert.Contains(actualString,containsubstring);" 我认为可以使用此“ StringAssert.Contains(actualString,containsubstring);” in all Framework .NET 在所有Framework .NET中

It returns true if all the values of expected array is found in actual variable: 如果在实际变量中找到了期望数组的所有值,则返回true:

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

Return true even if just one value is contained in the actual string: 即使实际字符串中仅包含一个值,也返回true:

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

An extension method for the string class? 字符串类的扩展方法?

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

callable in this way: 可以这样调用:

    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

or also 或者也

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

of course put the extension method inside a static class 当然,将扩展方法放在静态类中
Tried also in Visual Studio 2010 Console Application 在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));
        }
    }
}

EDIT: 编辑:

The problem with different case could be resolved changing the extension in this way 通过更改扩展名可以解决不同情况下的问题

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

but, to prevent false positives when one of expected strings is embedded inside the actual string you need to add a space at the end of the actual string 但是,为防止在预期字符串之一嵌入实际字符串时出现误报,您需要在实际字符串的末尾添加一个空格

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

if you did 如果你做了

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

but I think you would still need to iterate the expected's 但我认为您仍然需要迭代预期的

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

EDIT as per Gene S comment 根据Gene S注释进行编辑

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

use the sugar if you want to, but there is no processor savings, it just makes it harder to read. 如果需要,可以使用糖,但不会节省处理器,只会增加阅读难度。

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

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