statement unittest
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Fri Mar 29 12:26:00 UTC 2024
On Friday, March 29, 2024 5:53:52 AM MDT Nicholas Wilson via dip.ideas wrote:
> On Thursday, 21 March 2024 at 19:39:41 UTC, monkyyy wrote:
> > I have this code:
> >
> > ```d
> > auto reduce(alias F,R)(R r)=>r.acc!F.last.front;
> > unittest{ assert(counter(10).reduce!((a,int b=0)=>a+b)==45);}
> >
> > auto all(alias F,R)(R r)=>r.map!F.filter!(a=>!a).empty;
> > unittest{ assert([1,3,5,7,9].torange.all!(a=>a%2));}
> >
> > auto any(alias F,R)(R r)=> ! r.filter!F.empty;
> > unittest{ assert([1,2,3,4,15].torange.any!(a=>a==15));}
> >
> > auto count(R)(R r)=>r.reduce!((a,int b=0)=>b+1);
> > unittest{ assert(counter(10).filter!(a=>a%2).count==5);}
> > ```
> >
> > "unittest{ assert(..);}" should be reduceable to `unittest(...)`
> >
> > i.e. `unittest(1==1)` would compile
>
> there should be no DIP required here, `unittest`s are function
> declarations, and so they _should_ work as `unittest =>
> assert(1==1);`. They don't currently though (purely a problem
> with the parser[1]), that seems like a bug, please file one.
>
> [1]:
> https://github.com/dlang/dmd/blob/e00869b27e3d9cabed2b6e73503561997398759c/c
> ompiler/src/dmd/parse.d#L521 explicitly checks for { }
> a similar problem exists for `parseUnitTest` that calls
> `parseStatement` that explicitly checks for `{}`
Even though it's close, they don't use the normal function syntax. They
happen to be implemented as functions, but they're not declared like they're
functions. If they used the normal function syntax, then they'd have parens,
which they don't. So, I don't see how you can argue that it's a bug that
they don't allow the lambda syntax.
It's also incredibly bad practice in general to be writing unit tests that
only have a single test. So, I really don't see any value in allowing poeple
to write something like
unittest => assert(1 == 1);
And if it ever becomes possible, it should be actively discouraged, because
it would be terrible practice to write tests that covered that little. So,
we're just better off not try to add new syntax here.
- Jonathan M Davis
More information about the dip.ideas
mailing list