Skip to content

Commit

Permalink
fix(combine): the usage method should not overwrite with defaults (#26)
Browse files Browse the repository at this point in the history
* test(combine): test that parameters are not overwritten

* revert(combine): use current default value for fontsize
  • Loading branch information
engeir authored May 2, 2024
1 parent fa83522 commit e146ffe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
33 changes: 18 additions & 15 deletions cosmoplots/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,39 @@ def combine(self, *files: str | pathlib.Path) -> Combine:

def using(
self,
gravity: str = "northwest",
pos: tuple[float, float] = (10.0, 10.0),
font: str = "Times-New-Roman",
fontsize: int = 100,
color: str = "black",
*,
gravity: str | None = None,
pos: tuple[float, float] | None = None,
font: str | None = None,
fontsize: int | None = None,
color: str | None = None,
) -> Combine:
"""Set text properties.
The properties must be given as keyword arguments to take effect.
Parameters
----------
gravity : str
gravity : str, optional
Where the position of the text is relative to in the subfigure. Default is
`northwest`. Possible values are `north`, `northeast`, `northwest`, `south`,
`southeast`, `southwest`, `west`, `east` and `center`.
pos : tuple[float, float]
pos : tuple[float, float], optional
The position in the subfigure relative to `gravity`. Default is `(10.0,
10.0)`.
font : str
font : str, optional
The type of font to use, default is Times New Roman. See `convert -list
font` for a list of available fonts.
fontsize : int
fontsize : int, optional
The size of the font in pointsize. Default is `100`.
color : str
color : str, optional
The color of the text. Default is `black`.
"""
self._gravity = gravity
self._fontsize = fontsize
self._pos = pos
self._font = font
self._color = color
self._gravity = gravity or self._gravity
self._fontsize = fontsize or self._fontsize
self._pos = pos or self._pos
self._font = font or self._font
self._color = color or self._color
return self

def in_grid(self, w: int, h: int) -> Combine:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def _combine() -> None:
first_img = tmp_path / "out.jpg"
assert first_img.exists()


def test_in_grid_not_specified(tmp_path: pathlib.Path) -> None:
"""Test error when `in_grid` has not been called."""

Expand Down Expand Up @@ -153,6 +154,13 @@ def test_output_not_found(tmp_path: pathlib.Path) -> None:
).in_grid(w=2, h=2).save(tmp_path / "second_level" / "out.png")


def test_using_update() -> None:
"""Test that the `using` method can be called multiple times."""
c = cosmoplots.Combine().using(fontsize=12)
c.using(gravity="southwest")
assert c._fontsize == 12


def test_input_not_found() -> None:
"""Test the input files."""
with pytest.raises(FileNotFoundError):
Expand Down

0 comments on commit e146ffe

Please sign in to comment.