Skip to content

Commit e931108

Browse files
Merge pull request #1 from davidbrochart/tests
Add test
2 parents 166fb87 + 7fbb0e6 commit e931108

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

.github/workflows/test.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
test:
17+
name: Test
18+
runs-on: ${{ matrix.os }}
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
os: [ubuntu-latest, macos-latest, windows-latest]
23+
python-version: [ '3.8', '3.9', '3.10', '3.11' ]
24+
25+
steps:
26+
- name: Checkout
27+
uses: actions/checkout@v3
28+
29+
- uses: actions/setup-python@v4
30+
with:
31+
python-version: ${{ matrix.python-version }}
32+
33+
- name: Upgrade pip
34+
run: python3 -m pip install --upgrade pip
35+
36+
- name: Install ypywidgets in dev mode
37+
run: |
38+
pip install .[test]
39+
40+
- name: Run tests
41+
run: |
42+
pytest ./tests -v

pyproject.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dynamic = ["version"]
88
description = "Attributes with superpowers"
99
readme = "README.md"
1010
license = "MIT"
11-
requires-python = ">=3.7"
11+
requires-python = ">=3.8"
1212
authors = [
1313
{ name = "David Brochart", email = "[email protected]" },
1414
]
@@ -19,6 +19,11 @@ keywords = [
1919
[project.urls]
2020
Homepage = "https://github.com/davidbrochart/reacttrs"
2121

22+
[project.optional-dependencies]
23+
test = [
24+
"pytest",
25+
]
26+
2227
[tool.hatch.version]
2328
path = "reacttrs/__init__.py"
2429

tests/test_reactive.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from textwrap import dedent
2+
3+
from reacttrs import reactive
4+
5+
6+
class Foo:
7+
8+
name = reactive("Paul")
9+
age = reactive(33)
10+
birth = reactive(1990)
11+
12+
def watch_name(self, old, new):
13+
print(f"{old=}, {new=}")
14+
15+
def validate_name(self, name):
16+
if name == "John":
17+
print("Hey John!")
18+
return name
19+
20+
def compute_age(self) -> int:
21+
age = 2023 - self.birth
22+
print(f"{age=}")
23+
return age
24+
25+
26+
def test(capfd):
27+
foo = Foo()
28+
29+
foo.name = "John"
30+
out, err = capfd.readouterr()
31+
# watch_name is called first time unless `init=False`
32+
assert out == dedent("""\
33+
old='Paul', new='Paul'
34+
Hey John!
35+
old='Paul', new='John'
36+
""")
37+
38+
foo.name = "Steve"
39+
out, err = capfd.readouterr()
40+
assert out == dedent("""\
41+
old='John', new='Steve'
42+
""")
43+
44+
foo.age
45+
out, err = capfd.readouterr()
46+
# compute_age is called first time unless `init=False`
47+
assert out == dedent("""\
48+
age=33
49+
age=33
50+
""")
51+
52+
foo.birth = 1991
53+
foo.age
54+
out, err = capfd.readouterr()
55+
assert out == dedent("""\
56+
age=32
57+
""")

0 commit comments

Comments
 (0)