From acc7401f8d9e4f9b86d55924685b9153e762eefb Mon Sep 17 00:00:00 2001 From: Pablo San-Jose Date: Sat, 11 Apr 2020 20:27:08 +0200 Subject: [PATCH 1/2] fixes #8 --- src/function.jl | 2 +- test/function.jl | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/function.jl b/src/function.jl index 02a4a7a..32e5f6c 100644 --- a/src/function.jl +++ b/src/function.jl @@ -102,7 +102,7 @@ function splitdef(ex::Expr; throw::Bool=true) if !haskey(def, :args) def[:args] = [arg] elseif !haskey(def, :kwargs) - def[:kwargs] = [arg] + def[:kwargs] = arg isa Symbol ? [arg] : [:($(Expr(:kw, arg.args...)))] else return invalid_def("an invalid block expression as arguments") end diff --git a/test/function.jl b/test/function.jl index 96e97b5..4443785 100644 --- a/test/function.jl +++ b/test/function.jl @@ -545,6 +545,28 @@ function_form(short::Bool) = string(short ? "short" : "long", "-form") @test strip_lineno!(c_expr) == strip_lineno!(expr) end + @testset "(x; y = 0)" begin + f, expr = if short + @audit (x; y = 0) -> (x, y) + else + @audit function (x; y = 0); (x, y) end + end + @test length(methods(f)) == 1 + @test f(0) == (0, 0) + @test f(0, y=1) == (0, 1) + + # Note: the semi-colon is missing from the expression + d = splitdef(expr) + @test keys(d) == Set([:head, :args, :kwargs, :body]) + @test d[:args] == [:x] + @test d[:kwargs] == [Expr(:kw, :y, 0)] + + c_expr = combinedef(d) + expr = Expr(:->, Expr(:tuple, Expr(:parameters, Expr(:kw, :y, 0)), :x), Expr(:block, :((x, y)))) + expr.head = short ? :-> : :function + @test strip_lineno!(c_expr) == strip_lineno!(expr) + end + @testset "Expr(:block, :x, :y)" begin expr = Expr(:->, Expr(:block, :x, :y), Expr(:block, :((x, y)))) expr.head = short ? :-> : :function From b0ab5d16bef3f6013fbfde96ab0c3e72580c4bc3 Mon Sep 17 00:00:00 2001 From: Pablo San-Jose Date: Sat, 11 Apr 2020 21:15:25 +0200 Subject: [PATCH 2/2] tighter condition --- src/function.jl | 6 +++++- test/function.jl | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/function.jl b/src/function.jl index 32e5f6c..2f6c5fd 100644 --- a/src/function.jl +++ b/src/function.jl @@ -102,7 +102,11 @@ function splitdef(ex::Expr; throw::Bool=true) if !haskey(def, :args) def[:args] = [arg] elseif !haskey(def, :kwargs) - def[:kwargs] = arg isa Symbol ? [arg] : [:($(Expr(:kw, arg.args...)))] + if arg isa Expr && arg.head == :(=) + def[:kwargs] = [:($(Expr(:kw, arg.args...)))] + else + def[:kwargs] = [arg] + end else return invalid_def("an invalid block expression as arguments") end diff --git a/test/function.jl b/test/function.jl index 4443785..baa50cf 100644 --- a/test/function.jl +++ b/test/function.jl @@ -567,6 +567,29 @@ function_form(short::Bool) = string(short ? "short" : "long", "-form") @test strip_lineno!(c_expr) == strip_lineno!(expr) end + @testset "(x; y = 0, _...)" begin + f, expr = if short + @audit (x; y = 0, _...) -> (x, y) + else + @audit function (x; y = 0, _...); (x, y) end + end + @test length(methods(f)) == 1 + @test f(0) == (0, 0) + @test f(0, y=1) == (0, 1) + @test f(0, y=1, z=2) == (0, 1) + + # Note: the semi-colon is missing from the expression + d = splitdef(expr) + @test keys(d) == Set([:head, :args, :kwargs, :body]) + @test d[:args] == [:x] + @test d[:kwargs] == [Expr(:kw, :y, 0), :(_...)] + + c_expr = combinedef(d) + expr = Expr(:->, Expr(:tuple, Expr(:parameters, Expr(:kw, :y, 0), :(_...)), :x), Expr(:block, :((x, y)))) + expr.head = short ? :-> : :function + @test strip_lineno!(c_expr) == strip_lineno!(expr) + end + @testset "Expr(:block, :x, :y)" begin expr = Expr(:->, Expr(:block, :x, :y), Expr(:block, :((x, y)))) expr.head = short ? :-> : :function