Skip to content
Open
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
35 changes: 34 additions & 1 deletion tests/core/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
import numpy
import pytest

from scenic.core.distributions import DiscreteRange, Options, Range, distributionFunction
from scenic.core.distributions import (
DiscreteRange,
Options,
Range,
TupleDistribution,
distributionFunction,
)
from scenic.core.object_types import Object
from scenic.core.type_support import (
CoercionFailure,
Expand Down Expand Up @@ -151,6 +157,33 @@ def check(values, fail=False):
check([(1, 2), (1, 2, 3, 4)], fail=True)


def test_coerce_tupledist_vector():
# 2D and 3D TupleDistribution should be coercible to Vector.
td2 = TupleDistribution(Range(0, 1), Range(0, 1))
td3 = TupleDistribution(Range(0, 1), Range(0, 1), Range(0, 1))

# Can be treated as Vector-compatible random values.
assert canCoerce(td2, Vector)

v2 = coerce(td2, Vector)
v3 = coerce(td3, Vector)
# Special-case in coerce() should produce Vectors, not wrapper distributions.
assert isinstance(v2, Vector)
assert isinstance(v3, Vector)


def test_coerce_tupledist_vector_bad_len():
# Length-1 and length-4 TupleDistributions should be rejected.
td1 = TupleDistribution(Range(0, 1))
td4 = TupleDistribution(Range(0, 1), Range(0, 1), Range(0, 1), Range(0, 1))

with pytest.raises(TypeError, match="expected vector, got tuple of length 1"):
coerce(td1, Vector)

with pytest.raises(TypeError, match="expected vector, got tuple of length 4"):
coerce(td4, Vector)


def test_coerce_distribution_tuple():
y = makeDistWithType(Tuple[float, str])
assert coerce(y, tuple) is y
Expand Down
Loading