3
\$\begingroup\$

I have a unit test that needs to compare two arrays but it shouldn't care about the order of the arrays. I didn't see anything in the Test:Unit docs that provided this so I wrote my own. I really don't want to use this code if I don't have to (want to avoid Not Invented Here syndrome).

If you can give me some feedback on the following points it would be a huge help, thanks!

  1. Should I even use this? Is there a built-in Test:Unit assertion I can use?
  2. I've refactored this as much as I can but any further improvements are encouraged and welcome.
  3. I've included my tests for this custom assertion, can you see any cases that I may be missing?

Here is the custom assertion:

def assert_have_same_items(expected, actual, message = nil)
  full_message = build_message(message, "<?> was expected to contain the same collection of items as \n<?> but did not.\n", actual, expected)

  test = expected | actual

  assert_equal expected.size, actual.size, full_message
  assert_equal expected.size, test.size, full_message
end

And the tests:

test "assert_have_same_items with two arrays with same items in same order reports success" do
  assert_have_same_items [1,2,3], [1,2,3]
end

test "assert_have_same_items with two arrays with same items in different order reports success" do
  assert_have_same_items [1,2,3], [2,1,3]
end

test "assert_have_same_items with actual missing an item, reports failure" do
  assert_should_fail do
    assert_have_same_items [1,2,3], [2,1]
  end
end

test "assert_have_same_items with actual having more items, reports failure" do
  assert_should_fail do
    assert_have_same_items [1,2,3], [2,1,3,4,5,6]
  end
end

test "assert_have_same_items with actual having duplicates, reports failure" do
  assert_should_fail do
    assert_have_same_items [1,2,3], [2,1,3,2,1,3]
  end
end

test "assert_have_same_items with actual having duplicates and expected having same number of items, reports failure" do
  assert_should_fail do
    assert_have_same_items [1,2,3,4,5,6], [2,1,3,2,1,3]
  end
end

protected
def assert_should_fail
  begin
    yield if block_given?
    flunk "Expected failure but assertion reported success"
  rescue Test::Unit::AssertionFailedError
    assert true
  end
end
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You have adequate test coverage for integers, and probably this will extend to floating points as well (although you really should test those as well, IMO). However you should also test for objects. Also, try a couple combinations of objects, numbers, and strings to be really sure that your assertion is correct.

If you only need to test for integer arrays, you should make it clear that the assert only works for them and its behavior with other arrays is undefined. In that case maybe you should change the name to assert_same_items_integer or something similar for clarity.

\$\endgroup\$
1
  • \$\begingroup\$ good point, the intent is for it to handle arrays of anything so I'll add some tests for each case. Thanks! \$\endgroup\$
    – Jared
    Commented Dec 30, 2011 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.