Explorar o código

Coerce the on_error callback to a code ref

Daniel Gempesaw %!s(int64=10) %!d(string=hai) anos
pai
achega
5d13e082a4
Modificáronse 2 ficheiros con 15 adicións e 3 borrados
  1. 13 3
      lib/Selenium/Remote/Driver.pm
  2. 2 0
      t/01-driver.t

+ 13 - 3
lib/Selenium/Remote/Driver.pm

@@ -374,10 +374,20 @@ has 'remote_conn' => (
     },
 );
 
-has 'on_error' => ( 
-    is => 'rw', 
-    predicate => 1,
+has 'on_error' => (
+    is => 'rw',
+    coerce => sub {
+        my ($maybe_coderef) = @_;
+
+        if ( ref($maybe_coderef) eq 'CODE' ) {
+            return $maybe_coderef;
+        }
+        else {
+            croak 'The error handler must be a code ref.';
+        }
+    },
     clearer => 1,
+    predicate => 1
 );
 
 has 'ua' => (

+ 2 - 0
t/01-driver.t

@@ -522,6 +522,8 @@ ERROR: {
     like( exception { $driver->find_element("somethingthatdoesnotexist") }, qr/^Got message:/, "Error handler catches correctly an error");
     $driver->clear_on_error;
     unlike( exception { $driver->find_element("somethingthatdoesnotexist") }, qr/^Got message:/, "Error handler was correctly cleared");
+
+    like( exception { $driver->on_error( 'hello' ) }, qr/must be a code ref/, 'we only accept code refs as error handlers');
 }
 
 QUIT: {