Skip to content

Commit

Permalink
Merge pull request #13 from tmcw/add-tests
Browse files Browse the repository at this point in the history
Reorganize, add tests
  • Loading branch information
tmcw authored Dec 15, 2024
2 parents 259d6dd + a514791 commit 22c9bc0
Show file tree
Hide file tree
Showing 46 changed files with 150 additions and 42 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.12
uses: actions/setup-python@v4
with:
python-version: "3.12"
- uses: astral-sh/setup-uv@v1
with:
version: "latest"
- name: Install dependencies
run: uv sync -p 3.12
- name: pytest
run: uv run pytest
- name: lint
run: uv run ruff check indiepixel tests
- name: format
run: uv run ruff format --check indiepixel tests
47 changes: 44 additions & 3 deletions src/indiepixel/__init__.py → indiepixel/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PIL import ImageDraw, ImageFont, ImageColor
from PIL import ImageDraw, ImageFont, ImageColor
from os import path
import glob
from typing import TypedDict
Expand All @@ -10,12 +10,18 @@

fonts = {}

mypath = path.abspath(path.dirname(__file__))


def relpath(p):
return path.join(mypath, p)


def initialize_fonts():
files = glob.glob("./fonts/*.pil")
files = glob.glob(relpath("./fonts/*.pil"))
for file in files:
name, ext = path.splitext(path.basename(file))
fonts[name] = ImageFont.load(file)
fonts[name] = ImageFont.load(relpath(file))


initialize_fonts()
Expand All @@ -40,6 +46,41 @@ def measure(self, bounds: Bounds) -> tuple[int, int]:
pass


# https://github.com/tidbyt/pixlet/blob/main/docs/widgets.md#root
class RootParams(TypedDict):
child: Renderable
max_age: NotRequired[str]
show_full_application: NotRequired[bool]


class Root(Renderable):
"""The root of the widget tree."""

delay = 0
max_age = 0
show_full_application = False

def __init__(self, **kwargs: Unpack[RootParams]):
self.child = kwargs.get("child")
self.delay = kwargs.get("delay", 100)
self.max_age = kwargs.get("max_age", 100)
self.show_full_application = kwargs.get("show_full_application", False)

def measure(self, bounds: Bounds):
return (64, 32)

def paint(self, draw: ImageDraw.ImageDraw, bounds: Bounds):
self.child.paint(
draw,
(
0,
0,
64,
32,
),
)


class RectParams(TypedDict):
"""A color, anything parseable by ImageColor.getrgb"""

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 7 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ dependencies = [
"flask>=3.1.0",
"pytz>=2024.2",
"gunicorn>=23.0.0",
"typing-extensions>=4.12.2",
"mypy>=1.13.0",
"ruff>=0.8.2",
"typing-extensions>=4.12.2"
]
[project.urls]
Homepage = "https://github.com/tmcw/indiepixel"
Expand All @@ -28,3 +26,9 @@ Issues = "https://github.com/tmcw/indiepixel/issues"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[dependency-groups]
dev = [
"ruff>=0.8.2",
"pytest>=8.3.4",
]
Empty file added tests/__init__.py
Empty file.
Empty file added tests/unit/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions tests/unit/test_widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from __future__ import annotations
from indiepixel import Rect, Text, Box, Root


def test_rect():
r = Rect(width=10, height=10, background="#f00")
assert r.measure((0, 0, 100, 100)) == (10, 10)


def test_text():
t = Text(text="Hello world")
assert t.measure((0, 0, 100, 100)) == (51, 8)


def test_box():
t = Text(text="Hello world")
b = Box(t)
# TODO: this is probably wrong, this should
# be the same as the text that is contained!
assert b.measure((0, 0, 100, 100)) == (52, 9)


def test_root():
r = Root(child=Rect(width=10, height=10, background="#000"))
assert r.measure((0, 0, 64, 32)) == (64, 32)
81 changes: 45 additions & 36 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 22c9bc0

Please sign in to comment.