Skip to content

Commit

Permalink
test: precompile @register_unit in an external module.
Browse files Browse the repository at this point in the history
  • Loading branch information
ven-k committed Feb 11, 2024
1 parent 5c00b03 commit cbd071c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/register_units.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import .Units: UNIT_MAPPING, UNIT_SYMBOLS, UNIT_VALUES, _lazy_register_unit
import .SymbolicUnits:
SymbolicDimensionsSingleton, SYMBOLIC_UNIT_VALUES, update_symbolic_unit_values!
import .SymbolicUnits: update_external_symbolic_unit_value

# Update the unit collections
const UNIT_UPDATE_LOCK = Threads.SpinLock()
Expand All @@ -12,7 +11,7 @@ function update_all_values(name_symbol, unit)
i = lastindex(ALL_VALUES)
ALL_MAPPING[name_symbol] = i
UNIT_MAPPING[name_symbol] = i
update_symbolic_unit_values!(name_symbol)
update_external_symbolic_unit_value(name_symbol)
end
end

Expand Down
12 changes: 11 additions & 1 deletion src/symbolic_dimensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,17 @@ module SymbolicUnits
update_symbolic_unit_values!(w::WriteOnceReadMany) = update_symbolic_unit_values!.(w._raw_data)
update_symbolic_unit_values!(UNIT_SYMBOLS)

"""
# Non-eval version of `update_symbolic_unit_values!` for registering units in
# an external module.
function update_external_symbolic_unit_value(unit)
unit = constructorof(DEFAULT_SYMBOLIC_QUANTITY_TYPE)(
DEFAULT_VALUE_TYPE(1.0),
SymbolicDimensionsSingleton{DEFAULT_DIM_BASE_TYPE}(unit)
)
push!(SYMBOLIC_UNIT_VALUES, unit)
end

"""
sym_uparse(raw_string::AbstractString)
Parse a string containing an expression of units and return the
Expand Down
21 changes: 21 additions & 0 deletions test/precompile_test/ExternalUnitRegistration.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module ExternalUnitRegistration

using DynamicQuantities: @register_unit, @u_str, @us_str,
ALL_MAPPING, ALL_SYMBOLS, DEFAULT_QUANTITY_TYPE,
DEFAULT_SYMBOLIC_QUANTITY_OUTPUT_TYPE, UNIT_SYMBOLS, UNIT_MAPPING
using Test

@register_unit Wb u"m^2*kg*s^-2*A^-1"

@testset " Register Unit Inside a Module" begin
for collection in (UNIT_SYMBOLS, ALL_SYMBOLS, keys(ALL_MAPPING._raw_data), keys(UNIT_MAPPING._raw_data))
@test :Wb collection
end

w = u"Wb"
ws = us"Wb"
@test w isa DEFAULT_QUANTITY_TYPE
@test ws isa DEFAULT_SYMBOLIC_QUANTITY_OUTPUT_TYPE
end

end
13 changes: 13 additions & 0 deletions test/unittests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using DynamicQuantities: promote_quantity_on_quantity, promote_quantity_on_value
using DynamicQuantities: UNIT_VALUES, UNIT_MAPPING, UNIT_SYMBOLS, ALL_MAPPING, ALL_SYMBOLS, ALL_VALUES
using DynamicQuantities.SymbolicUnits: SYMBOLIC_UNIT_VALUES
using DynamicQuantities: map_dimensions
using DynamicQuantities: _register_unit
using Ratios: SimpleRatio
using SaferIntegers: SafeInt16
using StaticArrays: SArray, MArray
Expand Down Expand Up @@ -1861,6 +1862,8 @@ all_map_count_before_registering = length(ALL_MAPPING)
@register_unit MySV us"V"
@register_unit MySV2 us"km/h"

@test_throws "Unit `m` is already defined as `1.0 m`" esc(_register_unit(:m, u"s"))

@testset "Register Unit" begin
@test MyV === u"V"
@test MyV == us"V"
Expand All @@ -1880,3 +1883,13 @@ all_map_count_before_registering = length(ALL_MAPPING)
@test my_unit in ALL_SYMBOLS
end
end

push!(LOAD_PATH, joinpath(@__DIR__, "precompile_test"))

using ExternalUnitRegistration: Wb
@testset "Type of Extenral Unit" begin
@test Wb isa DEFAULT_QUANTITY_TYPE
@test Wb/u"m^2*kg*s^-2*A^-1" == 1.0
end

pop!(LOAD_PATH)

0 comments on commit cbd071c

Please sign in to comment.