DoesTesting.pm 2.4 KB

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