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

时间:2012-06-13 11:54:23

标签: c# visual-studio-2010 mstest

我有以下字符串:

       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字符串?

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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

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

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

答案 2 :(得分:1)

字符串类的扩展方法?

    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

答案 3 :(得分:1)

如果你做了

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))
如果你愿意,可以使用糖,但没有处理器节省,只是让它更难阅读。