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

Additional conversions for Unitful #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 coverage.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using Coverage
# process '*.cov' files
coverage = process_folder() # defaults to src/; alternatively, supply the folder name as argument
coverage = cat(coverage, process_folder("ext"); dims=1) # add coverage from ext/

LCOV.writefile("lcov.info", coverage)

# process '*.info' files
coverage = merge_coverage_counts(
coverage,
filter!(
let prefixes = (joinpath(pwd(), "src", ""),)
let prefixes = (joinpath(pwd(), "src", ""), joinpath(pwd(), "ext", ""))
c -> any(p -> startswith(c.filename, p), prefixes)
end,
LCOV.readfolder("test"),
Expand Down
12 changes: 11 additions & 1 deletion ext/DynamicQuantitiesUnitfulExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ Base.convert(::Type{DynamicQuantities.Quantity}, x::Unitful.Quantity) =
return DynamicQuantities.Quantity(value, dimension)
end

Base.convert(::Type{DynamicQuantities.Quantity}, x::Unitful.FreeUnits) =
let
convert(DynamicQuantities.Quantity, 1.0 * x)
end

Base.convert(::Type{DynamicQuantities.Dimensions}, d::Unitful.Dimensions{D}) where {D} =
let
cumulator = DynamicQuantities.Dimensions()
Expand All @@ -45,6 +50,11 @@ Base.convert(::Type{DynamicQuantities.Dimensions}, d::Unitful.Dimensions{D}) whe
cumulator
end

Base.convert(::Type{DynamicQuantities.Dimensions}, x::Unitful.FreeUnits) =
let
DynamicQuantities.dimension(convert(DynamicQuantities.Quantity, x))
end

function _map_dim_name_to_dynamic_units(::Type{Unitful.Dimension{D}}) where {D}
# (We could do this automatically, but it's more obvious what we are doing this way.)
D == :Length && return :length
Expand All @@ -54,7 +64,7 @@ function _map_dim_name_to_dynamic_units(::Type{Unitful.Dimension{D}}) where {D}
D == :Temperature && return :temperature
D == :Luminosity && return :luminosity
D == :Amount && return :amount
error("Unknown dimension: $D")
throw(error("Unknown dimension: $D. Valid dimensions are: (:Length, :Mass, :Time, :Current, :Temperature, :Luminosity, :Amount)."))
end


Expand Down
11 changes: 11 additions & 0 deletions test/unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@ x_unitful = 0.2u"mol^2*A^(-1//2)*cd^(2//5)"

@test convert(Unitful.Quantity, x) ≈ x_unitful
@test convert(DynamicQuantities.Quantity, convert(Unitful.Quantity, x)) ≈ x

@test convert(DynamicQuantities.Quantity, u"c") ≈ 2.99792458e8 * DynamicQuantities.Dimensions(length=1, time=-1)
@test convert(DynamicQuantities.Dimensions, u"c") == DynamicQuantities.Dimensions(length=1, time=-1)
@test convert(DynamicQuantities.Dimensions, Unitful.dimension(u"c")) == DynamicQuantities.Dimensions(length=1, time=-1)

# Defining a custom dimension should throw an error:
Unitful.@dimension 𝚾 "𝚾" MyDimension
# @refunit m "m" Meter 𝐋 true
Unitful.@refunit my_unit "x" MyDimension 𝚾 true

@test_throws "Unknown dimension: MyDimension" convert(DynamicQuantities.Quantity, 1.0 * my_unit)