1
1
import html
2
2
import locale
3
3
import re
4
+ import types
4
5
from contextlib import closing
5
6
from io import StringIO
6
7
from unittest .mock import patch
@@ -443,7 +444,9 @@ def test_html_documentation_link_mixin_sklearn(mock_version):
443
444
("prefix.mypackage.mymodule.submodule" , "prefix.mypackage.mymodule.submodule" ),
444
445
],
445
446
)
446
- def test_html_documentation_link_mixin_get_doc_link (module_path , expected_module ):
447
+ def test_html_documentation_link_mixin_get_doc_link_instance (
448
+ module_path , expected_module
449
+ ):
447
450
"""Check the behaviour of the `_get_doc_link` with various parameter."""
448
451
449
452
class FooBar (_HTMLDocumentationLinkMixin ):
@@ -459,6 +462,32 @@ class FooBar(_HTMLDocumentationLinkMixin):
459
462
assert est ._get_doc_link () == f"https://website.com/{ expected_module } .FooBar.html"
460
463
461
464
465
+ @pytest .mark .parametrize (
466
+ "module_path,expected_module" ,
467
+ [
468
+ ("prefix.mymodule" , "prefix.mymodule" ),
469
+ ("prefix._mymodule" , "prefix" ),
470
+ ("prefix.mypackage._mymodule" , "prefix.mypackage" ),
471
+ ("prefix.mypackage._mymodule.submodule" , "prefix.mypackage" ),
472
+ ("prefix.mypackage.mymodule.submodule" , "prefix.mypackage.mymodule.submodule" ),
473
+ ],
474
+ )
475
+ def test_html_documentation_link_mixin_get_doc_link_class (module_path , expected_module ):
476
+ """Check the behaviour of the `_get_doc_link` when `_doc_link_module` and
477
+ `_doc_link_template` are defined at the class level and not at the instance
478
+ level."""
479
+
480
+ class FooBar (_HTMLDocumentationLinkMixin ):
481
+ _doc_link_module = "prefix"
482
+ _doc_link_template = (
483
+ "https://website.com/{estimator_module}.{estimator_name}.html"
484
+ )
485
+
486
+ FooBar .__module__ = module_path
487
+ est = FooBar ()
488
+ assert est ._get_doc_link () == f"https://website.com/{ expected_module } .FooBar.html"
489
+
490
+
462
491
def test_html_documentation_link_mixin_get_doc_link_out_of_library ():
463
492
"""Check the behaviour of the `_get_doc_link` with various parameter."""
464
493
mixin = _HTMLDocumentationLinkMixin ()
@@ -469,7 +498,7 @@ def test_html_documentation_link_mixin_get_doc_link_out_of_library():
469
498
assert mixin ._get_doc_link () == ""
470
499
471
500
472
- def test_html_documentation_link_mixin_doc_link_url_param_generator ():
501
+ def test_html_documentation_link_mixin_doc_link_url_param_generator_instance ():
473
502
mixin = _HTMLDocumentationLinkMixin ()
474
503
# we can bypass the generation by providing our own callable
475
504
mixin ._doc_link_template = (
@@ -482,11 +511,30 @@ def url_param_generator(estimator):
482
511
"another_variable" : "value_2" ,
483
512
}
484
513
485
- mixin ._doc_link_url_param_generator = url_param_generator
514
+ mixin ._doc_link_url_param_generator = types . MethodType ( url_param_generator , mixin )
486
515
487
516
assert mixin ._get_doc_link () == "https://website.com/value_1.value_2.html"
488
517
489
518
519
+ def test_html_documentation_link_mixin_doc_link_url_param_generator_class ():
520
+ # we can bypass the generation by providing our own callable
521
+
522
+ def url_param_generator (estimator ):
523
+ return {
524
+ "my_own_variable" : "value_1" ,
525
+ "another_variable" : "value_2" ,
526
+ }
527
+
528
+ class FooBar (_HTMLDocumentationLinkMixin ):
529
+ _doc_link_template = (
530
+ "https://website.com/{my_own_variable}.{another_variable}.html"
531
+ )
532
+ _doc_link_url_param_generator = url_param_generator
533
+
534
+ estimator = FooBar ()
535
+ assert estimator ._get_doc_link () == "https://website.com/value_1.value_2.html"
536
+
537
+
490
538
@pytest .fixture
491
539
def set_non_utf8_locale ():
492
540
"""Pytest fixture to set non utf-8 locale during the test.
0 commit comments