diff --git a/tests/core/test_typing.py b/tests/core/test_typing.py index ec438e41f..6bdc13f1e 100644 --- a/tests/core/test_typing.py +++ b/tests/core/test_typing.py @@ -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, @@ -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