From 09631c547207b93e77fd1055d4b6406957b62cb9 Mon Sep 17 00:00:00 2001 From: Hugo Sansaqua Date: Sat, 27 Jul 2024 14:00:43 +0900 Subject: [PATCH 1/3] add tests for units.unit_name --- tests/test_05_tools/test_500_units.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_05_tools/test_500_units.py b/tests/test_05_tools/test_500_units.py index 4b6760053..1010f9edf 100644 --- a/tests/test_05_tools/test_500_units.py +++ b/tests/test_05_tools/test_500_units.py @@ -1,7 +1,7 @@ # Copyright (c) 2019-2020 Manfred Moitzi # License: MIT License import pytest -from ezdxf.units import DrawingUnits, PaperSpaceUnits, conversion_factor +from ezdxf.units import DrawingUnits, PaperSpaceUnits, conversion_factor, unit_name from math import isclose @@ -159,3 +159,12 @@ def test_conversion_type_error(source, target): def test_conversion_invalid_units(): with pytest.raises(ValueError): conversion_factor(25, 6) + + +def test_unit_name_valid_input(): + assert unit_name(0) == "Unitless" + assert unit_name(1) == "Inches" + + +def test_unit_name_invalid_input(): + assert unit_name(-1) == "unitless" From 802f0254257eb618a10e49af69af1c8350b337af Mon Sep 17 00:00:00 2001 From: Hugo Sansaqua Date: Sat, 27 Jul 2024 14:01:30 +0900 Subject: [PATCH 2/3] Use name property instead of str() in units.unit_name --- src/ezdxf/units.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ezdxf/units.py b/src/ezdxf/units.py index 8218670fb..64985035a 100644 --- a/src/ezdxf/units.py +++ b/src/ezdxf/units.py @@ -201,7 +201,7 @@ def conversion_factor( def unit_name(enum: int) -> str: """Returns the name of the unit enum.""" try: - return str(InsertUnits(enum)).split(".")[1] + return InsertUnits(enum).name except (ValueError, IndexError): return "unitless" From f4c68bb64de40b99e0108a13009b90fce3bf3ce4 Mon Sep 17 00:00:00 2001 From: Hugo Sansaqua Date: Sat, 27 Jul 2024 14:14:17 +0900 Subject: [PATCH 3/3] Clean exception handling in unit_name implementation --- src/ezdxf/units.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ezdxf/units.py b/src/ezdxf/units.py index 64985035a..a82ae0fde 100644 --- a/src/ezdxf/units.py +++ b/src/ezdxf/units.py @@ -202,7 +202,7 @@ def unit_name(enum: int) -> str: """Returns the name of the unit enum.""" try: return InsertUnits(enum).name - except (ValueError, IndexError): + except ValueError: return "unitless"