Search
statement unittest
Mar 21
monkyyy
Mar 23
Dom DiSc
Mar 29
Dom DiSc
Mar 23
monkyyy
Mar 30
monkyyy
Mar 30
monkyyy
Apr 05
monkyyy
Apr 06
Meta
Apr 06
monkyyy

I have this code:

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

On Thursday, 21 March 2024 at 19:39:41 UTC, monkyyy wrote:

>

I have this code:

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);}

I agree, that looks somewhat ugly. But if you have such short functions and short unittest, why not sort them:

auto reduce(alias F,R)(R r)=>r.acc!F.last.front;
auto all(alias F,R)(R r)=>r.map!F.filter!(a=>!a).empty;
auto any(alias F,R)(R r)=> ! r.filter!F.empty;
auto count(R)(R r)=>r.reduce!((a,int b=0)=>b+1);

unittest
{
assert(counter(10).reduce!((a,int b=0)=>a+b)==45);
assert([1,3,5,7,9].torange.all!(a=>a%2));
assert([1,2,3,4,15].torange.any!(a=>a==15));
assert(counter(10).filter!(a=>a%2).count==5);
}

That's pretty much as short as your proposal and even more readable.

On Saturday, 23 March 2024 at 09:43:20 UTC, Dom DiSc wrote:

>

I agree, that looks somewhat ugly. But if you have such short functions and short unittest, why not sort them:

auto reduce(alias F,R)(R r)=>r.acc!F.last.front;
auto all(alias F,R)(R r)=>r.map!F.filter!(a=>!a).empty;
auto any(alias F,R)(R r)=> ! r.filter!F.empty;
auto count(R)(R r)=>r.reduce!((a,int b=0)=>b+1);

unittest
{
assert(counter(10).reduce!((a,int b=0)=>a+b)==45);
assert([1,3,5,7,9].torange.all!(a=>a%2));
assert([1,2,3,4,15].torange.any!(a=>a==15));
assert(counter(10).filter!(a=>a%2).count==5);
}

That's pretty much as short as your proposal and even more readable.

Then you can't have documented unittests for each function.

On Saturday, 23 March 2024 at 09:43:20 UTC, Dom DiSc wrote:

>

On Thursday, 21 March 2024 at 19:39:41 UTC, monkyyy wrote:

>

[...]

I agree, that looks somewhat ugly. But if you have such short functions and short unittest, why not sort them:

auto reduce(alias F,R)(R r)=>r.acc!F.last.front;
auto all(alias F,R)(R r)=>r.map!F.filter!(a=>!a).empty;
auto any(alias F,R)(R r)=> ! r.filter!F.empty;
auto count(R)(R r)=>r.reduce!((a,int b=0)=>b+1);

unittest
{
assert(counter(10).reduce!((a,int b=0)=>a+b)==45);
assert([1,3,5,7,9].torange.all!(a=>a%2));
assert([1,2,3,4,15].torange.any!(a=>a==15));
assert(counter(10).filter!(a=>a%2).count==5);
}

That's pretty much as short as your proposal and even more readable.

Theres 30 of these and while all of them should be 1 liners, not all of them are

On Saturday, 23 March 2024 at 16:28:14 UTC, Nick Treleaven wrote:

>

On Saturday, 23 March 2024 at 09:43:20 UTC, Dom DiSc wrote:

>

That's pretty much as short as your proposal and even more readable.

Then you can't have documented unittests for each function.

If you want to generate documentation from this, every test should at least have one line of comment (aka "Documentation"), so can't be a one-liner anyway.

And especially in the description of some closely related functions I would prefer one unittest block testing all of them over the scattered tests between each function declaration.

On Thursday, 21 March 2024 at 19:39:41 UTC, monkyyy wrote:

>

I have this code:

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, unittests are function declarations, and so they should work as unittest => assert(1==1);. They don't currently though (purely a problem with the parser1), that seems like a bug, please file one.

explicitly checks for { }
a similar problem exists for parseUnitTest that calls parseStatement that explicitly checks for {}

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

On 30/03/2024 12:53 AM, Nicholas Wilson wrote:
> 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/compiler/src/dmd/parse.d#L521
> explicitly checks for { }
> a similar problem exists for `parseUnitTest` that calls `parseStatement` that explicitly checks for `{}`

https://issues.dlang.org/show_bug.cgi?id=24467

On Friday, 29 March 2024 at 11:53:52 UTC, Nicholas Wilson wrote:

>

unittest => assert(1==1);
thats only marginly shorter and I have no idea why you think lambda syntax helps here

On Friday, 29 March 2024 at 12:26:00 UTC, Jonathan M Davis wrote:
> It's also incredibly bad practice in general to be writing unit tests that only have a single test.

more tests gooder, I was writing one line tests after one line functions and had to get `);}` right each time

please treat users (like me) like easily distractable children
« First   ‹ Prev
1 2