forked from beeware/toga
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import sys | ||
|
||
import pytest | ||
|
||
|
||
def test_lazy_succeed(monkeypatch): | ||
"""Submodules are imported on demand.""" | ||
for mod_name in ["toga", "toga.documents", "toga.widgets.button"]: | ||
monkeypatch.delitem(sys.modules, mod_name, raising=False) | ||
|
||
# A clean import of the top-level toga module should not import any submodules. | ||
import toga | ||
|
||
assert "toga.documents" not in sys.modules | ||
assert "toga.widgets.button" not in sys.modules | ||
|
||
# Accessing a name should import only the necessary submodules. | ||
Button = toga.Button | ||
assert "toga.widgets.button" in sys.modules | ||
assert "toga.documents" not in sys.modules | ||
|
||
# Accessing a name multiple times should return the same object. | ||
assert Button is toga.Button | ||
assert Button is sys.modules["toga.widgets.button"].Button | ||
|
||
# Same again with a different module. | ||
Document = toga.Document | ||
assert Document is sys.modules["toga.documents"].Document | ||
|
||
|
||
def test_lazy_fail(): | ||
"""Nonexistent names should raise a normal AttributeError.""" | ||
import toga | ||
|
||
with pytest.raises( | ||
AttributeError, match="module 'toga' has no attribute 'nonexistent'" | ||
): | ||
toga.nonexistent |