1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
use strict;
use warnings;
use Test::Builder;
use Test2::Tools::Tiny;
use Test2::API::InterceptResult;
use Scalar::Util qw/reftype/;
use Test2::API qw/intercept context/;
my $CLASS = 'Test2::API::InterceptResult';
tests construction => sub {
my $one = $CLASS->new('a');
ok($one->isa($CLASS), "Got an instance");
is(reftype($one), 'ARRAY', "Blessed arrayref");
is_deeply($one, ['a'], "Ref looks good.");
my $two = $CLASS->new_from_ref(['a']);
ok($two->isa($CLASS), "Got an instance");
is(reftype($two), 'ARRAY', "Blessed arrayref");
is_deeply($two, ['a'], "Ref looks good.");
my $three = $two->clone;
ok($three->isa($CLASS), "Got an instance");
is(reftype($three), 'ARRAY', "Blessed arrayref");
is_deeply($three, ['a'], "Ref looks good.");
push @$two => 'b';
is_deeply($two, ['a', 'b'], "Modified two");
is_deeply($three, ['a'], "three was not changed");
my $four = intercept {
ok(1, "Pass");
};
ok($four->isa($CLASS), "Intercept returns an instance");
};
tests event_list => sub {
my $one = $CLASS->new('a', 'b');
is_deeply([$one->event_list], ['a', 'b'], "event_list is essentially \@{\$self}");
};
tests _upgrade => sub {
require Test2::Event::Pass;
my $event = Test2::Event::Pass->new(name => 'soup for you', trace => {frame => ['foo', 'foo.pl', 42]});
ok($event->isa('Test2::Event'), "Start with an event");
my $one = $CLASS->new;
my $up = $one->_upgrade($event);
ok($up->isa('Test2::API::InterceptResult::Event'), "Upgraded the event");
is($up->result_class, $CLASS, "set the result class");
is_deeply($event->facet_data, $up->facet_data, "Facet data is identical");
$up->facet_data->{trace}->{frame}->[2] = 43;
is($up->trace_line, 43, "Modified the facet data in the upgraded clone");
is($event->facet_data->{trace}->{frame}->[2], 42, "Did not modify the original");
my $up2 = $one->_upgrade($up);
is("$up2", "$up", "Returned the ref unmodified because it is already an upgraded item");
require Test2::Event::V2;
my $subtest = 'Test2::Event::V2'->new(
trace => {frame => ['foo', 'foo.pl', 42]},
assert => {pass => 1, details => 'pass'},
parent => {
hid => 1,
children => [ $event ],
},
);
my $subup = $one->_upgrade($subtest);
ok($subup->the_subtest->{children}->isa($CLASS), "Blessed subtest subevents");
ok(
$subup->the_subtest->{children}->[0]->isa('Test2::API::InterceptResult::Event'),
"Upgraded the children"
);
};
tests hub => sub {
my $one = intercept {
ok(1, "pass");
ok(0, "fail");
plan 2;
};
my $hub = $one->hub;
ok($hub->isa('Test2::Hub'), "Hub is a proper instance");
ok($hub->check_plan, "Had a plan and followed it");
is($hub->count, 2, "saw both events");
is($hub->failed, 1, "saw a failure");
ok($hub->ended, "Hub ended");
is_deeply(
$one->state,
{
count => 2,
failed => 1,
is_passing => 0,
plan => 2,
bailed_out => undef,
skip_reason => undef,
follows_plan => 1,
},
"Got the hub state"
);
};
tests upgrade => sub {
my $one = intercept {
require Test::More;
Test::More::ok(1, "pass");
Test::More::ok(1, "pass");
};
ok($one->[0]->isa('Test2::Event::Ok'), "Original event is not upgraded 0");
ok($one->[1]->isa('Test2::Event::Ok'), "Original event is not upgraded 1");
my $two = $one->upgrade;
ok($one->[0]->isa('Test2::Event::Ok'), "Did not modify original 0");
ok($one->[0]->isa('Test2::Event::Ok'), "Did not modify original 1");
ok($two->[0]->isa('Test2::API::InterceptResult::Event'), "Upgraded copy 0");
ok($two->[1]->isa('Test2::API::InterceptResult::Event'), "Upgraded copy 1");
my $three = $two->upgrade;
ok("$two->[0]" ne "$three->[0]", "Upgrade on an already upgraded instance returns copies of the events, not originals");
like(
exception { $one->upgrade() },
qr/Called a method that creates a new instance in void context/,
"Calling upgrade() without keeping the result is a bug"
);
$one->upgrade(in_place => 1);
ok($one->[0]->isa('Test2::API::InterceptResult::Event'), "Upgraded in place 0");
ok($one->[1]->isa('Test2::API::InterceptResult::Event'), "Upgraded in place 1");
};
tests squash_info => sub {
my $one = intercept {
diag "isolated 1";
note "isolated 2";
sub {
my $ctx = context();
diag "inline 1";
note "inline 2";
$ctx->fail;
diag "inline 3";
note "inline 4";
$ctx->release;
}->();
diag "isolated 3";
note "isolated 4";
};
my $new = $one->squash_info;
$one->squash_info(in_place => 1);
is_deeply(
$new,
$one,
"Squash and squash in place produce the same result"
);
is(@$one, 5, "5 events after squash");
is_deeply([$one->[0]->info_messages], ['isolated 1'], "First event not modified");
is_deeply([$one->[1]->info_messages], ['isolated 2'], "Second event not modified");
is_deeply([$one->[3]->info_messages], ['isolated 3'], "second to last event not modified");
is_deeply([$one->[4]->info_messages], ['isolated 4'], "last event not modified");
is_deeply(
[$one->[2]->info_messages],
[
'inline 1',
'inline 2',
'inline 3',
'inline 4',
],
"Assertion collected info generated in the same context"
);
ok($one->[2]->has_assert, "Assertion is still an assertion");
my $two = intercept {
};
};
tests messages => sub {
my $one = intercept {
note "foo";
diag "bar";
ok(1);
sub {
my $ctx = context();
$ctx->send_ev2(
errors => [
{tag => 'error', details => "Error 1" },
{tag => 'error', details => "Error 2" },
],
info => [
{tag => 'DIAG', details => 'Diag 1'},
{tag => 'DIAG', details => 'Diag 2'},
{tag => 'NOTE', details => 'Note 1'},
{tag => 'NOTE', details => 'Note 2'},
],
);
$ctx->release;
}->();
note "baz";
diag "bat";
};
is_deeply(
$one->diag_messages,
['bar', 'Diag 1', 'Diag 2', 'bat'],
"Got diags"
);
is_deeply(
$one->note_messages,
['foo', 'Note 1', 'Note 2', 'baz'],
"Got Notes"
);
is_deeply(
$one->error_messages,
['Error 1', 'Error 2'],
"Got errors"
);
};
tests grep => sub {
my $one = intercept {
ok(1), # 0
note "A Note"; # 1
diag "A Diag"; # 2
tests foo => sub { ok(1) }; # 3
sub { # 4
my $ctx = context();
$ctx->send_ev2(errors => [{tag => 'error', details => "Error 1"}]);
$ctx->release;
}->(); # 4
plan 2; # 5
};
$one->upgrade(in_place => 1);
is_deeply($one->asserts, [$one->[0], $one->[3]], "Got the asserts");
is_deeply($one->subtests, [$one->[3]], "Got the subtests");
is_deeply($one->diags, [$one->[2]], "Got the diags");
is_deeply($one->notes, [$one->[1]], "Got the notes");
is_deeply($one->errors, [$one->[4]], "Got the errors");
is_deeply($one->plans, [$one->[5]], "Got the plans");
$one->asserts(in_place => 1);
is(@$one, 2, "2 events");
ok($_->has_assert, "Is an assert") for @$one;
};
tests map => sub {
my $one = intercept { ok(1); ok(2) };
$one->upgrade(in_place => 1);
is_deeply(
$one->flatten,
[ $one->[0]->flatten, $one->[1]->flatten ],
"Flattened both events"
);
is_deeply(
$one->briefs,
[ $one->[0]->brief, $one->[1]->brief ],
"Brief of both events"
);
is_deeply(
$one->summaries,
[ $one->[0]->summary, $one->[1]->summary ],
"Summaries of both events"
);
my $two = intercept {
tests foo => sub { ok(1) };
ok(1);
tests bar => sub { ok(1) };
}->upgrade;
is_deeply(
$two->subtest_results,
[ $two->[0]->subtest_result, $two->[2]->subtest_result ],
"Got subtest results"
);
};
done_testing;
|