Skip to content

Commit 8b94e8e

Browse files
committed
magicbot: Add tests for tunable
1 parent ecd8a4c commit 8b94e8e

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tests/test_magicbot_tunable.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import ntcore
2+
import pytest
3+
from wpimath import geometry
4+
5+
from magicbot.magic_tunable import setup_tunables, tunable
6+
7+
8+
def test_tunable() -> None:
9+
class Component:
10+
an_int = tunable(1)
11+
ints = tunable([0])
12+
floats = tunable([1.0, 2.0])
13+
rotation = tunable(geometry.Rotation2d())
14+
rotations = tunable([geometry.Rotation2d()])
15+
16+
component = Component()
17+
setup_tunables(component, "test_tunable")
18+
nt = ntcore.NetworkTableInstance.getDefault().getTable("/components/test_tunable")
19+
20+
for name, type_str, value in [
21+
("an_int", "int", 1),
22+
("ints", "int[]", [0]),
23+
("floats", "double[]", [1.0, 2.0]),
24+
]:
25+
topic = nt.getTopic(name)
26+
assert topic.getTypeString() == type_str
27+
assert topic.genericSubscribe().get().value() == value
28+
29+
for name, value in [
30+
("rotation", geometry.Rotation2d()),
31+
]:
32+
struct_type = type(value)
33+
assert nt.getTopic(name).getTypeString() == f"struct:{struct_type.__name__}"
34+
topic = nt.getStructTopic(name, struct_type)
35+
assert topic.subscribe(None).get() == value
36+
37+
for name, struct_type, value in [
38+
("rotations", geometry.Rotation2d, [geometry.Rotation2d()]),
39+
]:
40+
assert nt.getTopic(name).getTypeString() == f"struct:{struct_type.__name__}[]"
41+
topic = nt.getStructArrayTopic(name, struct_type)
42+
assert topic.subscribe([]).get() == value
43+
44+
45+
def test_tunable_errors():
46+
with pytest.raises(TypeError):
47+
48+
class Component:
49+
invalid = tunable(None)
50+
51+
52+
def test_tunable_errors_with_empty_sequence():
53+
with pytest.raises(ValueError):
54+
55+
class Component:
56+
empty = tunable([])

0 commit comments

Comments
 (0)