DoesTesting.pm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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) . "'"
  48. if $num_of_args > 0;
  49. my $test_name = pop @args // $default_test_name;
  50. return $self->ok( $rv, $test_name);
  51. }
  52. # build the subs with the correct arg set
  53. sub _build_sub {
  54. my $self = shift;
  55. my $meth_name = shift;
  56. my @func_args;
  57. my $comparators = {
  58. is => 'is_eq',
  59. isnt => 'isnt_eq',
  60. like => 'like',
  61. unlike => 'unlike',
  62. };
  63. my @meth_elements = split( '_', $meth_name );
  64. my $meth = '_check_ok';
  65. my $meth_comp = pop @meth_elements;
  66. if ( $meth_comp eq 'ok' ) {
  67. push @func_args, join( '_', @meth_elements );
  68. }
  69. else {
  70. if ( defined( $comparators->{$meth_comp} ) ) {
  71. $meth = '_check_method';
  72. push @func_args, join( '_', @meth_elements ),
  73. $comparators->{$meth_comp};
  74. }
  75. else {
  76. return sub {
  77. my $self = shift;
  78. $self->croak("Sub $meth_name could not be defined");
  79. }
  80. }
  81. }
  82. return sub {
  83. my $self = shift;
  84. local $Test::Builder::Level = $Test::Builder::Level + 2;
  85. $self->$meth( @func_args, @_ );
  86. };
  87. }
  88. 1;
  89. =head1 NAME
  90. Selenium::Remote::Role::DoesTesting - Role implementing the common logic used for testing
  91. =cut