www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - statement unittest

reply monkyyy <crazymonkyyy gmail.com> writes:
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
Mar 21
next sibling parent reply Dom DiSc <dominikus scherkl.de> writes:
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);}
 ```
I agree, that looks somewhat ugly. But if you have such short functions and short unittest, why not sort them: ```d 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.
Mar 23
next sibling parent reply Nick Treleaven <nick geany.org> writes:
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:

 ```d
 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.
Mar 23
parent Dom DiSc <dominikus scherkl.de> writes:
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.
Mar 29
prev sibling parent monkyyy <crazymonkyyy gmail.com> writes:
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: ```d 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
Mar 23
prev sibling next sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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/compiler/src/dmd/parse.d#L521 explicitly checks for { } a similar problem exists for `parseUnitTest` that calls `parseStatement` that explicitly checks for `{}`
Mar 29
next sibling parent "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
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
Mar 29
prev sibling next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Mar 29
parent monkyyy <crazymonkyyy gmail.com> writes:
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
Mar 29
prev sibling parent monkyyy <crazymonkyyy gmail.com> writes:
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
Mar 29
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 3/21/2024 12:39 PM, monkyyy wrote:
 i.e. `unittest(1==1)` would compile
It's an interesting idea, but it would close the door on ever having arguments to unittest blocks.
Apr 05
parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Friday, 5 April 2024 at 15:38:28 UTC, Walter Bright wrote:
 On 3/21/2024 12:39 PM, monkyyy wrote:
 i.e. `unittest(1==1)` would compile
It's an interesting idea, but it would close the door on ever having arguments to unittest blocks.
why? ```d unittest identity(T)(T i){ assert(i==i); } identity(1); identity(13.37); ``` ```d unittest(1==1,"math broke"); ``` I dont think unittest syntax is constrained much
Apr 05
parent reply Meta <jared771 gmail.com> writes:
On Friday, 5 April 2024 at 17:34:43 UTC, monkyyy wrote:
 On Friday, 5 April 2024 at 15:38:28 UTC, Walter Bright wrote:
 On 3/21/2024 12:39 PM, monkyyy wrote:
 i.e. `unittest(1==1)` would compile
It's an interesting idea, but it would close the door on ever having arguments to unittest blocks.
why? ```d unittest identity(T)(T i){ assert(i==i); } identity(1); identity(13.37); ``` ```d unittest(1==1,"math broke"); ``` I dont think unittest syntax is constrained much
Instead why not just allow assert to be a top-level statement?
Apr 05
parent monkyyy <crazymonkyyy gmail.com> writes:
On Saturday, 6 April 2024 at 06:33:07 UTC, Meta wrote:
 On Friday, 5 April 2024 at 17:34:43 UTC, monkyyy wrote:
 On Friday, 5 April 2024 at 15:38:28 UTC, Walter Bright wrote:
 On 3/21/2024 12:39 PM, monkyyy wrote:
 i.e. `unittest(1==1)` would compile
It's an interesting idea, but it would close the door on ever having arguments to unittest blocks.
why? ```d unittest identity(T)(T i){ assert(i==i); } identity(1); identity(13.37); ``` ```d unittest(1==1,"math broke"); ``` I dont think unittest syntax is constrained much
Instead why not just allow assert to be a top-level statement?
I dont see a meaningful difference, if it was top level it would probably need a dummy context and a way to be run which will probably come from it acting like a unittest
Apr 06