Hi, guys. I need to create some function in a test block. And I don’t want to create them out of the test block(because they are functions only related to this test block).
When I try to create function in the test block, but soon I find it’s not supported.
So I change to encapsulate into a struct.
But then I got the problem below. What’s the reason.
And is there any good way to to achieve this goal?
test "test_cond" {
const ProducerAndConsumer = struct {
idx: usize = 0,
queue: [Len]T = undefined,
const Len: usize = 1024;
const T = i32;
// The line below’s throwing an error: use of undeclared identifier 'ProducerAndConsumer'
fn can_produce(self: ProducerAndConsumer) bool {
return self.idx < self.queue.len;
}
// fn produce() {}
// ...
};
const v: ProducerAndCunsumer = .{};
std.testing.expect(v.can_produce());
}
If only your test uses Bla it should disappear for non test builds.
I guess the downside could be that editor tooling still shows the type when it is just used for the test and shouldn’t clutter the bigger namespace.