Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pint test #12

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/metrology_apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __pow__(self, other: int | float, /) -> Self: ...

def __rmul__(self, other: Self, /) -> Self: ...
def __rtruediv__(self, other: Self, /) -> Self: ...
def __rpow__(self, other: int | float, /) -> Self: ...
# def __rpow__(self, other: int | float, /) -> Self: ...

@runtime_checkable
class Quantity[V, U: Unit](Protocol):
Expand All @@ -38,3 +38,4 @@ def unit(self) -> U: ...
### Dunder Methods

def __eq__[B](self: "Quantity[V, U]", other: "Quantity[op.CanEq[V, B], U]", /) -> B: ...
#
62 changes: 62 additions & 0 deletions test_pint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pint
import metrology_apis as mt
import operator as ops

Quantity = pint.Quantity
Unit = pint.Unit
# Dimension = pint.UnitsContainer


def test_dimension_op(d: mt.Dimension, op) -> None:
print(op(d, d))


def test_dimension_pow(d: mt.Dimension) -> None:
print(ops.pow(d, 2))


for op in [ops.mul, ops.truediv]:
test_dimension_op(Unit("m").dimension, op)

test_dimension_pow(Unit("m").dimension)

print(isinstance(Unit("m").dimension, mt.Dimension))

def test_unit_op(u: mt.Unit, op) -> None:
print(op(u, u))

def test_unit_pow(u: mt.Unit) -> None:
print(ops.pow(u, 2))

for op in [ops.mul, ops.truediv]:
test_unit_op(Unit("m"), op)

test_unit_pow(Unit("m"))


print(isinstance(Unit("m"), mt.Unit))

def test_unit(q: mt.Quantity) -> None:
print(q.unit)

def test_value(q: mt.Quantity) -> None:
print(q.value)


test_unit(Quantity(1, "m"))
test_value(Quantity(1, "m"))

print(isinstance(Quantity(1, "m"), mt.Quantity))

def test_op(q: mt.Quantity, op) -> None:
print(op(q, q))


# for op in [ops.eq]:
# test_op(Quantity(1, "m"), op)

# for op in [ops.eq, ops.ne, ops.lt, ops.le, ops.gt, ops.ge]:
# test_op(Quantity(1, "m"), op)

# for op in [ops.add, ops.sub, ops.mul, ops.truediv]:
# test_op(Quantity(1, "m"), op)