简体   繁体   中英

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:

[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. Is there a quick method I can use without iterating into the expected strings?

I think it's possible to use this "StringAssert.Contains(actualString,containsubstring);" in all Framework .NET

It returns true if all the values of expected array is found in actual variable:

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

Return true even if just one value is contained in the actual string:

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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