diff options
author | Leonard Richardson <leonardr@segfault.org> | 2023-02-04 08:06:47 -0500 |
---|---|---|
committer | Leonard Richardson <leonardr@segfault.org> | 2023-02-04 08:06:47 -0500 |
commit | 6d70cafddd4a265feec5a30cc5b302fd6fbaeb83 (patch) | |
tree | 5fab35bf1313596836de4f73a18e22cc7c244663 /bs4/tests/test_css.py | |
parent | 526fcf8d98ccc457d5943c83ab133fe49c174d1e (diff) |
Added a __getattr__ fallback.
Diffstat (limited to 'bs4/tests/test_css.py')
-rw-r--r-- | bs4/tests/test_css.py | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/bs4/tests/test_css.py b/bs4/tests/test_css.py index 51662ed..a6c17de 100644 --- a/bs4/tests/test_css.py +++ b/bs4/tests/test_css.py @@ -1,5 +1,6 @@ import pytest import types +from unittest.mock import MagicMock from bs4 import ( CSS, @@ -469,7 +470,29 @@ class TestCSSSelectors(SoupTest): assert result['id'] == 'header3' def test_escape(self): - m = CSS.escape + m = self.soup.css.escape assert m(".foo#bar") == '\\.foo\\#bar' assert m("()[]{}") == '\\(\\)\\[\\]\\{\\}' assert m(".foo") == self.soup.css.escape(".foo") + + def test_fallback(self): + class Mock(): + attribute = "value" + pass + mock_soupsieve = Mock() + mock_soupsieve.some_other_method = MagicMock() + + # If an unknown method turns out to be present in Soup Sieve, + # we may still be able to call it. + css = CSS(self.soup, api=mock_soupsieve) + css.some_other_method("selector", 1, flags=0) + mock_soupsieve.some_other_method.assert_called_with( + "selector", self.soup, 1, flags=0 + ) + + # If the attribute is not callable, getattr is a passthrough. + assert mock_soupsieve.attribute == "value" + + # If the method just isn't there, too bad. + with pytest.raises(AttributeError): + mock_soupsieve.no_such_method() |