|
| 1 | +""" |
| 2 | +Tests for the alias system. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pygmt.alias import Alias, AliasSystem |
| 7 | +from pygmt.exceptions import GMTInvalidInput |
| 8 | +from pygmt.helpers import build_arg_list |
| 9 | + |
| 10 | + |
| 11 | +def func(projection=None, region=None, frame=None, label=None, text=None, **kwargs): |
| 12 | + """ |
| 13 | + A simple function to test the alias system. |
| 14 | + """ |
| 15 | + alias = AliasSystem( |
| 16 | + J=Alias(projection, name="projection"), |
| 17 | + R=Alias(region, name="region", separator="/", size=[4, 6]), |
| 18 | + B=Alias(frame, name="frame"), |
| 19 | + U=[Alias(label, name="label"), Alias(text, name="text", prefix="+t")], |
| 20 | + ).merge(kwargs) |
| 21 | + return build_arg_list(alias.kwdict) |
| 22 | + |
| 23 | + |
| 24 | +def test_alias_system_one_alias(): |
| 25 | + """ |
| 26 | + Test that the alias system works with a single alias. |
| 27 | + """ |
| 28 | + assert func(projection="X10c") == ["-JX10c"] |
| 29 | + assert func(projection="H10c", region=[0, 10, 0, 20]) == ["-JH10c", "-R0/10/0/20"] |
| 30 | + assert func(frame=["WSen", "xaf", "yaf"]) == ["-BWSen", "-Bxaf", "-Byaf"] |
| 31 | + |
| 32 | + |
| 33 | +def test_alias_system_one_alias_short_form(): |
| 34 | + """ |
| 35 | + Test that the alias system works when short-form parameters coexist. |
| 36 | + """ |
| 37 | + # Long-form exists but is not given, and short-form is given. |
| 38 | + with pytest.warns( |
| 39 | + SyntaxWarning, |
| 40 | + match="Short-form parameter 'J' is not recommended. Use long-form parameter 'projection' instead.", |
| 41 | + ): |
| 42 | + assert func(J="X10c") == ["-JX10c"] |
| 43 | + |
| 44 | + # Coexistence of long-form and short-form parameters. |
| 45 | + with pytest.raises(GMTInvalidInput, match="can't coexist"): |
| 46 | + func(projection="X10c", J="H10c") |
| 47 | + |
| 48 | + |
| 49 | +def test_alias_system_multiple_aliases(): |
| 50 | + """ |
| 51 | + Test that the alias system works with multiple aliases. |
| 52 | + """ |
| 53 | + assert func(label="abcd", text="efg") == ["-Uabcd+tefg"] |
| 54 | + |
| 55 | + |
| 56 | +def test_alias_system_multiple_aliases_short_form(): |
| 57 | + """ |
| 58 | + Test that the alias system works with multiple aliases when short-form parameters |
| 59 | + are used. |
| 60 | + """ |
| 61 | + with pytest.warns( |
| 62 | + SyntaxWarning, |
| 63 | + match="Short-form parameter 'U' is not recommended. Use long-form parameter 'label', 'text' instead.", |
| 64 | + ): |
| 65 | + assert func(U="abcd+tefg") == ["-Uabcd+tefg"] |
| 66 | + |
| 67 | + with pytest.raises(GMTInvalidInput, match="can't coexist"): |
| 68 | + func(label="abcd", U="efg") |
| 69 | + |
| 70 | + with pytest.raises(GMTInvalidInput, match="can't coexist"): |
| 71 | + func(text="efg", U="efg") |
0 commit comments