zig icon indicating copy to clipboard operation
zig copied to clipboard

std.testing: expectEqualDeep() - support self-referential structs

Open travisstaloch opened this issue 2 years ago • 4 comments

add std.testing.Error with names of all possible errors found in std.testing's pub functions.

  • use this new error set to allow expectEqualDeep() to support self-referential structs
  • add a test to verify that this error set contains all possible errors - currently skipped due to compiler bug

travisstaloch avatar Mar 17 '23 09:03 travisstaloch

I'm hoping for some feedback on how to improve the error set test. I would like to make sure that Error contains exactly all possible errors and nothing more. But I'm not sure if its yet possible in stage2 to construct an error set from other error sets yet (due to #12806).

travisstaloch avatar Mar 17 '23 09:03 travisstaloch

~~I'm not 100% sure all std.testing functions should have the same error set? I think it would make more sense to split it up into a bunch of error sets specific to each function - then if you'd still want a single error set to refer to, you could just combine all the sub-error sets into one. One drawback to that is extra maintenance burden.~~

Edit: ignore that first part, I misunderstood the purpose of the change.

It would likely be prudent to add a note that this could go into an infinite loop and cause a stack overflow if given a container that actually has a pointer to itself, or a value which contains a container that does.

InKryption avatar Mar 17 '23 11:03 InKryption

It would likely be prudent to add a note that this could go into an infinite loop and cause a stack overflow if given a container that actually has a pointer to itself, or a value which contains a container that does.

Thanks @InKryption :+1: . I've modified the old note: comment which was previously removed.

I've also added a few other comments which document intent.

A benefit of this pr which I should have mentioned earlier is that the new std.testing.Error set will allow users to make recursive test helpers like expectEqualDeep() without having define an error set. Instead they can define it as:

fn myRecursiveTestHelper(...) std.testing.Error!void { ... }

travisstaloch avatar Mar 17 '23 20:03 travisstaloch

Is it possible for expectEqualDeep to return any error other than TestExpectedEqual? Unless I'm missing something, it seems to me it would be clearer to specify that restricted error set explicitly (error{TestExpectedEqual}!void), although the new Error type could be useful for other things, as you mentioned.

ianprime0509 avatar Jun 10 '23 19:06 ianprime0509

fixes #16625

it would be clearer to specify that restricted error set explicitly (error{TestExpectedEqual}!void)

I've followed your advice @ianprime0509 and removed the Error set and test Error. The test wasn't really doing anything. I've verified locally that this fixes #16625.

travisstaloch avatar Sep 22 '23 02:09 travisstaloch

I can't tell why aarch64-linux failed. It doesn't seem related to this pr. This is the end of the log:

 -- Install configuration: "Debug"
+ stage3/bin/zig test ../test/behavior.zig -I../test
Error: The operation was canceled.

travisstaloch avatar Sep 23 '23 00:09 travisstaloch

Seems like that CI run timed out somehow. I agree that that doesn't seem like this PR's fault - I've triggered a re-run.

mlugg avatar Sep 23 '23 04:09 mlugg