How to Elucidate Array Differences?
Tags: discuss, minitest, ruby, testing
Published: 2019-01-31 21:15:20

---
title: How to Elucidate Array Differences?
published: true
tags: discuss, minitest, ruby, testing
---

Should have posted [this](https://dev.to/burdettelamar/lucid-array-differences-1b5e) to <code>#discuss</code> in the first place.

So now I will paste in the previous post here, and plead for discussion.

<hr>

As promised, I'm looking to add <code>Array</code> explication to gem [minitest_lucid](https://rubygems.org/gems/minitest_lucid) (documentation at [GitHub](https://github.com/BurdetteLamar/minitest_lucid#minitest-lucid)).

So far, useful elucidations for <code>Hash</code>, <code>Set</code>, and <code>Struct</code> have been pretty straightforward, as will be the case for <code>OpenStruct</code>.

For <code>Array</code>, not so much.

*Stipulated:  None of this matters for small arrays, whose differences are easy to evaluate visually.  More sophisticated comparisons matter only for larger arrays.*

*Yes, this is overkill -- until it isn't.*

*For a failed assertion:*

- *How much information is enough?*
- *More is better.*
- *Too much is just about right.*


I can think of some array treatments that might be useful, but the usefulness of each may depend greatly on the nature of the actual data:

- Element-by-element comparison.
- Diff-LCS comparison.
- Missing elements:  <code>expected - actual</code>.
- Unexpected elements:  <code>actual - expected</code>.
- Common elements:  <code>actual & expected</code>.
- All elements:  <code>actual | expected</code>.

I'll detail these first two.

<h3>Element-By-Element Comparison</h3>

In this comparison, <code>actual[0]</code> is compared to <code>expected[0]</code>, <code>actual[1]</code> is compared to <code>expected[1]</code>, ... <code>actual[n]</code> is compared to <code>expected[n]</code>.

This comparison is ideal when:

- The arrays are the same size.
- Most of the elements are the same.

The comparison may still be useful when:

- The arrays are of similar size.
- Fewer of the elements are the same. 

<h3>Diff-LCS Comparison</h3>

"[Diff::LCS](https://github.com/halostatue/diff-lcs#description) computes the difference between two Enumerable sequences using the McIlroy-Hunt longest common subsequence (LCS) algorithm."

The matching algorithm is good at finding sequences that match, even when they are not in the same position in the arrays.

This comparison is ideal when:

- The arrays have multiple matching sequences.
- The actual array has a few insertions.
- The actual array has a few deletions.

The comparison may still be useful when:

- The arrays have fewer matching sequences.
- The actual array has more insertions.
- The actual array has more deletions.

<h3>Elucidation Output Format

I began by putting the elucidation into the message of the failed assertion.

Because there may be more elucidation data rather than less, I'm experimenting with additional output in HTML, where color and links can help organize and clarify.

<h3>Finally, What Should <code>minitest_lucid</code> Do?

If <code>minitest_lucid</code> implements several of these strategies, should the default be to return the information for all?  If not, what's the default?  And should there be an option for the non-default?

<hr>

I will be grateful for your thinking about these complexities, expecially if you're a present or future user of gem [minitest_lucid](https://rubygems.org/gems/minitest_lucid).

Thanks much!