Playwright-Base.t 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. spec => $Playwright::spec->{$options{type}}{members},
  42. type => $options{type},
  43. guid => $options{id},
  44. ua => $options{handle}{ua},
  45. port => $options{handle}{port},
  46. }, $class);
  47. };
  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->{Fake}{members}, "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}, %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();