Skip to content

Commit

Permalink
Merge pull request astropy#16661 from eerovaher/unit-formatter-tests
Browse files Browse the repository at this point in the history
Tidy up a few unit formatter tests
  • Loading branch information
mhvk authored Jul 3, 2024
2 parents a88b82a + 172936c commit b1a9b7a
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions astropy/units/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
Regression tests for the units.format package
"""

from __future__ import annotations

import warnings
from contextlib import nullcontext
from fractions import Fraction
from typing import TYPE_CHECKING, NamedTuple

import numpy as np
import pytest
Expand All @@ -18,10 +21,28 @@
from astropy.units.utils import is_effectively_unity
from astropy.utils.exceptions import AstropyDeprecationWarning

if TYPE_CHECKING:
from collections.abc import Iterable


class StringUnitPair(NamedTuple):
string: str
unit: UnitBase


def list_string_unit_pairs(
*test_cases: tuple[Iterable[str], UnitBase],
) -> list[StringUnitPair]:
return [
StringUnitPair(string, unit)
for strings, unit in test_cases
for string in strings
]


@pytest.mark.parametrize(
"strings, unit",
[
"test_pair",
list_string_unit_pairs(
(["m s", "m*s", "m.s"], u.m * u.s),
(["m/s", "m*s**-1", "m /s", "m / s", "m/ s"], u.m / u.s),
(["m**2", "m2", "m**(2)", "m**+2", "m+2", "m^(+2)"], u.m**2),
Expand All @@ -37,27 +58,24 @@
(["mag(ct/s)"], u.MagUnit(u.ct / u.s)),
(["dex"], u.dex),
(["dex(cm s**-2)", "dex(cm/s2)"], u.DexUnit(u.cm / u.s**2)),
],
),
ids=lambda x: x.string,
)
def test_unit_grammar(strings, unit):
for s in strings:
print(s)
unit2 = u_format.Generic.parse(s)
assert unit2 == unit
def test_unit_grammar(test_pair: StringUnitPair):
assert u_format.Generic.parse(test_pair.string) == test_pair.unit


@pytest.mark.parametrize(
"string", ["sin( /pixel /s)", "mag(mag)", "dB(dB(mW))", "dex()"]
)
def test_unit_grammar_fail(string):
with pytest.raises(ValueError):
print(string)
u_format.Generic.parse(string)


@pytest.mark.parametrize(
"strings, unit",
[
"test_pair",
list_string_unit_pairs(
(["0.1nm"], u.AA),
(["mW/m2"], u.Unit(u.erg / u.cm**2 / u.s)),
(["mW/(m2)"], u.Unit(u.erg / u.cm**2 / u.s)),
Expand Down Expand Up @@ -96,13 +114,11 @@ def test_unit_grammar_fail(string):
(["[cm/s2]"], dex(u.cm / u.s**2)),
(["[K]"], dex(u.K)),
(["[-]"], dex(u.dimensionless_unscaled)),
],
),
ids=lambda x: x.string,
)
def test_cds_grammar(strings, unit):
for s in strings:
print(s)
unit2 = u_format.CDS.parse(s)
assert unit2 == unit
def test_cds_grammar(test_pair: StringUnitPair):
assert u_format.CDS.parse(test_pair.string) == test_pair.unit


@pytest.mark.parametrize(
Expand Down Expand Up @@ -134,7 +150,6 @@ def test_cds_grammar(strings, unit):
)
def test_cds_grammar_fail(string):
with pytest.raises(ValueError):
print(string)
u_format.CDS.parse(string)


Expand All @@ -151,8 +166,8 @@ def test_cds_log10_dimensionless():
# These examples are taken from the EXAMPLES section of
# https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_93_001/
@pytest.mark.parametrize(
"strings, unit",
[
"test_pair",
list_string_unit_pairs(
(
["count /s", "count/s", "count s**(-1)", "count / s", "count /s "],
u.count / u.s,
Expand Down Expand Up @@ -217,13 +232,11 @@ def test_cds_log10_dimensionless():
],
(u.count / u.s) * (1.0 / (u.pixel * u.s)),
),
],
),
ids=lambda x: x.string,
)
def test_ogip_grammar(strings, unit):
for s in strings:
print(s)
unit2 = u_format.OGIP.parse(s)
assert unit2 == unit
def test_ogip_grammar(test_pair: StringUnitPair):
assert u_format.OGIP.parse(test_pair.string) == test_pair.unit


@pytest.mark.parametrize(
Expand All @@ -239,7 +252,6 @@ def test_ogip_grammar(strings, unit):
)
def test_ogip_grammar_fail(string):
with pytest.raises(ValueError):
print(string)
u_format.OGIP.parse(string)


Expand Down Expand Up @@ -650,7 +662,6 @@ def test_deprecated_did_you_mean_units():
def test_fits_function(string):
# Function units cannot be written, so ensure they're not parsed either.
with pytest.raises(ValueError):
print(string)
u_format.FITS().parse(string)


Expand Down

0 comments on commit b1a9b7a

Please sign in to comment.