Skip to content

Commit cbd071c

Browse files
committed
test: precompile @register_unit in an external module.
1 parent 5c00b03 commit cbd071c

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

src/register_units.jl

+2-3
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

+11-1
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
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

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ using DynamicQuantities: promote_quantity_on_quantity, promote_quantity_on_value
88
using DynamicQuantities: UNIT_VALUES, UNIT_MAPPING, UNIT_SYMBOLS, ALL_MAPPING, ALL_SYMBOLS, ALL_VALUES
99
using DynamicQuantities.SymbolicUnits: SYMBOLIC_UNIT_VALUES
1010
using DynamicQuantities: map_dimensions
11+
using DynamicQuantities: _register_unit
1112
using Ratios: SimpleRatio
1213
using SaferIntegers: SafeInt16
1314
using StaticArrays: SArray, MArray
@@ -1861,6 +1862,8 @@ all_map_count_before_registering = length(ALL_MAPPING)
18611862
@register_unit MySV us"V"
18621863
@register_unit MySV2 us"km/h"
18631864

1865+
@test_throws "Unit `m` is already defined as `1.0 m`" esc(_register_unit(:m, u"s"))
1866+
18641867
@testset "Register Unit" begin
18651868
@test MyV === u"V"
18661869
@test MyV == us"V"
@@ -1880,3 +1883,13 @@ all_map_count_before_registering = length(ALL_MAPPING)
18801883
@test my_unit in ALL_SYMBOLS
18811884
end
18821885
end
1886+
1887+
push!(LOAD_PATH, joinpath(@__DIR__, "precompile_test"))
1888+
1889+
using ExternalUnitRegistration: Wb
1890+
@testset "Type of Extenral Unit" begin
1891+
@test Wb isa DEFAULT_QUANTITY_TYPE
1892+
@test Wb/u"m^2*kg*s^-2*A^-1" == 1.0
1893+
end
1894+
1895+
pop!(LOAD_PATH)

0 commit comments

Comments
 (0)