Playwright-Base.t 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. use Test2::V0 -target => 'Playwright::Base';
  2. use Test2::Tools::Explain;
  3. use Playwright::Base;
  4. use JSON;
  5. use Test::MockModule qw{strict};
  6. local $Playwright::spec = {
  7. Fake => {
  8. members => {
  9. tickle => {
  10. args => {
  11. chase => { type => { name => 'boolean' }, order => 1 },
  12. tickleOptions => {
  13. order => 0,
  14. type => {
  15. name => 'Object',
  16. properties => {
  17. intense => { name => 'intense', type => { name => 'boolean' } },
  18. tickler => { name => 'tickler', type => { name => 'string' } },
  19. optional => { name => 'optional', type => { name => 'boolean' } }, # Optional, shouldn't show up in output
  20. },
  21. },
  22. },
  23. who => { type => { name => 'string' }, order => 2 },
  24. hug => { type => { name => 'boolean' }, order => 3 }, # Optional bool arg, make sure we dont choke
  25. },
  26. },
  27. }
  28. },
  29. };
  30. local %Playwright::mapper = (
  31. Fake => sub {
  32. my ($self, $res) = @_;
  33. my $class = "Playwright::Fake";
  34. return $class->new( handle => $self, id => $res->{_guid}, type => 'Fake', spec => $Playwright::spec->{Fake} );
  35. },
  36. );
  37. no warnings qw{redefine once};
  38. local *Playwright::Fake::new = sub {
  39. my ($class,%options) = @_;
  40. return bless( {
  41. type => $options{type},
  42. guid => $options{id},
  43. ua => $options{handle}{ua},
  44. port => $options{handle}{port},
  45. }, $class);
  46. };
  47. local *Playwright::Fake::spec = sub { return $Playwright::spec->{Fake}{members} };
  48. use warnings;
  49. my $obj = CLASS()->new(
  50. type => 'Fake',
  51. id => 666,
  52. handle => { ua => 'bogus', port => 420 },
  53. );
  54. is($obj->spec, $Playwright::spec, "Spec correctly filed by constructor");
  55. my %in = (
  56. command => 'tickle',
  57. type => 'Fake',
  58. args => [{ intense => 1, tickler => 'bigtime' },0, 'boom'],
  59. );
  60. my %expected = (
  61. command => 'tickle',
  62. type => 'Fake',
  63. args => [{ intense => JSON::true, tickler => 'bigtime' }, JSON::false, 'boom'],
  64. );
  65. my %out = Playwright::Base::_coerce($obj->spec->{Fake}{members}, %in);
  66. is(\%out, \%expected, "_coerce correctly transforms bools and leaves everything else alone");
  67. my $result = { error => JSON::true, message => "U suck" };
  68. my $utilmock = Test::MockModule->new('Playwright::Util');
  69. $utilmock->redefine('request', sub {
  70. return $result;
  71. });
  72. is( $obj->_api_request(%in), $result, "Data directly returned when no _type or _guid");
  73. $result = { _guid => 666, _type => 'Fake' };
  74. my $exp_obj = Playwright::Fake->new(
  75. id => 666,
  76. type => 'Fake',
  77. spec => $Playwright::spec->{Fake}{members},
  78. handle => $obj
  79. );
  80. my $oot = $obj->_api_request(%in);
  81. is( $oot, $exp_obj, "Object returned when _type or _guid returned");
  82. done_testing();