Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions tests/syntax/test_assignments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Minimal semantics tests for multiple assignment and LHS unpacking.

import pytest

from tests.utils import sampleParamPFrom


def test_multiple_assignment_swap():
p = sampleParamPFrom(
"""
a = 1
b = 2
a, b = b, a
param p = (a, b)
"""
)
assert p == (2, 1)


def test_lhs_tuple_unpack():
p = sampleParamPFrom(
"""
x, y = (3, 4)
param p = (x, y)
"""
)
assert p == (3, 4)


def test_unpack_from_distribution_pair():
code = """
pair = (Uniform(0, 1), Uniform(0, 1))
x, y = pair
param p = (x, y)
"""
outs = [sampleParamPFrom(code) for _ in range(20)]
assert len(set(outs)) > 1
for x, y in outs:
assert 0 <= x <= 1
assert 0 <= y <= 1


@pytest.mark.parametrize(
"rhs",
["(1, 2, 3)", "(1,)"], # too many; not enough
)
def test_unpack_arity_mismatch_exec_raises(rhs):
with pytest.raises(ValueError):
sampleParamPFrom(
f"""
x, y = {rhs}
param p = x
"""
)
14 changes: 14 additions & 0 deletions tests/syntax/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ def test_workspace_assign(self):
assert False


class TestAssign:
def test_tuple_lhs_parses(self):
mod = parse_string_helper("a, b = 1, 2")
stmt = mod.body[0]
match stmt:
case Assign(
targets=[Tuple([Name("a"), Name("b")])],
value=Tuple([Constant(1), Constant(2)]),
):
assert True
case _:
assert False


class TestInitialScenario:
def test_initial_scenario(self):
mod = parse_string_helper("initial scenario")
Expand Down
Loading