Ignore struct field when using std.testing.expectEqualDeep

Say I have a struct with some fields:

const S = struct {
  a: u32,
  b: u32,
};

I am comparing them for equality during testing using std.testing.expectEqualDeep, however, for some particular test I want the equality to skip the comparison of a particular field. Is there a way to achieve this?

Hypothetically I am looking for something like this:

const one: S = .{ .a = 1, .b = 2 };
const two: S = .{ .a = 1, .b = std.testing.ANY };
try std.testing.expectEqualDeep(one, two); // Passes for any value of one.b

You could just set them to the same value right before testing:

var two = ...
two.b = one.b
try std.testing.expectEqualDeep(one, two);

The alternative would be to write your own comparison function.

3 Likes

Yes that is the solution that I ended up doing, but having been using Python at work recently my brain decided that having the ANY value is nicer, just wanted to see if there is already something like that as I could not find it.

But regardless, thanks for your answer.