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

don't warn for identical units when resolving symbols from u"..." #98

Merged
merged 2 commits into from
Sep 17, 2017
Merged
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
26 changes: 14 additions & 12 deletions src/user.jl
Original file line number Diff line number Diff line change
Expand Up @@ -360,26 +360,28 @@ dottify(s) = s
dottify() = Main # needed because fullname(Main) == (). TODO: How to test?

function replace_value(sym::Symbol)
where = [isdefined(unitmodules[i], sym) for i in eachindex(unitmodules)]
count = reduce(+, 0, where)
if count == 0
f = m->(isdefined(m,sym) && ustrcheck_bool(getfield(m, sym)))
inds = find(f, unitmodules)
isempty(inds) &&
error("Symbol $sym could not be found in registered unit modules.")
end

m = unitmodules[findlast(where)]
m = unitmodules[inds[end]]
u = getfield(m, sym)
expr = Expr(:(.), dottify(fullname(m)...), QuoteNode(sym))
if count > 1

any(u != u1 for u1 in getfield.(unitmodules[inds[1:(end-1)]], sym)) &&
warn("Symbol $sym was found in multiple registered unit modules. ",
"We will use the one from $m.")
end
return :(Unitful.ustrcheck($expr))
"We will use the one from $m.")

return expr
end

replace_value(literal::Number) = literal

ustrcheck(x::Union{Units,Dimensions}) = x
ustrcheck(x::Quantity) = x
ustrcheck(x) = error("Symbol $x is not a unit, dimension, or quantity.")
ustrcheck_bool(::Units) = true
ustrcheck_bool(::Dimensions) = true
ustrcheck_bool(::Quantity) = true
ustrcheck_bool(::Any) = false

"""
basefactor(x::Unit)
Expand Down
16 changes: 6 additions & 10 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,24 +286,20 @@ end
end

@testset "Unit string macro" begin
@test macroexpand(:(u"m")) == :(Unitful.ustrcheck(Unitful.m))
@test macroexpand(:(u"m,s")) ==
:(Unitful.ustrcheck(Unitful.m), Unitful.ustrcheck(Unitful.s))
@test macroexpand(:(u"m")) == :(Unitful.m)
@test macroexpand(:(u"m,s")) == :(Unitful.m, Unitful.s)
@test macroexpand(:(u"1.0")) == 1.0
@test macroexpand(:(u"m/s")) ==
:(Unitful.ustrcheck(Unitful.m) / Unitful.ustrcheck(Unitful.s))
@test macroexpand(:(u"1.0m/s")) ==
:((1.0 * Unitful.ustrcheck(Unitful.m)) / Unitful.ustrcheck(Unitful.s))
@test macroexpand(:(u"m^-1")) ==
:(Unitful.ustrcheck(Unitful.m) ^ -1)
@test macroexpand(:(u"m/s")) == :(Unitful.m / Unitful.s)
@test macroexpand(:(u"1.0m/s")) == :((1.0 * Unitful.m) / Unitful.s)
@test macroexpand(:(u"m^-1")) == :(Unitful.m ^ -1)
@test isa(macroexpand(:(u"N m")).args[1], ParseError)
@test isa(macroexpand(:(u"abs(2)")).args[1], ErrorException)

# test ustrcheck(::Quantity)
@test u"h" == Unitful.h

# test ustrcheck(x) fallback to catch non-units / quantities
@test_throws ErrorException u"ustrcheck"
@test_throws ErrorException @eval u"basefactor"
end

@testset "Unit and dimensional analysis" begin
Expand Down