|
| 1 | +from enum import Enum |
| 2 | + |
| 3 | +import pytest |
| 4 | +from pytest import approx |
| 5 | + |
| 6 | +from geocompy.data import ( |
| 7 | + toenum, |
| 8 | + enumparser, |
| 9 | + parsestr, |
| 10 | + Angle, |
| 11 | + AngleUnit, |
| 12 | + Byte, |
| 13 | + Coordinate |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class A(Enum): |
| 18 | + MEMBER = 1 |
| 19 | + |
| 20 | + |
| 21 | +class TestFunctions: |
| 22 | + def test_toenum(self): |
| 23 | + assert toenum(A, "MEMBER") is A.MEMBER |
| 24 | + assert toenum(A, A.MEMBER) is A.MEMBER |
| 25 | + |
| 26 | + def test_enumparser(self): |
| 27 | + assert callable(enumparser(A)) |
| 28 | + assert enumparser(A)("1") is A.MEMBER |
| 29 | + |
| 30 | + def test_parsestr(self): |
| 31 | + assert parsestr("value") == "value" |
| 32 | + assert parsestr("\"value") == "\"value" |
| 33 | + assert parsestr("value\"") == "value\"" |
| 34 | + assert parsestr("\"value\"") == "value" |
| 35 | + |
| 36 | + |
| 37 | +class TestAngle: |
| 38 | + def test_init(self): |
| 39 | + assert float(Angle(1)) == approx(float(Angle(1, AngleUnit.RAD))) |
| 40 | + |
| 41 | + units = AngleUnit._member_names_.copy() |
| 42 | + units.remove("DMS") |
| 43 | + |
| 44 | + for name in units: |
| 45 | + unit = AngleUnit[name] |
| 46 | + assert ( |
| 47 | + float(Angle(1, name)) # type: ignore |
| 48 | + == approx(float(Angle(1, unit))) |
| 49 | + ) |
| 50 | + |
| 51 | + def test_asunit(self): |
| 52 | + value = Angle(180, 'DEG') |
| 53 | + assert value.asunit('DEG') == approx(180) |
| 54 | + assert value.asunit() == value.asunit('RAD') |
| 55 | + |
| 56 | + def test_normalize(self): |
| 57 | + assert ( |
| 58 | + Angle( |
| 59 | + 370, |
| 60 | + 'DEG', |
| 61 | + normalize=True, |
| 62 | + positive=True |
| 63 | + ).asunit('DEG') |
| 64 | + == approx(10) |
| 65 | + ) |
| 66 | + assert ( |
| 67 | + Angle( |
| 68 | + -10, |
| 69 | + 'DEG', |
| 70 | + normalize=True, |
| 71 | + positive=True |
| 72 | + ).asunit('DEG') |
| 73 | + == approx(350) |
| 74 | + ) |
| 75 | + assert ( |
| 76 | + Angle( |
| 77 | + -370, |
| 78 | + 'DEG', |
| 79 | + normalize=True, |
| 80 | + positive=True |
| 81 | + ).asunit('DEG') |
| 82 | + == approx(350) |
| 83 | + ) |
| 84 | + assert ( |
| 85 | + Angle(370, 'DEG', normalize=True).asunit('DEG') |
| 86 | + == approx(Angle(370, 'DEG').normalized().asunit('DEG')) |
| 87 | + ) |
| 88 | + |
| 89 | + def test_arithmetic(self): |
| 90 | + a1 = Angle(90, 'DEG') |
| 91 | + a2 = Angle(90, 'DEG') |
| 92 | + assert ( |
| 93 | + float(a1 + a2) |
| 94 | + == approx(float(Angle(180, 'DEG'))) |
| 95 | + ) |
| 96 | + assert ( |
| 97 | + float(a1 - a2) |
| 98 | + == approx(float(Angle(0, 'DEG'))) |
| 99 | + ) |
| 100 | + assert ( |
| 101 | + float(a1 * 2) |
| 102 | + == approx(float(Angle(180, 'DEG'))) |
| 103 | + ) |
| 104 | + assert ( |
| 105 | + float(a1 / 2) |
| 106 | + == approx(float(Angle(45, 'DEG'))) |
| 107 | + ) |
| 108 | + with pytest.raises(TypeError): |
| 109 | + a1 * "str" # type: ignore |
| 110 | + |
| 111 | + with pytest.raises(TypeError): |
| 112 | + a1 / "str" # type: ignore |
| 113 | + |
| 114 | + |
| 115 | +class TestByte: |
| 116 | + def test_init(self): |
| 117 | + with pytest.raises(ValueError): |
| 118 | + Byte(-1) |
| 119 | + |
| 120 | + with pytest.raises(ValueError): |
| 121 | + Byte(256) |
| 122 | + |
| 123 | + def test_str(self): |
| 124 | + value = Byte(12) |
| 125 | + assert int(value) == 12 |
| 126 | + assert str(value) == "'0C'" |
| 127 | + |
| 128 | + |
| 129 | +class TestCoordinate: |
| 130 | + def test_init(self): |
| 131 | + value = Coordinate(1, 2, 3) |
| 132 | + assert value.x == 1 |
| 133 | + assert value.y == 2 |
| 134 | + assert value.z == 3 |
| 135 | + assert value[0] == value.x |
| 136 | + x, _, _ = value |
| 137 | + assert x == value.x |
| 138 | + |
| 139 | + def test_arithmetic(self): |
| 140 | + c1 = Coordinate(1, 1, 1) |
| 141 | + c2 = Coordinate(1, 2, 3) |
| 142 | + |
| 143 | + assert c1 + c2 == Coordinate(2, 3, 4) |
| 144 | + assert c1 - c2 == Coordinate(0, -1, -2) |
| 145 | + assert type(+c1) is Coordinate |
| 146 | + c3 = +c1 |
| 147 | + assert c3 is not c1 |
0 commit comments