summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pint/testsuite/test_unit.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/pint/testsuite/test_unit.py b/pint/testsuite/test_unit.py
index 96db871..bb6f7ea 100644
--- a/pint/testsuite/test_unit.py
+++ b/pint/testsuite/test_unit.py
@@ -54,6 +54,25 @@ class TestUnit(QuantityTestCase):
with subtests.test(spec):
assert spec.format(x) == result
+ def test_latex_escaping(self, subtests):
+ ureg = UnitRegistry()
+ ureg.define(r"percent = 1e-2 = %")
+ x = ureg.Unit(UnitsContainer(percent=1))
+ for spec, result in {"L": r"\mathrm{percent}", "L~": r"\mathrm{\%}"}.items():
+ with subtests.test(spec):
+ ureg.default_format = spec
+ assert f"{x}" == result, f"Failed for {spec}, {result}"
+ # no '#' here as it's a comment char when define()ing new units
+ ureg.define(r"weirdunit = 1 = \~_^&%$_{}")
+ x = ureg.Unit(UnitsContainer(weirdunit=1))
+ for spec, result in {
+ "L": r"\mathrm{weirdunit}",
+ "L~": r"\mathrm{\textbackslash \textasciitilde \_\textasciicircum \&\%\$\_\{\}}",
+ }.items():
+ with subtests.test(spec):
+ ureg.default_format = spec
+ assert f"{x}" == result, f"Failed for {spec}, {result}"
+
def test_unit_default_formatting(self, subtests):
ureg = UnitRegistry()
x = ureg.Unit(UnitsContainer(meter=2, kilogram=1, second=-1))