zunit icon indicating copy to clipboard operation
zunit copied to clipboard

Introduce assert_not function

Open MichaelAquilina opened this issue 6 years ago • 1 comments

This is a very early implementation of an idea I had a few weeks back. Opened this early to get some feedback before continuing

Introduce an assert_not function that behaves the exact same way that assert does but with a negated result.

This should prevent the need to stay adding negative functions for every assertion created "same_as" / "different_to" etc. It also reads quite nicely imo:

assert_not "hello world" same_as "not the same thing"

Opening this PR early to get some early feedback about the approach

MichaelAquilina avatar Aug 18 '18 14:08 MichaelAquilina

Sorry for the delay in getting to this.

In principle, I like the idea. The only reason I have reservations is the same reason I went with explicit negative assertions in the first place, which is that they are more readable, and handling the difference in error message between positive/negative assertions becomes difficult when they both use the same function.

For example, assert_not 'a' in 'b' 'c' will return a message 'a' is not in (b c) as you've put in your test. In actual fact, if the test has passed, there should be no message generated at all. Similiarly in this case, if assert_not were to fail, then no message would be generated.

I realise that this is an early PR, but I'm not sure of a good way to handle this within each of the different assertion functions, unless we were to either:

  • Rename all negative assertion functions so that they follow a consistent format with positive/negative variants. E.g. assert equals will call _zunit_assert_equals, and assert_not equals will call _zunit_assert_not_equals (would require renaming _zunit_assert_not_equal_to and a breaking change)
  • Maintain a map of positive assertion keywords to their negative/opposite assertion functions (and vice-versa) and then call the relevant one, which will also allow us to maintain the existing negative assertion keywords.

I do particularly want to keep the 'readability' of the assertion calls as close to plain english sentences as reasonably possible, as this was one of my main motivations for building ZUnit in the first place. If we can keep the readability, then I'm happy for this to be included as an additional option if we can come up with something sensible.

molovo avatar Nov 05 '18 13:11 molovo