Skip to content

Demo Test2::V0#7025

Draft
perlpunk wants to merge 6 commits intoos-autoinst:masterfrom
perlpunk:test2-v0
Draft

Demo Test2::V0#7025
perlpunk wants to merge 6 commits intoos-autoinst:masterfrom
perlpunk:test2-v0

Conversation

@perlpunk
Copy link
Copy Markdown
Contributor

@perlpunk perlpunk commented Feb 21, 2026

This is to show how Test2::V0 can be used.
Some differences:

  • PRO is can replace is_deeply and it can be more clever - you can leave out parts of the data structure you're not interested in
  • PRO Comes with its own mock and mock_obj function. Call tracking is possible. It also offers before, after and around. https://metacpan.org/pod/Test2::Tools::Mock
  • CONTRA It doesn't have an explain function :( (but there is https://metacpan.org/dist/Test2-Tools-Explain )
  • CONTRA we need -no_srand => 1 because otherwise the test database schema name will not be random and complain that it exists already (from a previous run). Maybe there is a better way to work around that
  • PRO ref_ok for checking a type of a variable
  • PRO lots of comparison functions https://metacpan.org/pod/Test2::V1#COMPARE

@perlpunk
Copy link
Copy Markdown
Contributor Author

I added another commit "Compare partial data structure and use 'bag' check for sets" showing how more sophisticated data structure tests can be made.

I removed the usage of always_explain there, and I think the default comparison output might be sufficient enough.

E()

{
   data => E()
}

means that in this test, we only check that the key data exists, but in the next test we check its content in more detail.

bag:

my $check = bag { item $_ for $jobW->id, $jobU->id, $jobR->id, $jobT->id; end() };
is $jobQ_deps->{children}->{Chained}, $check, 'jobQ is chained parent to all jobs except jobTA';

This is checking that any of the items are in the array, but the order doesn't matter.

@okurz
Copy link
Copy Markdown
Member

okurz commented Feb 21, 2026

But https://metacpan.org/pod/Test2::V1 "is out now" :) Should we use that?

@perlpunk
Copy link
Copy Markdown
Contributor Author

perlpunk commented Feb 21, 2026

But https://metacpan.org/pod/Test2::V1 "is out now" :) Should we use that?

Yes. That actually "fixes" the problem with srand - srand is off by default in V1.
(But we have to enable strict and warnings now explicitly with use Test2::V1 -p.)

We have to submit that version To Leap 16 though

@perlpunk
Copy link
Copy Markdown
Contributor Author

It seems mock_obj doesn't provide everything that Test::MockObject provides, e.g, set_series, but OTOH the simple use case is really short:

#  Test::MockObject
my $obj = Test::MockObject->new->set_always(is_server_error => 1)->set_always(json => {error => 'fake error'});
# Test2::V0
my $obj = mock 'obj', {is_server_error => 1, json => {error => 'fake error'}};
say $obj->is_server_error; # 1
say $obj->something; # undef

So the given hashref provides the return values for the methods, but calling any other method will not die, but simply return undef. Which seems a bit odd to me, but probably not a problem.

@perlpunk
Copy link
Copy Markdown
Contributor Author

set_series can be emulated like this though:

my $obj = mock obj => {} => (
    add => [
        methodname => sub { state @ret = qw(a b c); return shift @ret }
    ]
);

Comment on lines +646 to +648
my $check = bag { item $_ for $jobW->id, $jobU->id, $jobR->id, $jobT->id; end() };
is $jobQ_deps->{children}->{Chained}, $check, 'jobQ is chained parent to all jobs except jobTA';
is $jobQ_deps->{children}->{'Directly chained'}, [$jobTA->id], 'jobQ is directly chained parent to jobTA';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new test API allows for shorter code but the code is also less intuitive with "magic" functions like bag, item and end() that are not really self-explanatory.

@Martchus
Copy link
Copy Markdown
Contributor

Looks generally interesting.

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Feb 24, 2026

This pull request is now in conflicts. Could you fix it? 🙏

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.

3 participants