Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
66 changes: 66 additions & 0 deletions tests/syntax/test_assignments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Minimal semantics tests for multiple assignment and LHS unpacking.

import pytest

from tests.utils import compileScenic, sampleEgoActions


def test_multiple_assignment_swap():
scenario = compileScenic(
"""
behavior Foo():
a = 1
b = 2
a, b = b, a
take a
take b
ego = new Object with behavior Foo
"""
)
actions = sampleEgoActions(scenario, maxSteps=2)
assert actions == [2, 1]


def test_lhs_tuple_unpack():
scenario = compileScenic(
"""
behavior Foo():
x, y = (3, 4)
take x
take y
ego = new Object with behavior Foo
"""
)
assert sampleEgoActions(scenario, maxSteps=2) == [3, 4]


def test_unpack_from_distribution_pair():
scenario = compileScenic(
"""
behavior Foo():
pair = Uniform((0, 1), (1, 0))
x, y = pair
take x
take y
ego = new Object with behavior Foo
"""
)
outs = [tuple(sampleEgoActions(scenario, maxSteps=2)) for _ in range(60)]
assert {(0, 1), (1, 0)} <= set(outs)


@pytest.mark.parametrize(
"rhs",
["(1, 2, 3)", "(1,)"], # too many; not enough
)
def test_unpack_arity_mismatch_exec_raises(rhs):
scenario = compileScenic(
f"""
behavior Foo():
x, y = {rhs}
take x
ego = new Object with behavior Foo
"""
)
with pytest.raises(ValueError):
sampleEgoActions(scenario, maxSteps=1)
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