Skip to content

Commit

Permalink
add a bunch of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thautwarm committed Nov 18, 2019
1 parent 62b4404 commit 393ccdf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
29 changes: 1 addition & 28 deletions src/func_arg_decs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function func_arg(@nospecialize(ex))::FuncArg
@match ex begin
:($var :: $ty) => @with func_arg(var).type = ty
Expr(:kw, var, default) => @with func_arg(var).default = default
Expr(:(=), var, default) => @with func_arg(var).default = default
var => FuncArg(var, unset, unset)
end
end
Expand All @@ -48,34 +49,6 @@ function func_header(@nospecialize(ex))::FuncHeader
end
end

function to_exp(fh::FuncHeader)
args = Any[]
flag = if fh.name !== unset
push!(args, fh.name)
:call
else
:tuple
end
if fh.kwargs !== unset
push!(args, Expr(:parameters, map(to_exp, fh.kwargs)...))
end
if fh.args !== unset
append!(args, map(to_exp, fh.args))
end
exp = Expr(flag, args...)
exp = fh.ret === unset ? exp : Expr(:(::), exp, fh.ret)
exp = fh.fresh === unset ? exp : Expr(:where, exp, fh.fresh...)
exp
end

function to_exp(fa::FuncArg)
base =
fa.name == unset ? :(:: $(fa.type)) :
fa.type != unset ? :($(fa.name) :: $(fa.type)) : fa.name
base = fa.default === unset ? base : Expr(:kw, base, fa.default)
base
end

function of_args(::Unset)
Argument[]
end
Expand Down
26 changes: 24 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rmlines = NGG.rmlines
@gg function f1(a)
quote
x -> a + x
end
end |> rmlines
end

@test f1(1)(2) == 3
Expand All @@ -23,7 +23,7 @@ end
a += 2
x + a
end
end
end |> rmlines
end

@test f2(1)(2) == 5
Expand Down Expand Up @@ -202,6 +202,18 @@ end
end
end
@test bar(2)() == 2 + 20

@gg function foobar(x::T, y::A) where {T <: Number, A <: AbstractArray{T}}
quote
g = x + 20
x = 10
() -> begin
x = g
(A, x + y[1])
end
end
end
@test foobar(2, [3])() == (Vector{Int}, 2 + 20 + 3)
end

@testset "support default arguments" begin
Expand Down Expand Up @@ -254,6 +266,16 @@ end
end
@test test_free_of_let()() == 6
end

@testset "show something" begin
f1 = mk_function(:(x -> x + 1))
f2 = mk_function(:((x :: Int = 2, ) -> x + 1))
@test f1(1) == 2
@test f2() == 3
println(f1)
println(f2)
end

# # From Chris Rackauckas: https://github.com/JuliaLang/julia/pull/32737
# @inline @generated function _invokefrozen(f, ::Type{rt}, args...) where rt
# tupargs = Expr(:tuple,(a==Nothing ? Int : a for a in args)...)
Expand Down

0 comments on commit 393ccdf

Please sign in to comment.