patterncsharpMinor
Test if two IDictionary objects contain the same values
Viewed 0 times
sametheobjectscontaintwotestidictionaryvalues
Problem
I'm adding a function to my test library to assist in testing if two
I need the method to be generic and support a dictionary that has collections as values. So I thought I'd try to do it without using class types.
It appears to be working, but I would like to clarify that I didn't make any mistakes that would impact my tests later.
```
///
/// Checks if two dictionaries contain the same values. Supports recursive
/// dictionaries and collections as values.
///
/// Expected value
/// Actual value
// ReSharper disable CanBeReplacedWithTryCastAndCheckForNull
public static void dictionary(IDictionary pExpect, IDictionary pActual)
{
Assert.IsNotNull(pExpect);
Assert.IsNotNull(pActual);
if (pExpect.Keys.Count != pActual.Keys.Count)
{
Assert.Fail("Expected {0} keys, but contains {1} keys.", pExpect.Keys.Count, pActual.Keys.Count);
}
object[] expectKeys = new object[pExpect.Keys.Count];
object[] actualKeys = new object[pExpect.Keys.Count];
pExpect.Keys.CopyTo(expectKeys, 0);
pActual.Keys.CopyTo(actualKeys, 0);
// check if the two key sets are the same
CollectionAssert.AreEquivalent(expectKeys, actualKeys);
for (int i = 0, c = expectKeys.Length; i < c; i++)
{
object expect = pExpect[expectKeys[i]];
object actual = pActual[actualKeys[i]];
// both can be null
if (expect == null && actual == null)
{
continue;
}
// both must be assigned a value
Assert.IsNotNull(expect);
Assert.IsNotNull(actual);
// must be same types
Assert.AreEqual(expect.GetType(), actual.GetType());
if (expect is IDictionary)
{
// support recursive dictionary checks
dictionary((IDictionary)expect,
IDictionary objects contain the same keys/values.I need the method to be generic and support a dictionary that has collections as values. So I thought I'd try to do it without using class types.
It appears to be working, but I would like to clarify that I didn't make any mistakes that would impact my tests later.
```
///
/// Checks if two dictionaries contain the same values. Supports recursive
/// dictionaries and collections as values.
///
/// Expected value
/// Actual value
// ReSharper disable CanBeReplacedWithTryCastAndCheckForNull
public static void dictionary(IDictionary pExpect, IDictionary pActual)
{
Assert.IsNotNull(pExpect);
Assert.IsNotNull(pActual);
if (pExpect.Keys.Count != pActual.Keys.Count)
{
Assert.Fail("Expected {0} keys, but contains {1} keys.", pExpect.Keys.Count, pActual.Keys.Count);
}
object[] expectKeys = new object[pExpect.Keys.Count];
object[] actualKeys = new object[pExpect.Keys.Count];
pExpect.Keys.CopyTo(expectKeys, 0);
pActual.Keys.CopyTo(actualKeys, 0);
// check if the two key sets are the same
CollectionAssert.AreEquivalent(expectKeys, actualKeys);
for (int i = 0, c = expectKeys.Length; i < c; i++)
{
object expect = pExpect[expectKeys[i]];
object actual = pActual[actualKeys[i]];
// both can be null
if (expect == null && actual == null)
{
continue;
}
// both must be assigned a value
Assert.IsNotNull(expect);
Assert.IsNotNull(actual);
// must be same types
Assert.AreEqual(expect.GetType(), actual.GetType());
if (expect is IDictionary)
{
// support recursive dictionary checks
dictionary((IDictionary)expect,
Solution
Your code doesn't work, because items in a dictionary aren't ordered in any way.
Example code that fails your test:
Some more notes about your code:
Also, I would avoid using the non-generic
Example code that fails your test:
dictionary(
new Dictionary { { 0, 0 }, { 1, 1 } },
new Dictionary { { 1, 1 }, { 0, 0 } });Some more notes about your code:
public static void dictionary(IDictionary pExpect, IDictionary pActual)dictionary is a bad name for this method, because it doesn't really explain what it's supposed to do. Something like DictionaryEqualityTest would be better.Also, I would avoid using the non-generic
IDictionary if possible, IDictionary would be better (though it would complicate the recursion).Code Snippets
dictionary(
new Dictionary<int, int> { { 0, 0 }, { 1, 1 } },
new Dictionary<int, int> { { 1, 1 }, { 0, 0 } });public static void dictionary(IDictionary pExpect, IDictionary pActual)Context
StackExchange Code Review Q#35233, answer score: 4
Revisions (0)
No revisions yet.