3

I have a function that manually extracts a date.

[TestMethod]
public void TestRemoveTwoDates()
{
    String test = "at 06/2/2012 to 12/10/2012";
    int[][] actual = String_Parser.RemoveTwoDates(test, "to");
    int[][] expected = new int[2][];
    expected[0] = new int[3] { 6, 2, 2012 };
    expected[1] = new int[3] { 12, 10, 2012 };

    CollectionAssert.AreEqual(expected, actual);
}

P.S. Browsing through SO, I realized it isn't good practice to return arrays as they are mutable. But I still wanna know how i can test this other than using a loop to Assert Each element in the array.

Henry
  • 1,041
  • 1
  • 14
  • 14
  • Sidenote: Here's a much simpler way to populate the expected value: `var expected = new[] { new[] { 6, 2, 2012 }, new[] { 12, 10, 2012 } };`. – Marcelo Cantos Nov 02 '12 at 06:08
  • Technically, what you have is an array of arrays, or a jagged array, not a 2D array. A 2D array would be `int[,]`. – David Yaw Nov 02 '12 at 06:25

2 Answers2

4

Since you cannot use the Hashcode which uses the reference rather than the content of an array, the way to go would be to iterate all values in a nested loop, compare value by value (as in stackoverflow.com/questions/2893297/iterate-multi-dimensional-array-with-nested-foreach-statement), use a boolean which you set to false at the first mismatch, and assert that this boolean is true in your unit test.

Sathish
  • 1,936
  • 4
  • 28
  • 38
Olaf
  • 10,049
  • 8
  • 38
  • 54
0

look at Collection section in https://fluentassertions.com/introduction, it may help you

ZAD-Man
  • 1,328
  • 23
  • 42
Sergii Bidnyi
  • 306
  • 1
  • 7