11
11
from pluggy import PluginValidationError
12
12
from pluggy ._hooks import HookCaller
13
13
from pluggy ._hooks import HookImpl
14
+ from pluggy ._hooks import HookimplOpts
14
15
from pluggy ._tracing import saferepr
15
16
16
17
hookspec = HookspecMarker ("example" )
@@ -410,7 +411,8 @@ def hello(self, arg: object) -> None:
410
411
411
412
pm .add_hookspecs (Api )
412
413
413
- # make sure a bad signature still raises an error when using specname
414
+ """make sure a bad signature still raises an error when using specname"""
415
+
414
416
class Plugin :
415
417
@hookimpl (specname = "hello" )
416
418
def foo (self , arg : int , too , many , args ) -> int :
@@ -419,8 +421,9 @@ def foo(self, arg: int, too, many, args) -> int:
419
421
with pytest .raises (PluginValidationError ):
420
422
pm .register (Plugin ())
421
423
422
- # make sure check_pending still fails if specname doesn't have a
423
- # corresponding spec. EVEN if the function name matches one.
424
+ """make sure check_pending still fails if specname doesn't have a
425
+ corresponding spec. EVEN if the function name matches one."""
426
+
424
427
class Plugin2 :
425
428
@hookimpl (specname = "bar" )
426
429
def hello (self , arg : int ) -> int :
@@ -451,14 +454,62 @@ def conflict(self) -> None:
451
454
)
452
455
453
456
454
- def test_hookcaller_repr_with_saferepr_failure (
455
- hc : HookCaller , addmeth : AddMeth
456
- ) -> None :
457
- @addmeth ()
458
- def he_method2 () -> None :
459
- # Intentional error to make the repr fail
460
- raise ValueError ("Intentional error in he_method2" )
461
-
462
- # Verify that HookCaller.repr with saferepr still works despite the error
463
- expected_repr = f"<HookCaller { saferepr (hc .name )} >"
464
- assert repr (hc ) == expected_repr
457
+ def test_hook_impl_initialization () -> None :
458
+ # Mock data
459
+ plugin = "example_plugin"
460
+ plugin_name = "ExamplePlugin"
461
+
462
+ def example_function (x ):
463
+ return x
464
+
465
+ hook_impl_opts : HookimplOpts = {
466
+ "wrapper" : False ,
467
+ "hookwrapper" : False ,
468
+ "optionalhook" : False ,
469
+ "tryfirst" : False ,
470
+ "trylast" : False ,
471
+ "specname" : "" ,
472
+ }
473
+
474
+ # Initialize HookImpl
475
+ hook_impl = HookImpl (plugin , plugin_name , example_function , hook_impl_opts )
476
+
477
+ # Verify attributes are set correctly
478
+ assert hook_impl .function == example_function
479
+ assert hook_impl .argnames == ("x" ,)
480
+ assert hook_impl .kwargnames == ()
481
+ assert hook_impl .plugin == plugin
482
+ assert hook_impl .opts == hook_impl_opts
483
+ assert hook_impl .plugin_name == plugin_name
484
+ assert not hook_impl .wrapper
485
+ assert not hook_impl .hookwrapper
486
+ assert not hook_impl .optionalhook
487
+ assert not hook_impl .tryfirst
488
+ assert not hook_impl .trylast
489
+
490
+
491
+ def test_hook_impl_representation () -> None :
492
+ # Mock data
493
+ plugin = "example_plugin"
494
+ plugin_name = "ExamplePlugin"
495
+
496
+ def example_function (x ):
497
+ return x
498
+
499
+ hook_impl_opts : HookimplOpts = {
500
+ "wrapper" : False ,
501
+ "hookwrapper" : False ,
502
+ "optionalhook" : False ,
503
+ "tryfirst" : False ,
504
+ "trylast" : False ,
505
+ "specname" : "" ,
506
+ }
507
+
508
+ # Initialize HookImpl
509
+ hook_impl = HookImpl (plugin , plugin_name , example_function , hook_impl_opts )
510
+
511
+ # Verify __repr__ method
512
+ expected_repr = (
513
+ f"<HookImpl plugin_name={ saferepr (plugin_name )} , " f"plugin={ saferepr (plugin )} >"
514
+ )
515
+ assert repr (hook_impl ) == expected_repr
0 commit comments