Skip to content

Commit 8b22c1e

Browse files
committed
Test additional branches in @u_str
1 parent 02142b2 commit 8b22c1e

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

src/uparse.jl

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,20 @@ function map_to_scope(ex::Expr)
6666
elseif ex.head == :tuple
6767
ex.args[:] = map(map_to_scope, ex.args)
6868
return ex
69-
elseif ex.head == :.
70-
if ex.args[1] == :Constants
71-
@assert ex.args[2] isa QuoteNode
72-
return lookup_constant(ex.args[2].value)
73-
else
74-
return ex
75-
end
69+
elseif ex.head == :. && ex.args[1] == :Constants
70+
@assert ex.args[2] isa QuoteNode
71+
return lookup_constant(ex.args[2].value)
7672
else
77-
throw(ArgumentError("Unexpected expression: $ex. Only `:call`, `:tuple`, and `:.` are expected."))
78-
return ex
73+
throw(ArgumentError("Unexpected expression: $ex. Only `:call`, `:tuple`, and `:.` (for `Constants`) are expected."))
7974
end
8075
end
8176
function map_to_scope(sym::Symbol)
8277
if sym in UNIT_SYMBOLS
8378
return lookup_unit(sym)
8479
elseif sym in CONSTANT_SYMBOLS
8580
throw(ArgumentError("Symbol $sym found in `Constants` but not `Units`. Please access the `Constants` module. For example, `u\"Constants.$sym\"`."))
86-
return sym
8781
else
8882
throw(ArgumentError("Symbol $sym not found in `Units` or `Constants`."))
89-
return sym
9083
end
9184
end
9285
function map_to_scope(ex)

test/unittests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,13 @@ end
471471
@test typeof(u"fm") == DEFAULT_QUANTITY_TYPE
472472
@test typeof(u"fm"^2) == DEFAULT_QUANTITY_TYPE
473473

474+
# Can also use tuples:
475+
@test typeof(u"(m, s)") == Tuple{DEFAULT_QUANTITY_TYPE, DEFAULT_QUANTITY_TYPE}
476+
474477
@test_throws LoadError eval(:(u"x"))
475478
VERSION >= v"1.9" && @test_throws "Symbol x not found" uparse("x")
476479
VERSION >= v"1.9" && @test_throws "Symbol c found in `Constants` but not `Units`" uparse("c")
480+
VERSION >= v"1.9" && @test_throws "Unexpected expression" uparse("import ..Units")
477481
end
478482

479483
@testset "Constants" begin

0 commit comments

Comments
 (0)