Skip to content

Commit 6a23eb5

Browse files
committed
test: precompile @register_unit in an external module.
1 parent afef660 commit 6a23eb5

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

src/register_units.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import .Units: UNIT_MAPPING, UNIT_SYMBOLS, UNIT_VALUES, _lazy_register_unit
2-
import .SymbolicUnits:
3-
SymbolicDimensionsSingleton, SYMBOLIC_UNIT_VALUES, update_symbolic_unit_values!
2+
import .SymbolicUnits: update_external_symbolic_unit_value
43

54
# Update the unit collections
65
const UNIT_UPDATE_LOCK = Threads.SpinLock()
@@ -12,7 +11,7 @@ function update_all_values(name_symbol, unit)
1211
i = lastindex(ALL_VALUES)
1312
ALL_MAPPING[name_symbol] = i
1413
UNIT_MAPPING[name_symbol] = i
15-
update_symbolic_unit_values!(name_symbol)
14+
update_external_symbolic_unit_value(name_symbol)
1615
end
1716
end
1817

src/symbolic_dimensions.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,17 @@ module SymbolicUnits
417417
update_symbolic_unit_values!(w::WriteOnceReadMany) = update_symbolic_unit_values!.(w._raw_data)
418418
update_symbolic_unit_values!(UNIT_SYMBOLS)
419419

420-
"""
420+
# Non-eval version of `update_symbolic_unit_values!` for registering units in
421+
# an external module.
422+
function update_external_symbolic_unit_value(unit)
423+
unit = constructorof(DEFAULT_SYMBOLIC_QUANTITY_TYPE)(
424+
DEFAULT_VALUE_TYPE(1.0),
425+
SymbolicDimensionsSingleton{DEFAULT_DIM_BASE_TYPE}(unit)
426+
)
427+
push!(SYMBOLIC_UNIT_VALUES, unit)
428+
end
429+
430+
"""
421431
sym_uparse(raw_string::AbstractString)
422432
423433
Parse a string containing an expression of units and return the
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module ExternalUnitRegistration
2+
3+
using DynamicQuantities: @register_unit, @u_str, @us_str,
4+
ALL_MAPPING, ALL_SYMBOLS, DEFAULT_QUANTITY_TYPE,
5+
DEFAULT_SYMBOLIC_QUANTITY_OUTPUT_TYPE, UNIT_SYMBOLS, UNIT_MAPPING
6+
using Test
7+
8+
@register_unit Wb u"m^2*kg*s^-2*A^-1"
9+
10+
@testset " Register Unit Inside a Module" begin
11+
for collection in (UNIT_SYMBOLS, ALL_SYMBOLS, keys(ALL_MAPPING._raw_data), keys(UNIT_MAPPING._raw_data))
12+
@test :Wb collection
13+
end
14+
15+
w = u"Wb"
16+
ws = us"Wb"
17+
@test w isa DEFAULT_QUANTITY_TYPE
18+
@test ws isa DEFAULT_SYMBOLIC_QUANTITY_OUTPUT_TYPE
19+
end
20+
21+
end

test/unittests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,3 +1880,13 @@ all_map_count_before_registering = length(ALL_MAPPING)
18801880
@test my_unit in ALL_SYMBOLS
18811881
end
18821882
end
1883+
1884+
push!(LOAD_PATH, joinpath(@__DIR__, "precompile_test"))
1885+
1886+
using ExternalUnitRegistration: Wb
1887+
@testset "Type of Extenral Unit" begin
1888+
@test Wb isa DEFAULT_QUANTITY_TYPE
1889+
@test Wb/u"m^2*kg*s^-2*A^-1" == 1.0
1890+
end
1891+
1892+
pop!(LOAD_PATH)

0 commit comments

Comments
 (0)