Skip to content

Commit

Permalink
add macro tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nulinspiratie committed Dec 2, 2024
1 parent f55fb45 commit d2d6301
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/macros/test_method_macro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from quam.core.macro.method_macro import MethodMacro


class TestClass:
def __init__(self, value: int):
self.value = value

@MethodMacro
def add(self, x: int) -> int:
return self.value + x


def test_method_macro_binding():
"""Test that MethodMacro correctly binds to instance methods"""
obj = TestClass(5)
assert isinstance(obj.add, MethodMacro)
assert MethodMacro.is_macro_method(obj.add)
assert obj.add.instance == obj


def test_method_macro_apply():
"""Test that MethodMacro.apply works with instance methods"""
obj = TestClass(5)
assert obj.add.apply(3) == 8 # 5 + 3
assert obj.add(3) == 8 # Should work the same way


def test_is_macro_method():
"""Test the is_macro_method static method"""
obj = TestClass(5)

assert MethodMacro.is_macro_method(obj.add)
assert not MethodMacro.is_macro_method(lambda x: x)
assert not MethodMacro.is_macro_method(42)


def test_method_macro_preserves_metadata():
"""Test that MethodMacro preserves the original function's metadata"""

def original(x: int) -> int:
"""Test docstring"""
return x

decorated = MethodMacro(original)
assert decorated.__doc__ == original.__doc__
assert decorated.__name__ == original.__name__
5 changes: 5 additions & 0 deletions tests/macros/test_pulse_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ def test_pulse_macro_pulse_string(test_qubit, mocker):
pulse_macro = PulseMacro(pulse="test_pulse")
assert pulse_macro.pulse == "test_pulse"

with pytest.raises(AttributeError):
pulse_macro.qubit

test_qubit.macros["test_pulse"] = pulse_macro

assert pulse_macro.qubit is test_qubit

assert test_qubit.get_macros() == {
"test_pulse": pulse_macro,
"align": test_qubit.align,
Expand Down

0 comments on commit d2d6301

Please sign in to comment.