exercism/xelixir — pull request #5 by andrewsardone (2014-05-04 18:31:54+00:00)
Title: Fix the order of some test assertions

Several [exercism/xelixir][xel] assertions seem to be flip-flopped, making their failure messages a bit confusing.

The [`assert/1` macro][as] will format failing scenarios in a helpful manner of comparing actual and expected values. Given a script with a failing test:

```elixir

defmodule Math do
def add(x, y), do: 0
end

ExUnit.start

defmodule MathTest do
use ExUnit.Case, async: true

test "addition works" do
assert Math.add(1, 1) == 2
end
end
```

…running `elixir example.exs` produces a helpful failure message:

```
1) test addition works (MathTest)
** (ExUnit.ExpectationError)
expected: 0
to be equal to (==): 2
at example.exs:11

Finished in 0.02 seconds (0.02s on load, 0.00s on tests)
1 tests, 1 failures
```

The proper form of writing an assertion seems to be:

```elixir
assert actual_value == expected_value
```

Several [exercism/xelixir][xel] tests have swapped the actual and expected values. For example, if you look at the [the first test of point_mutations_test.exs][pntmut], the `actual_value` is on the right-hand-side of the `==`, which produces a confusing failure message:

```
1) test no difference between empty strands (DNATest)
** (ExUnit.ExpectationError)
expected: 0
to be equal to (==): nil
at point_mutations_test.exs:14
```

The message is not describing the correct failure of `DNA.hamming_distance/2`, namely, that it's an empty implementation that's returning `nil` when the caclulation should return 0. Instead, this message leads the implementer to believe that the function is returning 0 when `nil` is what one wants.

Switching up the order of these assertions improves the failure messages and makes it easier to TDD an exercise solution.

[xel]: https://github.com/exercism/xelixir
[as]: http://elixir-lang.org/docs/stable/ExUnit.Assertions.html#assert/1
[pntmut]: https://github.com/exercism/xelixir/blob/8584070f5d8436816c9d7b1480b687947b383bc3/point-mutations/point_mutations_test.exs#L13-L15

## How to test

You could check if the test results for the example solutions match between `master` and the PR. Here's a quick script that will compare the results:

```sh
# TODO: Assign the name of your local branch
pr_branch=assertion-order

function md5_of_test_results {
for i in $(echo */); do pushd>/dev/null $i; EXERCISM_TEST_EXAMPLES=1 elixir *test.exs | tail -1; popd>/dev/null; done | md5
}

echo ""
echo "=== Running tests on master === "
git checkout master
master_md5=$( md5_of_test_results )

echo ""
echo "=== Running the tests on $pr_branch ==="
git checkout $pr_branch
branch_md5=$( md5_of_test_results )

echo ""
if [ "$master_md5" = "$branch_md5" ]; then
echo "✔︎ Same test results"
else
echo "𝗫 Different test results"
fi
```

Thanks! I think [exercism.io](http://exercism.io/) is a fantastic project!