-
Notifications
You must be signed in to change notification settings - Fork 827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Layout Resolver enhancement #295
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
76c5d3a
optimized layout_resolve added tests
willmcgugan ca009d8
removed edge defaults
willmcgugan 4224f59
allow tests to use new annotations
willmcgugan 68d9a66
stringify cast
willmcgugan c2e327c
simplify check, add test
willmcgugan d628b30
extra test
willmcgugan 1b50d77
comments
willmcgugan 0648549
optimization, more tests
willmcgugan 3bcabcf
fix
willmcgugan 61dc149
remote micro-optimization
willmcgugan dfa77d2
comments
willmcgugan d4c1258
list expressions are faster
willmcgugan 140ff00
docstrings
willmcgugan b23edf2
Testing layout resolve zero width, and empty list cases
darrenburns 78f8a3e
Update src/textual/_layout_resolve.py
willmcgugan 50f9aea
Merge branch 'css' into resolve
willmcgugan 4638541
Change tuple to dataclass
willmcgugan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,100 @@ | ||
from __future__ import annotations | ||
|
||
from typing import NamedTuple | ||
|
||
import pytest | ||
from textual._layout_resolve import layout_resolve | ||
|
||
|
||
class Edge(NamedTuple): | ||
size: int | None = None | ||
fraction: int = 1 | ||
min_size: int = 1 | ||
|
||
|
||
def test_empty(): | ||
assert layout_resolve(10, []) == [] | ||
|
||
|
||
def test_total_zero(): | ||
assert layout_resolve(0, [Edge(10)]) == [10] | ||
|
||
|
||
def test_single(): | ||
# One edge fixed size | ||
assert layout_resolve(100, [Edge(10)]) == [10] | ||
# One edge fraction of 1 | ||
assert layout_resolve(100, [Edge(None, 1)]) == [100] | ||
# One edge fraction 3 | ||
assert layout_resolve(100, [Edge(None, 2)]) == [100] | ||
# One edge, fraction1, min size 20 | ||
assert layout_resolve(100, [Edge(None, 1, 20)]) == [100] | ||
# One edge fraction 1, min size 120 | ||
assert layout_resolve(100, [Edge(None, 1, 120)]) == [120] | ||
|
||
|
||
def test_two(): | ||
# Two edges fixed size | ||
assert layout_resolve(100, [Edge(10), Edge(20)]) == [10, 20] | ||
# Two edges, fixed size of one exceeds total | ||
assert layout_resolve(100, [Edge(120), Edge(None, 1)]) == [120, 1] | ||
# Two edges, fraction 1 each | ||
assert layout_resolve(100, [Edge(None, 1), Edge(None, 1)]) == [50, 50] | ||
# Two edges, one with fraction 2, one with fraction 1 | ||
# Note first value is rounded down, second is rounded up | ||
assert layout_resolve(100, [Edge(None, 2), Edge(None, 1)]) == [66, 34] | ||
# Two edges, both with fraction 2 | ||
assert layout_resolve(100, [Edge(None, 2), Edge(None, 2)]) == [50, 50] | ||
# Two edges, one with fraction 3, one with fraction 1 | ||
assert layout_resolve(100, [Edge(None, 3), Edge(None, 1)]) == [75, 25] | ||
# Two edges, one with fraction 3, one with fraction 1, second with min size of 30 | ||
assert layout_resolve(100, [Edge(None, 3), Edge(None, 1, 30)]) == [70, 30] | ||
# Two edges, one with fraction 1 and min size 30, one with fraction 3 | ||
assert layout_resolve(100, [Edge(None, 1, 30), Edge(None, 3)]) == [30, 70] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"size, edges, result", | ||
[ | ||
(10, [Edge(8), Edge(None, 0, 2), Edge(4)], [8, 2, 4]), | ||
(10, [Edge(None, 1), Edge(None, 1), Edge(None, 1)], [3, 3, 4]), | ||
(10, [Edge(5), Edge(None, 1), Edge(None, 1)], [5, 2, 3]), | ||
(10, [Edge(None, 2), Edge(None, 1), Edge(None, 1)], [5, 2, 3]), | ||
(10, [Edge(None, 2), Edge(3), Edge(None, 1)], [4, 3, 3]), | ||
( | ||
10, | ||
[Edge(None, 2), Edge(None, 1), Edge(None, 1), Edge(None, 1)], | ||
[4, 2, 2, 2], | ||
), | ||
( | ||
10, | ||
[Edge(None, 4), Edge(None, 1), Edge(None, 1), Edge(None, 1)], | ||
[5, 2, 1, 2], | ||
), | ||
(2, [Edge(None, 1), Edge(None, 1), Edge(None, 1)], [1, 1, 1]), | ||
( | ||
2, | ||
[ | ||
Edge(None, 1, min_size=5), | ||
Edge(None, 1, min_size=4), | ||
Edge(None, 1, min_size=3), | ||
], | ||
[5, 4, 3], | ||
), | ||
( | ||
18, | ||
[ | ||
Edge(None, 1, min_size=1), | ||
Edge(3), | ||
Edge(None, 1, min_size=1), | ||
Edge(4), | ||
Edge(None, 1, min_size=1), | ||
Edge(5), | ||
Edge(None, 1, min_size=1), | ||
], | ||
[1, 3, 2, 4, 1, 5, 2], | ||
), | ||
], | ||
) | ||
def test_multiple(size, edges, result): | ||
assert layout_resolve(size, edges) == result |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how this loop turned out..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me too :)