Playwright.t 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. use Test2::V0;
  2. use Test2::Tools::Explain;
  3. use JSON::MaybeXS;
  4. use Test::MockModule qw{strict};
  5. use Test::MockFile;
  6. use Test::Fatal qw{exception};
  7. my ($qxret,$qxcode) = ('',255);
  8. use Test::Mock::Cmd qx => sub { $? = $qxcode; return $qxret }, system => sub { print $qxret };
  9. #De-Fang our BEGIN block so we can test safely
  10. no warnings qw{redefine once};
  11. $Playwright::SKIP_BEGIN = 1;
  12. use warnings;
  13. require Playwright;
  14. my $path2here = File::Basename::dirname(Cwd::abs_path($INC{'Playwright.pm'}));
  15. subtest "_check_and_build_spec" => sub {
  16. local $Playwright::spec = {};
  17. is(Playwright::_check_and_build_spec({}),{},"Already defined spec short-circuits");
  18. my $utilmock = Test::MockModule->new('Playwright::Util');
  19. $utilmock->redefine('request', sub { 'eee' });
  20. undef $Playwright::spec;
  21. like(exception { Playwright::_check_and_build_spec({ ua => 'eeep', port => 666} ) },qr/Could not retrieve/,"Fetch explodes when playwright_server doesn't have spec");
  22. };
  23. subtest "_build_classes" => sub {
  24. local $Playwright::spec = {
  25. Fake => {
  26. members => {
  27. tickle => {
  28. args => {
  29. chase => { type => { name => 'boolean' }, order => 1 },
  30. tickleOptions => {
  31. order => 0,
  32. type => {
  33. name => 'Object',
  34. properties => {
  35. intense => { name => 'intense', type => { name => 'boolean' } },
  36. tickler => { name => 'tickler', type => { name => 'string' } },
  37. optional => { name => 'optional', type => { name => 'boolean' } }, # Optional, shouldn't show up in output
  38. },
  39. },
  40. },
  41. who => { type => { name => 'string' }, order => 2 },
  42. hug => { type => { name => 'boolean' }, order => 3 }, # Optional bool arg, make sure we dont choke
  43. },
  44. },
  45. }
  46. },
  47. };
  48. #Very light testing here, example.pl is really what tests this
  49. Playwright::_build_classes();
  50. ok(defined &Playwright::Fake::new, "Constructors set up correctly");
  51. ok(defined &Playwright::Fake::tickle, "Class methods set up correctly");
  52. };
  53. subtest "_check_node" => sub {
  54. my $which = Test::MockModule->new('File::Which');
  55. my %to_return = (
  56. node => '/bogus',
  57. npm => '/hokum',
  58. playwright_server => "$path2here/../bin/playwright_server",
  59. );
  60. $which->redefine('which', sub { my $to = shift; $to_return{$to} } );
  61. my $node = Test::MockFile->file('/bogus', undef, { mode => 0777 } );
  62. my $npm = Test::MockFile->file('/hokum', undef, { mode => 0777 } );
  63. like( dies { Playwright::_check_node() }, qr/node must exist/i, "node not existing throws");
  64. undef $node;
  65. $node = Test::MockFile->file('/bogus', '', { mode => 0777 } );
  66. my $bin = Test::MockFile->file("$path2here/../bin/playwright_server");
  67. like( dies { Playwright::_check_node() }, qr/locate playwright_server/i, "Server not existing throws");
  68. undef $bin;
  69. $bin = Test::MockFile->file("$path2here/../bin/playwright_server",'', { mode => 0777 } );
  70. like( dies { Playwright::_check_node() }, qr/could not run/i, "Server exploding throws");
  71. #XXX for some reason I can't redefine this correctly
  72. #my $fakecapture = Test::MockModule->new('Capture::Tiny');
  73. #$fakecapture->redefine('capture_merged', sub { 'OK' });
  74. #ok( lives { Playwright::_check_node() }, "Can run all the way thru") or note $@;
  75. };
  76. subtest "new" => sub {
  77. my $portmock = Test::MockModule->new('Net::EmptyPort');
  78. $portmock->redefine('empty_port', sub { 420 });
  79. my $lwpmock = Test::MockModule->new('LWP::UserAgent');
  80. $lwpmock->redefine('new', sub { bless({},'LWP::UserAgent') });
  81. $lwpmock->redefine('request', sub {});
  82. my $selfmock = Test::MockModule->new('Playwright');
  83. $selfmock->redefine('_start_server', sub { 666 });
  84. $selfmock->redefine('_check_and_build_spec', sub {});
  85. $selfmock->redefine('_build_classes',sub {});
  86. $selfmock->redefine('DESTROY', sub {});
  87. my $expected = bless({
  88. ua => 'whee',
  89. debug => 1,
  90. parent => $$,
  91. pid => 666,
  92. port => 420,
  93. timeout => 5,
  94. }, 'Playwright');
  95. is(Playwright->new( timeout => 5, ua => 'whee', debug => 1), $expected, "Constructor functions as expected");
  96. $expected = bless({
  97. ua => bless({},'LWP::UserAgent'),
  98. debug => undef,
  99. parent => $$,
  100. pid => 666,
  101. port => 420,
  102. timeout => 30,
  103. }, 'Playwright');
  104. is(Playwright->new(), $expected, "Constructor defaults expected");
  105. };
  106. subtest "launch" => sub {
  107. my $basemock = Test::MockModule->new('Playwright::Base');
  108. $basemock->redefine('_coerce', sub {});
  109. my $utilmock = Test::MockModule->new('Playwright::Util');
  110. $utilmock->redefine('request', sub { 'eee' });
  111. my $selfmock = Test::MockModule->new('Playwright');
  112. $selfmock->redefine('DESTROY', sub {});
  113. my $obj = bless({}, 'Playwright');
  114. is($obj->launch( type => 'eee' ), 'eee' ,"launch passthru works");
  115. #XXX Don't feel like mocking the objectification right now
  116. };
  117. subtest "await" => sub {
  118. my $selfmock = Test::MockModule->new('Playwright');
  119. $selfmock->redefine('DESTROY', sub {});
  120. my $res = {};
  121. my $utilmock = Test::MockModule->new('Playwright::Util');
  122. $utilmock->redefine('await', sub { $res } );
  123. my $promise = { file => 'foo.out', pid => 666 };
  124. my $obj = bless({ ua => 'eee', 'port' => 1 }, 'Playwright');
  125. no warnings qw{redefine once};
  126. local *Playwright::Bogus::new = sub { my ($class, %input) = @_; return bless({ spec => 'whee', ua => $input{handle}{ua}, port => $input{handle}{port}, type => $input{type}, guid => $input{id} }, 'Playwright::Bogus') };
  127. use warnings;
  128. is($obj->await($promise), {},"await passthru works");
  129. $res = { _guid => 'abc123', _type => 'Bogus' };
  130. my $expected = bless({ spec => 'whee', ua => 'eee', port => 1, guid => 'abc123', type => 'Bogus' }, 'Bogus');
  131. is($obj->await($promise),$expected,"await objectification works");
  132. };
  133. #XXX Omitting destructor and server startup testing for now
  134. done_testing();