1use strict;
2use warnings;
3use Test2::Tools::Tiny;
4
5use Test2::API qw/context intercept/;
6
7use Test2::Event::V2();
8
9my $CLASS = 'Test2::Event::V2';
10
11ok($CLASS->isa('Test2::Event'), "Subclass of Test2::Event");
12
13is_deeply(
14    [Test2::Event::V2->non_facet_keys],
15    ['uuid', '_meta'],
16    "Got non-facet keys"
17);
18
19ok($CLASS->can($_), "has method $_") for qw{
20    causes_fail diagnostics global increments_count no_display sets_plan
21    subtest_id summary terminate
22    uuid set_uuid
23    meta
24    facet_data
25    about
26};
27
28ok(!exception { $CLASS->new(uuid => 2, about => {uuid => 2}) }, "Can have matching uuids");
29
30like(
31    exception { $CLASS->new(uuid => 1, about => {uuid => 2}) },
32    qr/uuid '1' passed to constructor, but uuid '2' is already set in the 'about' facet/,
33    "Cannot have a uuid mismatch"
34);
35
36my $one = $CLASS->new(uuid => 123);
37is($one->about->{uuid}, 123, "Set uuid in about facet");
38
39$one = $CLASS->new(about => { uuid => 123 });
40is($one->uuid, 123, "set uuid attribute");
41
42my $trace = {frame => ['main', 'file.t', 42, 'foo'], tid => 0, pid => $$};
43$one = $CLASS->new(trace => $trace);
44ok($trace != $one->trace, "Did not keep or modify the original trace ref");
45ok($one->trace->isa('Test2::EventFacet::Trace'), "Blessed the trace");
46is_deeply($one->trace, $trace, "Trace has all data");
47
48$one = $CLASS->new;
49ok(!$one->uuid, "no uuid attribute");
50ok(!($one->about && $one->about->{uuid}), "no uuid in about facet");
51$one->set_uuid(123);
52is($one->about->{uuid}, 123, "Set uuid in about facet");
53is($one->uuid, 123, "set uuid attribute");
54
55
56$one = $CLASS->new(
57    uuid => '123',
58    trace => $trace,
59    assert => {pass => 1, details => 'pass'},
60    info => [{tag => 'NOTE', details => 'a note'}],
61);
62
63$one->set_meta('foo' => {'xyz' => 1});
64
65$one->{_custom_sttr} = 'xxx';
66
67is_deeply(
68    $one->facet_data,
69    {
70        trace  => $trace,
71        assert => {pass => 1, details => 'pass'},
72        info   => [{tag => 'NOTE', details => 'a note'}],
73        meta  => {foo  => {'xyz' => 1}},
74        about => {uuid => 123},
75    },
76    "Facet data has everything we want, and nothing we do not"
77);
78
79sub my_tool {
80    my $ctx = context();
81
82    my $event = $ctx->send_ev2(info => [{tag => 'NOTE', details => "This is a note"}]);
83
84    $ctx->release;
85
86    return $event;
87}
88
89my $events = intercept {
90    my_tool();
91};
92
93is(@$events, 1, "Got 1 event");
94ok($events->[0]->isa($CLASS), "Created the right type of event");
95is_deeply(
96    $events->[0]->facet_data->{info},
97    [{tag => 'NOTE', details => "This is a note"}],
98    "Got the specified info facet"
99);
100
101done_testing;
102