Skip to content

IsSubset should return a better default AssertMessage #662

@LokiMidgard

Description

@LokiMidgard

Description

The CollectionAssert.IsSubsetOf error message could have more information.

Currently it only says it failed. But what elements are missing is actually ommitted.

public static void IsSubsetOf(ICollection subset, ICollection superset, string message, params object[] parameters)
{
Assert.CheckParameterNotNull(subset, "CollectionAssert.IsSubsetOf", "subset", string.Empty);
Assert.CheckParameterNotNull(superset, "CollectionAssert.IsSubsetOf", "superset", string.Empty);
if (!IsSubsetOfHelper(subset, superset))
{
Assert.HandleFail("CollectionAssert.IsSubsetOf", message, parameters);
}
}

The helper method seems to have the information but will not give that to the caller. (It takes a shourtcut on an error not searching for mor problems but even one element that is missing is an information for the tester)

internal static bool IsSubsetOfHelper(ICollection subset, ICollection superset)
{
// $ CONSIDER: The current algorithm counts the number of occurrences of each
// $ CONSIDER: element in each collection and then compares the count, resulting
// $ CONSIDER: in an algorithm of ~n*log(n) + m*log(m) + n*log(m). It should be
// $ CONSIDER: faster to sort both collections and do an element-by-element
// $ CONSIDER: comparison, which should result in ~n*log(n) + m*log(m) + n.
// Count the occurrences of each object in both collections.
int subsetNulls;
Dictionary<object, int> subsetElements = GetElementCounts(subset, out subsetNulls);
int supersetNulls;
Dictionary<object, int> supersetElements = GetElementCounts(superset, out supersetNulls);
if (subsetNulls > supersetNulls)
{
return false;
}
// Compare the counts of each object in the subset to the count of that object
// in the superset.
foreach (object element in subsetElements.Keys)
{
int subsetCount;
subsetElements.TryGetValue(element, out subsetCount);
int supersetCount;
supersetElements.TryGetValue(element, out supersetCount);
if (subsetCount > supersetCount)
{
return false;
}
}
// All the elements counts were OK.
return true;
}

Steps to reproduce

Write a Method that uses Assert.IsSubsetOf with an input that will fail

Expected behavior

Information what elements are missing in the collection.

Actual behavior

The Message only says something with the collection is wrong. CollectionAssert.IsSubsetOf failed.

Environment

  • MSTest.TestAdapter (1.4.0)
  • MSTest.TestFramework (1.4.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: AssertionHelp-WantedThe issue is up-for-grabs, and can be claimed by commenting

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions