Skip to content

Call code in "ok" method - #1094

Closed
vsepohui wants to merge 2 commits into
Test-More:masterfrom
vsepohui:master
Closed

vsepohui wants to merge 2 commits into
Test-More:masterfrom
vsepohui:master

Conversation

@vsepohui

Copy link
Copy Markdown

Call code in "ok" method

Looking to test.

@jkeenan

jkeenan commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

What is the rationale for this proposed change?

@vsepohui

Copy link
Copy Markdown
Author

For supporting of feature:

Putting sub { ... } to ok() method.

@exodist

exodist commented Oct 21, 2026

Copy link
Copy Markdown
Member

I appreciate the desire for this functionality, but I cannot merge this pull request for a couple of reasons:

  1. It will break any existing uses of Test::More where people do ok(\&code, ...) where their expectation is that it simply does a boolean check that the first argument is truthy. I have written code like this, and I am sure many others have as well. We cannot add a change that alters that behavior out from under them.
  2. Test::Builder and Test::More are more or less suspended in state, they can get bugfixes, but due to compatibility constraints and not wanting to break long-standing existing code, no new features or significant API changes (except possibly critical bug fixes) are allowed.

To do something like this it would have to be part of the Test2 tooling where active development still happens. That said I do not anticipate adding this to the standard ok() from Test2 because once again it could break things, and for sure breaks expectations.

That said the Test2 tooling makes it very easy for you to implement an ok() of your own that does this, and to create a bundle of Test2 tools that includes the standard ones, but overrides ok() with your own. If you are interested in that I would be happy to point you at the right docs, or even discuss it with you and help you get there.

@vsepohui

Copy link
Copy Markdown
Author

Same realized in subtest():

subtest 'some name' => sub {
my $num_tests = 2 + int( rand(3) );
plan tests => $num_tests;
ok 1 for 1 .. $num_tests - 1;
subtest 'some name' => sub {
plan 'no_plan';
ok 1 for 1 .. 2 + int( rand(3) );
};
};

Same code samples, may be very usefull in test-coding practice!

@exodist

exodist commented Oct 21, 2026

Copy link
Copy Markdown
Member

Subtests are an unrelated concept. I am not going to break compatibility by doing this. I am happy to help you use Test2 and a custom ok() if you would like.

@exodist exodist closed this Oct 21, 2026
@vsepohui

Copy link
Copy Markdown
Author

Ok, but what's about support?

I found in CPAN tests with "sub" in ok(), it was mistakes of developers, may be need to merge my patch for community supporting?

@haarg

haarg commented Oct 21, 2026

Copy link
Copy Markdown
Member

There are definitely numerous tests doing ok $class->can('method'); which this would break, so this change isn't possible.

It is also not obvious what value this would provide over other options like a do { } block or calling the coderef with ->().

@vsepohui

Copy link
Copy Markdown
Author

Ok!

Reasonable argument!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants