Skip to content

v1.6.0: ExUnit assertions edition

Compare
Choose a tag to compare
@novaugust novaugust released this 28 Jul 20:15
· 8 commits to main since this release

That's right, a feature release again so soon!

Improvements

This version of Styler adds many readability improvements around ExUnit assert and refute, specifically when working with 1. negations or 2. some Enum stdlib functions.

Some of these rewrites are not semantically equivalent; for example, refute is_nil(false) will be rewritten to assert false, which will fail.

ExUnit assert/refute rewrites

Styler now inverts negated (!, not) assert/refute (eg assert !x => refute x) statements, and further inverts refute with boolean comparison operators (refute x < y => assert x >= y) because non-trivial refutes are harder to reason about [ citation needed ]. Asserting something is not nil is the same as just asserting that something, so that's gone too now.

These changes are best summarized by the following table:

before styled
assert !x refute x
assert not x refute x
assert !!x assert x
assert x != nil assert x
assert x == nil no change
assert is_nil(x) no change
assert !is_nil(x) assert x
assert x not in y refute x in y
refute negated
refute x no change
refute !x assert x
refute not x assert x
refute x != y assert x == y
refute x !== y assert x === y
refute x != nil assert x == nil
refute x not in y assert x in y
refute comparison
refute x < y assert x >= y
refute x <= y assert x > y
refute x > y assert x <= y
refute x >= y assert x < y
  • assert Enum.member?(y, x) -> assert x in y
  • assert Enum.find(x, y) -> assert Enum.any?(x, y) (nb. not semantically equivalent in theory, but equivalent in practice)
  • assert Enum.any?(y, & &1 == x) -> assert x in y
  • assert Enum.any?(y, fn var -> var == x end) -> assert x in y

Fixes

  • alias lifting: fix bug lifting in snippets with a single ast node at the root level (like a credo config file) (#240, h/t @defndaines)