DoesTesting.pm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package Test::Selenium::Remote::Role::DoesTesting;
  2. # ABSTRACT: Role to cope with everything that is related to testing (could
  3. # be reused in both testing classes)
  4. use Moo::Role;
  5. use Test::Builder;
  6. use Try::Tiny;
  7. use namespace::clean;
  8. requires qw(func_list has_args);
  9. has _builder => (
  10. is => 'lazy',
  11. builder => sub { return Test::Builder->new() },
  12. handles => [qw/is_eq isnt_eq like unlike ok croak/],
  13. );
  14. # main method for non ok tests
  15. sub _check_method {
  16. my $self = shift;
  17. my $method = shift;
  18. my $method_to_test = shift;
  19. $method = "get_$method";
  20. my @args = @_;
  21. my $rv;
  22. try {
  23. my $num_of_args = $self->has_args($method);
  24. my @r_args = splice( @args, 0, $num_of_args );
  25. $rv = $self->$method(@r_args);
  26. }
  27. catch {
  28. $self->croak($_);
  29. };
  30. return $self->$method_to_test( $rv, @args );
  31. }
  32. # main method for _ok tests
  33. sub _check_ok {
  34. my $self = shift;
  35. my $method = shift;
  36. my @args = @_;
  37. my ($rv, $num_of_args, @r_args);
  38. try {
  39. $num_of_args = $self->has_args($method);
  40. @r_args = splice( @args, 0, $num_of_args );
  41. $rv = $self->$method(@r_args);
  42. }
  43. catch {
  44. $self->croak($_);
  45. };
  46. my $default_test_name = $method;
  47. $default_test_name .= join(" ", @r_args) if $num_of_args > 0;
  48. my $test_name = pop @args // $default_test_name;
  49. return $self->ok( $rv, $test_name);
  50. }
  51. # build the subs with the correct arg set
  52. sub _build_sub {
  53. my $self = shift;
  54. my $meth_name = shift;
  55. my @func_args;
  56. my $comparators = {
  57. is => 'is_eq',
  58. isnt => 'isnt_eq',
  59. like => 'like',
  60. unlike => 'unlike',
  61. };
  62. my @meth_elements = split( '_', $meth_name );
  63. my $meth = '_check_ok';
  64. my $meth_comp = pop @meth_elements;
  65. if ( $meth_comp eq 'ok' ) {
  66. push @func_args, join( '_', @meth_elements );
  67. }
  68. else {
  69. if ( defined( $comparators->{$meth_comp} ) ) {
  70. $meth = '_check_method';
  71. push @func_args, join( '_', @meth_elements ),
  72. $comparators->{$meth_comp};
  73. }
  74. else {
  75. return sub {
  76. my $self = shift;
  77. $self->croak("Sub $meth_name could not be defined");
  78. }
  79. }
  80. }
  81. return sub {
  82. my $self = shift;
  83. local $Test::Builder::Level = $Test::Builder::Level + 2;
  84. $self->$meth( @func_args, @_ );
  85. };
  86. }
  87. 1;
  88. =head1 NAME
  89. Selenium::Remote::Role::DoesTesting - Role implementing the common logic used for testing
  90. =cut