Thread overview
Re: Proposal: Enhancing unittest with Inline Module-Level Statements
Nov 20
monkyyy
Nov 21
ryuukk_
November 20
On Tuesday, November 19, 2024 11:11:58 PM MST Sharif yt via Digitalmars-d wrote:
> Hello D enthusiasts,
>
> One of D's standout features is its built-in unittest, which makes writing and maintaining test cases seamless and integrated. However, while working with the current unittest block structure, I’ve been experimenting with a more concise module-level statement for simpler assertions.
>
> Inspired by an idea I came across (and expanded upon), this
> approach allows inline assertions using unittest() with
> parentheses instead of braces {}. Here’s an example:
>
> d
> Copy code
> unittest(1 == 1);
> unittest(1 == 1, "Math broke");

> What do you think about extending D's unittest capabilities in this way? Could it make testing in D even more accessible and enjoyable?

Personally, I think that it will just encourage writing poor unit tests. Properly testing a function requires far more than a line or two unless the functions is absolutely trivial. And if you're writing more than a line or two, it just makes more sense to use the unittest blocks that we have. We should be encouraging more thorough testing, and this would do the exact opposite of that.

So, IMHO, this would be adding more complication to the language just to try to make it easier to write inadequate tests, and I don't think think that we should be doing that.

- Jonathan M Davis




November 20
On Wednesday, 20 November 2024 at 09:48:06 UTC, Jonathan M Davis wrote:
> On Tuesday, November 19, 2024 11:11:58 PM MST Sharif yt via Digitalmars-d wrote:
>> [...]
>
>> [...]
>
> Personally, I think that it will just encourage writing poor unit tests. Properly testing a function requires far more than a line or two unless the functions is absolutely trivial. And if you're writing more than a line or two, it just makes more sense to use the unittest blocks that we have. We should be encouraging more thorough testing, and this would do the exact opposite of that.
>
> So, IMHO, this would be adding more complication to the language just to try to make it easier to write inadequate tests, and I don't think think that we should be doing that.
>
> - Jonathan M Davis

making things annoying doesn't make me do them right
November 21

On Wednesday, 20 November 2024 at 09:48:06 UTC, Jonathan M Davis wrote:

>

On Tuesday, November 19, 2024 11:11:58 PM MST Sharif yt via

>

...
Inspired by an idea I came across (and expanded upon), this
approach allows inline assertions using unittest() with
parentheses instead of braces {}...
...
So, IMHO, this would be adding more complication to the language just to try to make it easier to write inadequate tests, and I don't think think that we should be doing that.

I agree with Jonathan. In addition, instead of the ordinary assert(), I use the following assert_eq(), inspired by Rust. This gives me 2 extra usage featıres.

alias equal = assert_eq;
template assert_eq(string msg = "error", R)
{
  enum pred = "a != b";

  bool assert_eq(in R p, in R q)
  {
    static if (imported!"std.range".hasLength!R)
    {
      import std.algorithm : equal;
      if (p.equal!pred(q))
        goto _error_;
    }	
	
    if (p != q)
      _error_:
	
    static if (msg.length) assert(0, msg);
    else return 0;

    return 1;
  }
}

SDB@79

November 21
On Wednesday, 20 November 2024 at 17:10:30 UTC, monkyyy wrote:
> On Wednesday, 20 November 2024 at 09:48:06 UTC, Jonathan M Davis wrote:
>> On Tuesday, November 19, 2024 11:11:58 PM MST Sharif yt via Digitalmars-d wrote:
>>> [...]
>>
>>> [...]
>>
>> Personally, I think that it will just encourage writing poor unit tests. Properly testing a function requires far more than a line or two unless the functions is absolutely trivial. And if you're writing more than a line or two, it just makes more sense to use the unittest blocks that we have. We should be encouraging more thorough testing, and this would do the exact opposite of that.
>>
>> So, IMHO, this would be adding more complication to the language just to try to make it easier to write inadequate tests, and I don't think think that we should be doing that.
>>
>> - Jonathan M Davis
>
> making things annoying doesn't make me do them right

unittest in the middle of a function is bad, to stay polite