www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Phobos should provide unit testing faciilities

reply Quirin Schroll <qs.il.paperinik gmail.com> writes:
In [*Taming Immutable Data Types in D with 
librebindable*](https://www.youtube.com/watch?v=eGX_fxlig8I) by 
Mathis Beer, in the Q&A, it was pointed out that it’s quite 
common to forget testing template code against types that do 
exotic (yet valid) things, e.g. ` disable this()`, 
define/` disable` the copy constructor, contain qualified 
members, be immutable, etc.

One way to mitigate this would be a Phobos module (name it 
`std.testing` or something) that – possibly among other things – 
defines aptly named types that are irregular for the sole purpose 
of being used as examples, together with various AliasSeqs that 
group types that belong together somehow. Then, one could do:

```D
unittest
{
     import std.testing;
     static foreach (T; IrregularStructs)
     {
         T obj; // can default construct? T = NoDefaultCtor fails.
         auto obj2 = obj; // can copy? T = NoCopyCtor fails.
         // etc.?
     }
}
```

Anyone can find *some* irregular types and test against them, but 
looking into *every* corner requires extensive knowledge about 
the language’s niches.
Sep 12 2022
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/12/22 7:28 AM, Quirin Schroll wrote:
 In [*Taming Immutable Data Types in D with 
 librebindable*](https://www.youtube.com/watch?v=eGX_fxlig8I) by Mathis 
 Beer, in the Q&A, it was pointed out that it’s quite common to forget 
 testing template code against types that do exotic (yet valid) things, 
 e.g. ` disable this()`, define/` disable` the copy constructor, contain 
 qualified members, be immutable, etc.
 
 One way to mitigate this would be a Phobos module (name it `std.testing` 
 or something) that – possibly among other things – defines aptly named 
 types that are irregular for the sole purpose of being used as examples, 
 together with various AliasSeqs that group types that belong together 
 somehow. Then, one could do:
 
 ```D
 unittest
 {
      import std.testing;
      static foreach (T; IrregularStructs)
      {
          T obj; // can default construct? T = NoDefaultCtor fails.
          auto obj2 = obj; // can copy? T = NoCopyCtor fails.
          // etc.?
      }
 }
 ```
 
 Anyone can find *some* irregular types and test against them, but 
 looking into *every* corner requires extensive knowledge about the 
 language’s niches.
Great idea! Many things come to mind here. Like a forward-range-accepting functions should be tested to make sure it's properly using `.save` when copying, one could probably construct a range that doesn't work without using that method. -Steve
Sep 12 2022
parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Monday, 12 September 2022 at 14:04:40 UTC, Steven 
Schveighoffer wrote:
 Like a forward-range-accepting functions should be tested to 
 make sure it's properly using `.save` when copying, one could 
 probably construct a range that doesn't work without using that 
 method.
You got me. I never used `.save` and were I to write a range-consuming function, I’d forget about it, but the `AllKindsOfRanges` sequence wouldn’t. The community as a whole will probably not miss much and with time fill in all the gaps.
Sep 12 2022