Unit testing a function returning void
Imperatorn
johan_forsberg_86 at hotmail.com
Thu Nov 3 18:07:25 UTC 2022
On Thursday, 3 November 2022 at 10:26:04 UTC, Imperatorn wrote:
> On Thursday, 3 November 2022 at 10:00:27 UTC, Bruno Pagis wrote:
>> Good morning,
>> I have the following class:
>>
>> ```
>> class A {
>> int[] array;
>>
>> ...
>>
>> void print() {
>> writeln("array = ", this.array);
>> }
>> }
>> ```
>>
>> I would like to unit test the print function (yes, I know, not
>> very useful on the above example since print is merely a
>> duplicate of writeln...). Is there a way to use assert to test
>> the output of the print function to stdout? Something like:
>> ```
>> A myClass= new A;
>> myClass.array = [1,2];
>> assert(myClass.print() == "array = [1,2]"); // I know that
>> print does not return anything so this is wrong, but you get
>> the idea :-)
>> ```
>> Thanks.
>
> Just so we understand, do you want to verify that the output is
> indeed directed to stdout and not some other stream?
Just for documentation purposes, if you wished to redirect I
believe you could do something like this (untested):
```d
auto original = stdout; // save
stdout.open(newdest, "wt"); // redirect
// do stuff, check newdest
stdout = original; // restore
```
More information about the Digitalmars-d-learn
mailing list