|
4 | 4 | """ |
5 | 5 | import pytest |
6 | 6 |
|
| 7 | +from unyt import accepts, meter, returns, second |
7 | 8 | from unyt.array import unyt_array, unyt_quantity |
| 9 | +from unyt.dimensions import length, time |
8 | 10 | from unyt.testing import assert_allclose_units |
9 | 11 |
|
10 | 12 |
|
@@ -40,3 +42,82 @@ def test_atol_conversion_error(): |
40 | 42 | a2 = unyt_array([1.0, 2.0, 3.0], "cm") |
41 | 43 | with pytest.raises(AssertionError): |
42 | 44 | assert_allclose_units(a1, a2, atol=unyt_quantity(0.0, "kg")) |
| 45 | + |
| 46 | + |
| 47 | +def test_accepts(): |
| 48 | + @accepts(a=time, v=length / time) |
| 49 | + def foo(a, v): |
| 50 | + return a * v |
| 51 | + |
| 52 | + foo(a=2 * second, v=3 * meter / second) |
| 53 | + |
| 54 | + with pytest.raises(TypeError): |
| 55 | + foo(a=2 * meter, v=3 * meter / second) |
| 56 | + |
| 57 | + with pytest.raises(TypeError): |
| 58 | + foo(a=2 * second, v=3 * meter) |
| 59 | + |
| 60 | + |
| 61 | +def test_accepts_partial(): |
| 62 | + @accepts(a=time) |
| 63 | + def bar(a, v): |
| 64 | + return a * v |
| 65 | + |
| 66 | + bar(a=2 * second, v=3 * meter / second) |
| 67 | + bar(a=2 * second, v=3 * meter) |
| 68 | + |
| 69 | + with pytest.raises(TypeError): |
| 70 | + bar(a=2 * meter, v=3 * meter / second) |
| 71 | + |
| 72 | + @accepts(v=length / time) |
| 73 | + def baz(a, v): |
| 74 | + return a * v |
| 75 | + |
| 76 | + baz(a=2 * second, v=3 * meter / second) |
| 77 | + baz(a=2 * meter, v=3 * meter / second) |
| 78 | + |
| 79 | + with pytest.raises(TypeError): |
| 80 | + baz(a=2 * second, v=3 * meter) |
| 81 | + |
| 82 | + |
| 83 | +def test_returns(): |
| 84 | + @returns(length) |
| 85 | + def foo(a, v): |
| 86 | + return a * v |
| 87 | + |
| 88 | + # This usage is deprecated, but we still want to support it for now. |
| 89 | + with pytest.deprecated_call(): |
| 90 | + |
| 91 | + @returns(r_unit=length) |
| 92 | + def bar(a, v): |
| 93 | + return a * v |
| 94 | + |
| 95 | + for func in [foo, bar]: |
| 96 | + func(a=2 * second, v=3 * meter / second) |
| 97 | + |
| 98 | + with pytest.raises(TypeError): |
| 99 | + func(a=2 * meter, v=3 * meter / second) |
| 100 | + |
| 101 | + with pytest.raises(TypeError): |
| 102 | + func(a=2 * second, v=3 * meter) |
| 103 | + |
| 104 | + # We don't support a mixture of the two usage styles. |
| 105 | + with pytest.raises(ValueError): |
| 106 | + |
| 107 | + @returns(length, r_unit=time) |
| 108 | + def _(a, v): |
| 109 | + return a, v |
| 110 | + |
| 111 | + |
| 112 | +def test_returns_multiple(): |
| 113 | + @returns(time, length / time) |
| 114 | + def baz(a, v): |
| 115 | + return a, v |
| 116 | + |
| 117 | + baz(a=2 * second, v=3 * meter / second) |
| 118 | + |
| 119 | + with pytest.raises(TypeError): |
| 120 | + baz(a=2 * meter, v=3 * meter / second) |
| 121 | + |
| 122 | + with pytest.raises(TypeError): |
| 123 | + baz(a=2 * second, v=3 * meter) |
0 commit comments